いくつかのシステムでは、ユーレカ、サーバーにアクセスすることができないためには、アクセスユーレカ・サーバ・サービスを必要としています。
方法の一つ:ダイレクトコールサービスアドレスは欠点の実装ですが、書くためのアドレスが死んであり、場合のアドレスの変更サービスにアクセスできません。
方法2:ユーレカサーバーにアクセスし、登録されたサービスのアドレスを見つけます。次に、サービスを提供するホストにアクセスできます。
メインはユーレカ・サーバーのサービス情報に登録された取得する方法について説明します。
ユーレカ・サーバは、HTTPリクエストによって動作させることができる、具体的な情報は、公式ウェブサイトのアドレスを参照することがあります。
https://github.com/Netflix/eureka/wiki/Eureka-REST-operations
1)全てのサービスに関する情報を取得します。
localhostを:8000 /ユーレカ/アプリの要求をGET
リターンxml形式のデータは、リクエストヘッダに以下の二つを追加指定します。
Content-Type:application/json
Accept:application/xml
あなたはJSONデータ・フォーマットを返したい場合は、リクエストヘッダに以下の2を追加します。
Content-Type:application/json
Accept:application/json
2)指定されたサービスに関する情報を取得します。
リクエストをGET:localhostを:8076 /ユーレカ/アプリケーション/ STUDENT-SERVICE
STUDENT-SERVICEは、アプリケーションの名前がspring.application.nameです
取得要求2〜okHttp
2.1紹介は、Mavenを頼ります
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
2.2 GETリクエスト
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://127.0.0.1:8010/eureka/apps/SERVICE-PROVIDER")
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/xml")
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println(response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
2.3マイクロサービスは、すべてのツールに対処し得ます
/**
* 获取服务的所有地址(注册在 eureka server 上的服务)
* @param eurekaIp
* @param eurekaPort
* @param servicename
* @return
*/
public static List<String> getAllServiceAddr(String eurekaIp, String eurekaPort, String servicename) {
List<String> result = new ArrayList<>();
String url = "http://" + eurekaIp + ":" + eurekaPort + "/eureka/apps/" + servicename;
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)//请求接口。如果需要传参拼接到接口后面。
.addHeader("Content-Type","application/json")
.addHeader("Accept","application/xml")
.get()
.build();//创建Request 对象
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
String responseContent = response.body().string();
Matcher matcher = Pattern.compile("<homePageUrl>(.+?)</homePageUrl>").matcher(responseContent);
while (matcher.find()) {
String homepage = matcher.group(1).trim();
result.add(homepage);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 随机获取一个微服务的地址
* @param eurekaIp
* @param eurekaPort
* @param servicename
* @return
*/
public static String getOneServiceAddr(String eurekaIp, String eurekaPort, String servicename) {
List<String> result = new ArrayList<>();
String url = "http://" + eurekaIp + ":" + eurekaPort + "/eureka/apps/" + servicename;
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)//请求接口。如果需要传参拼接到接口后面。
.addHeader("Content-Type","application/json")
.addHeader("Accept","application/xml")
.get()
.build();//创建Request 对象
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
String responseContent = response.body().string();
Matcher matcher = Pattern.compile("<homePageUrl>(.+?)</homePageUrl>").matcher(responseContent);
while (matcher.find()) {
String homepage = matcher.group(1).trim();
if (homepage.endsWith("/")) {
homepage = homepage.substring(0, homepage.length()-1);
}
result.add(homepage);
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (result.isEmpty()) {
return "";
} else {
Collections.shuffle(result);
return result.get(0);
}
}
使用例
List<String> allServiceAddr = getAllServiceAddr("127.0.0.1", "8010", "SERVICE-PROVIDER");
for (String url : allServiceAddr) {
System.out.println(url);
}
ユーレカサーバー上のアドレス127.0.0.1に登録されているすべての情報、の「サービスプロバイダー」という名前のサービスを取得します。リターンアドレスは、すべてのサービスユーレカサーバ上に保存され、ユーレカサーバ関連のプロパティを持つ利用可能ではないかもしれません。
2.4 OkHttp POSTリクエストは、要求モードは、アプリケーション/ JSONであります
次のようにコード例は以下のとおりです。
public static void postRequest() {
OkHttpClient client = new OkHttpClient();
RequestParamBase param = RequestParamBase.builder().id(24).name("requestParam").build();
ObjectMapper mapper = new ObjectMapper();
String json = "";
try {
json = mapper.writeValueAsString(param);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
Request request = new Request.Builder().post(body).url("http://127.0.0.1:8060/student/1").build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseString = response.body().string();
com.sse.model.Response result = mapper.readValue(responseString, com.sse.model.Response.class);
System.out.println(result);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}