我已经编写了一个Spring Boot微服务和一个REST客户端。客户端是另一个模块的一部分,并且对微服务进行RESTful调用。微服务已在Eureka注册表中注册,我希望我的客户端(不是Spring Boot项目)使用Eureka来查询并获取服务端点。
我的问题是,由于客户端不是Spring-Boot应用程序,因此我无法使用像这样的注释@SpringBootApplication,@EnableDiscoveryClient并且DiscoveryClient不会自动连接到应用程序。无论如何,有DiscoveryClient没有使用注释手动将bean 自动连线到客户端的情况?
@SpringBootApplication
@EnableDiscoveryClient
DiscoveryClient
好吧,这就是我的做法。基本上,这比我预期的要容易得多。以下内容是从Netflix eureka项目复制而来的。
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。