没有 Spring-boot 的 Eureka 服务发现

Eureka service discovery without Spring-boot

我写了一个spring启动微服务和一个REST客户端。客户端是另一个模块的一部分,并对微服务进行 RESTful 调用。微服务在 Eureka 注册表中注册,我希望我的客户端(不是 spring 引导项目)使用 Eureka 来查询和获取服务端点。

我的问题是,因为客户端不是 Spring-Boot 应用程序,所以我无法使用 @SpringBootApplication@EnableDiscoveryClientDiscoveryClient 等注释自动连接到应用程序。是否可以在不使用注释的情况下手动将 DiscoveryClient bean 自动连接到客户端?

要么你使用没有 spring-cloud 的 netflix-eureka-client 并且必须自己配置所有(这意味着复制 EurekaDiscoveryClientConfiguration)

或者您可以 运行 sidecar 服务。 sidecar 包含一个 zuul-proxy,它将代理 eureka 发现的服务。看看 Spring Cloud Docs - Polyglot support with Sidecar

好吧,我就是这样做的。基本上它比我预期的要容易得多。以下内容转自Netflix eureka project.

  DiscoveryManager.getInstance().initComponent(new MyDataCenterInstanceConfig(), new DefaultEurekaClientConfig());

  String vipAddress = "MY-SERVICE";

    InstanceInfo nextServerInfo = null;
    try {
        nextServerInfo = DiscoveryManager.getInstance()
                .getEurekaClient()
                .getNextServerFromEureka(vipAddress, false);
    } catch (Exception e) {
        System.err.println("Cannot get an instance of example service to talk to from eureka");
        System.exit(-1);
    }

    System.out.println("Found an instance of example service to talk to from eureka: "
            + nextServerInfo.getVIPAddress() + ":" + nextServerInfo.getPort());

    System.out.println("healthCheckUrl: " + nextServerInfo.getHealthCheckUrl());
    System.out.println("override: " + nextServerInfo.getOverriddenStatus());

    System.out.println("Server Host Name "+ nextServerInfo.getHostName() + " at port " + nextServerInfo.getPort() );

您还必须将配置文件添加到 class 路径。 Eureka 客户端使用此文件读取有关 eureka 服务器的信息。

eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8761/eureka/
eureka.decoderName=JacksonJson

此外,您还必须提供 eureka 客户端作为依赖项。 Eureka1 支持 JDK7,尽管它的一部分是用 JDK8 构建的。但是,我必须提供 "archaius-core" 和 "servo-core" 的旧版本才能使 JDK7 成为 运行。

    <dependency>
        <groupId>com.netflix.archaius</groupId>
        <artifactId>archaius-core</artifactId>
        <version>0.7.3</version>
    </dependency>
    <dependency>
        <groupId>com.netflix.servo</groupId>
        <artifactId>servo-core</artifactId>
        <version>0.10.0</version>
    </dependency>

Eureka2完全支持JDK7。

希望从遗留 spring(非引导)访问 Eureka 也像 @EnableEureka 和 @EnableFeignClient

一样简单

这是我最接近它的工作方式。这个例子在 Git Hub

的 Eureka-examples 中可用
public class EurekaConfiguration {

    private static ApplicationInfoManager applicationInfoManager;
    private static EurekaClient eurekaClient;

    private static synchronized ApplicationInfoManager initializeApplicationInfoManager(
            EurekaInstanceConfig instanceConfig) {
        if (applicationInfoManager == null) {
            InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
            applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo);
        }

        return applicationInfoManager;
    }

    private static synchronized EurekaClient initializeEurekaClient(ApplicationInfoManager applicationInfoManager,
            EurekaClientConfig clientConfig) {
        if (eurekaClient == null) {
            eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);
        }

        return eurekaClient;
    }
    
    public static EurekaClient getEurekaClient()
    {
        ApplicationInfoManager applicationInfoManager = initializeApplicationInfoManager(new MyDataCenterInstanceConfig());
        EurekaClient client = initializeEurekaClient(applicationInfoManager, new DefaultEurekaClientConfig());
        return client;
    }
}

我的客户

String vipAddress = "NLPService";

        InstanceInfo nextServerInfo = null;
        try {
            nextServerInfo = EurekaConfiguration.getEurekaClient().getNextServerFromEureka(vipAddress, false);
        } catch (Exception e) {
            System.err.println("Cannot get an instance of example service to talk to from eureka");
            System.exit(-1);
        }

        System.out.println("Found an instance of example service to talk to from eureka: "
                + nextServerInfo.getVIPAddress() + ":" + nextServerInfo.getPort());
        
        String serviceBaseURL = "http://"+ nextServerInfo.getHostName()
        +":"+nextServerInfo.getPort();
       
        
        String nlpServiceURL = serviceBaseURL +"/nlp";
        
        RestTemplate restTemplate = new RestTemplate();
        
        NLPInputToBeTransformed input = new NLPInputToBeTransformed();
        input.setInputText(" Test Input ");
        
        
        NLPResponse nlpResponse = restTemplate.postForObject
                (nlpServiceURL, input, NLPResponse.class, new HashMap<>());
        
        System.out.println( " Service Response  " + nlpResponse.getTags());