我已经编写了一个Spring Boot微服务和一个REST客户端。客户端是另一个模块的一部分,并且对微服务进行RESTful调用。微服务已在Eureka注册表中注册,我希望我的客户端(不是Spring Boot项目)使用Eureka来查询并获取服务端点。
我的问题是,因为客户端不是Spring-Boot应用程序,所以我无法使用@SpringBootApplication
,@EnableDiscoveryClient
和DiscoveryClient
之类的注释,而且DiscoveryClient
无法自动连接到应用程序。无论如何,是否可以不使用注释将ojit_code 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。