【问题标题】:Eureka service discovery without Spring-boot没有 Spring-boot 的 Eureka 服务发现
【发布时间】:2016-05-26 08:53:37
【问题描述】:

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

我的问题是由于客户端不是 Spring-Boot 应用程序,我不能使用 @SpringBootApplication@EnableDiscoveryClientDiscoveryClient 等注释,而 DiscoveryClient 没有自动连接到应用程序。无论如何手动将DiscoveryClient bean 自动连接到客户端而不使用注释?

【问题讨论】:

  • 检查 @EnableDiscoveryClient 注释,找到导入的配置并将其复制到您自己的项目中。

标签: java spring spring-boot microservices netflix-eureka


【解决方案1】:

嗯,我就是这样做的。基本上它比我预期的要容易得多。以下内容抄自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() );

您还必须将配置文件添加到类路径。 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。

【讨论】:

  • 嗨 Upul,我也处于同样的场景,我所有的微服务都是在 Spring MVC 中设计的,但我想我可以在启动时制作 Eureka 服务器,但不能更改客户端,所以必须找出转身要使用此功能,我尝试了您上面的代码,但无法使其工作,请您分享您的完整代码,您是如何做到的。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    希望从传统 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()); 
    

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 2019-03-04
      • 2018-04-15
      • 2017-02-20
      • 2018-06-06
      • 2018-04-21
      • 2015-08-02
      • 2020-01-13
      • 2017-04-04
      相关资源
      最近更新 更多