Use http request way to get eureka server service information

 For some systems can not access eureka server, you need the access eureka server service.

Method One: direct call service address is an implementation of the drawbacks is the address to write is dead, change of address in case the service is not accessible.

Method two: by visiting eureka server, find the service address registered. Then access to the host providing the service.

 

The main explain how to get registered on the eureka server service information.

Eureka server may be operated by a http request, the specific information may refer to the official website address:

https://github.com/Netflix/eureka/wiki/Eureka-REST-operations

 

1) obtain information about all services.

GET request: localhost: 8000 / eureka / apps

Specifies the return xml format data, add the following two in the request header:

Content-Type:application/json
Accept:application/xml

 

If you want to return json data format, to add the following two in the request header:

Content-Type:application/json
Accept:application/json

 

2) to obtain information about the specified services.

GET Request: localhost: 8076 / eureka / apps / STUDENT-SERVICE

STUDENT-SERVICE is the name of your application is spring.application.name

 

2 through acquisition request okHttp

2.1 introduces rely maven

<dependency> 
    <groupId>com.squareup.okhttp3</groupId> 
    <artifactId>okhttp</artifactId> 
    <version>3.11.0</version> 
</dependency>

 

2.2 GET request by

 

        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 micro obtain services address all tools

/**
     * 获取服务的所有地址(注册在 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);
        }
    }

 

 

Examples of Use

List<String> allServiceAddr = getAllServiceAddr("127.0.0.1", "8010", "SERVICE-PROVIDER");
 for (String url : allServiceAddr) {
     System.out.println(url); 
}

Get all the information registered in the address 127.0.0.1 on the eureka server, service named "SERVICE-PROVIDER" of. The return address is saved on all services eureka server, there may not be available with eureka server related properties.

 

2.4 OkHttp POST request, the request mode is application / json

Code examples are as follows:

 

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();
        }
        
    }

 

Guess you like

Origin blog.csdn.net/rubbertree/article/details/90256719