使用 http 请求方式获取 eureka server的服务信息
对于一些系统不能接入 eureka server,又需要访问接入eureka server 的服务。
方法一:直接调用服务的地址是一种实现方式,弊端就是地址是写死的,万一服务地址变更则访问不到。
方法二:通过访问 eureka server,发现已经注册的服务地址。然后访问提供服务的主机。
在此主要说明一下如何获取注册在 eureka server 上的服务信息。
可以通过 http 请求操作 eureka server,具体信息可以参考官网地址:
https://github.com/Netflix/eureka/wiki/Eureka-REST-operations
1) 获取所有服务的信息。
GET 请求: localhost:8076/eureka/apps
指定返回 xml 格式的数据,在请求头中添加下面2个:
Content-Type:application/json
Accept:application/xml
如果想返回json数据的格式,在请求头中添加下面2个即可:
Content-Type:application/json
Accept:application/json
2) 获取指定服务的信息。
GET 请求: localhost:8076/eureka/apps/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的 eureka server上,服务名为 “SERVICE-PROVIDER” 的所有 地址信息。返回的是eureka server 上保存的所有服务地址,有可能不可用跟 eureka server的属性有关。
2.4 OkHttp POST 请求,请求方式是 application/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(); } }
如果觉得有用,想赞助一下请移步赞助页面:赞助一下