没有Spring-boot的Eureka服务发现

时间:2016-02-15 12:32:50

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

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

我的问题是因为客户端不是Spring-Boot应用程序我不能使用@SpringBootApplication@EnableDiscoveryClient这样的注释,并且DiscoveryClient没有自动连接到应用程序。无论如何都要在不使用注释的情况下手动将DiscoveryClient bean自动连接到客户端?

3 个答案:

答案 0 :(得分:17)

这就是我做到的。基本上它比我预想的要容易得多。以下内容是从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。

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

希望从遗产春天(非启动)访问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 eurekaClient;
    }
}

我的客户

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