SpringCloud發現服務代碼(EurekaClient,DiscoveryClient)


1.說明

本文介紹SpringCloud發現服務代碼的開發,
通過使用EurekaClient,DiscoveryClient來發現注冊中心的服務等,
從而可以自定義客戶端對注冊中心的高級用法。

2.使用EurekaClient發現服務

通過Spring自動注入的EurekaClient,
不僅能發現服務實例,
還能查看客戶端連接等各種配置。

2.1.代碼

package com.yuwen.spring.config.client.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;

@RestController
public class EurekaClientController {
    private static Logger logger = LoggerFactory.getLogger(DiscoveryClientController.class);

    @Autowired
    private EurekaClient eurekaClient;

    @GetMapping("/eureka")
    public String getEurekaClient() {
        EurekaClientConfig config = eurekaClient.getEurekaClientConfig();
        if (!(config instanceof EurekaClientConfigBean)) {
            return eurekaClient.toString();
        }
        EurekaClientConfigBean eurekaConfig = (EurekaClientConfigBean) config;
        logger.info("serviceUrl=" + eurekaConfig.getServiceUrl());
        Applications applications = eurekaClient.getApplications();
        List<Application> registeredApplications = applications.getRegisteredApplications();
        for (Application application : registeredApplications) {
            logger.info("Application Name=" + application.getName());
            List<InstanceInfo> instances = application.getInstances();
            for (InstanceInfo instance : instances) {
                StringBuilder sb = new StringBuilder();
                sb.append(instance.getAppName()).append("\t").append(instance.getHostName()).append("\t")
                        .append(instance.getPort()).append("\t").append(instance.getVIPAddress());
                logger.info("InstanceInfo=" + sb);
            }
        }

        return eurekaClient.toString();
    }
}

2.2.測試

使用瀏覽器訪問:
http://localhost:8005/eureka
頁面返回:

org.springframework.cloud.netflix.eureka.CloudEurekaClient@6715470c

控制台打印:

serviceUrl={fetch-registry=true, defaultZone=http://localhost:7007/eureka, register-with-eureka=true}
Application Name=CONFIG-CLIENT
InstanceInfo=CONFIG-CLIENT  192.168.171.1   8005    config-client

3.使用DiscoveryClient發現服務

使用DiscoveryClient可以發現注冊中心上的服務,
注冊中心可以是Eureka,也可以是Zookeeper等,
因為不使用原始Netflix EurekaClient,
所以在某種包裝器后面的DiscoveryClient更易於使用。
它也能提供通過服務名稱查詢對應的所有服務實例等功能。

3.1.代碼

package com.yuwen.spring.config.client.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DiscoveryClientController {
    private static Logger logger = LoggerFactory.getLogger(DiscoveryClientController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/discovery")
    public Object getDiscoveryClient() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            logger.info("service=" + service);
        }

        String serviceId = "CONFIG-CLIENT";
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        for (ServiceInstance instance : instances) {
            StringBuilder sb = new StringBuilder();
            sb.append(instance.getServiceId()).append("\t").append(instance.getHost()).append("\t")
                    .append(instance.getPort()).append("\t").append(instance.getUri());
            logger.info("instance=" + sb);
        }
        return discoveryClient;
    }
}

3.1.測試

使用瀏覽器訪問:
http://localhost:8005/discovery

頁面返回:

config-client00config-client0

控制台打印:

service=config-client
instance=CONFIG-CLIENT  192.168.171.1   8005    http://192.168.171.1:8005

可以看到Eureka上面只有一個服務CONFIG-CLIENT,
這個服務只有一個實例http://192.168.171.1:8005,
其實就是提供http://localhost:8005/discovery這個服務的實例。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM