本文介绍了没有Spring-boot的Eureka服务发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a spring boot micro-service and a REST client. The client is a part of another module and make RESTful calls to the micro-service. The micro-service registers with the Eureka registry and I want my client (Which is not a spring boot project) to use the Eureka to query and get the service endpoints.

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

My problem is since the client is not a Spring-Boot applications I can not use the annotations like @SpringBootApplication, @EnableDiscoveryClient and the DiscoveryClient is not get auto wired to the application. Is there anyway to manually auto-wire the DiscoveryClient bean to the client without using the annotations ?

推荐答案

这就是我这样做的方式。基本上它比我预想的要容易得多。以下内容复制自

Well this is how I did It. Basically it is a lot easier than I anticipated. The following was copied from 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服务器的信息。

Also you have to add a configuration file to the class path. Eureka client uses this file to read the information about the eureka servers.

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一起运行。

Also you have to provide the eureka client as a dependency. Eureka1 supports JDK7 though some part of it has been built with JDK8. However I had to provide older versions of "archaius-core" and "servo-core" to make it run with 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。

Eureka2 fully supports JDK7.

这篇关于没有Spring-boot的Eureka服务发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 11:03