我写了一个spring boot微服务和一个REST客户端。客户端是另一个模块的一部分,对微服务进行 RESTful 调用。微服务在 Eureka 注册表中注册,我希望我的客户端(不是 spring boot 项目)使用 Eureka 来查询和获取服务端点。
我的问题是因为客户端不是 Spring-Boot 应用程序,所以我不能使用像 @SpringBootApplication
这样的注释, @EnableDiscoveryClient
和 DiscoveryClient
不会自动连接到应用程序。无论如何手动自动连接 DiscoveryClient
bean 到客户端而不使用注释?
最佳答案
我就是这样做的。基本上它比我预期的要容易得多。以下内容摘自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。
关于java - 没有 Spring-boot 的 Eureka 服务发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35409492/