java 没有 Spring-boot 的 Eureka 服务发现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35409492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eureka service discovery without Spring-boot
提问by Upul Doluweera
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 微服务和一个 REST 客户端。客户端是另一个模块的一部分,对微服务进行 RESTful 调用。微服务向 Eureka 注册中心注册,我希望我的客户端(不是 Spring Boot 项目)使用 Eureka 来查询和获取服务端点。
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 ?
我的问题是,由于客户端不是 Spring-Boot 应用程序,因此我无法使用诸如 的注释@SpringBootApplication
,@EnableDiscoveryClient
并且DiscoveryClient
无法自动连接到应用程序。无论如何,是否DiscoveryClient
可以在不使用注释的情况下手动将bean自动连接到客户端?
回答by Upul Doluweera
Well this is how I did It. Basically it is a lot easier than I anticipated. The following was copied from Netflix eureka project.
嗯,这就是我做到的。基本上它比我预期的要容易得多。以下内容是从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() );
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 客户端使用此文件读取有关 Eureka 服务器的信息。
eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8761/eureka/
eureka.decoderName=HymansonJson
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.
此外,您必须提供 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 fully supports JDK7.
Eureka2 完全支持JDK7。
回答by joshiste
Either you use the netflix-eureka-client without spring-cloud and have to configure all by yourself (which means duplicating EurekaDiscoveryClientConfiguration)
要么你用netflix-eureka-client不带spring-cloud,全部自己配置(就是复制EurekaDiscoveryClientConfiguration)
Or you could run a sidecar service. The sidecar includes a zuul-proxy which would proxy the services discovered by eureka. Have a look int the Spring Cloud Docs - Polyglot support with Sidecar
或者你可以运行一个 sidecar 服务。Sidecar 包含一个 zuul-proxy,它将代理 eureka 发现的服务。看一下Spring Cloud Docs - Polyglot support with Sidecar
回答by lives
Wish accessing Eureka from legacy spring ( non-boot ) is also made simple like @EnableEureka and @EnableFeignClient
Wish 从传统 spring ( non-boot ) 访问 Eureka 也像@EnableEureka 和 @EnableFeignClient 一样简单
This is the closest I could get it working . This example is available in Eureka-examples in Git Hub
这是我能让它工作的最接近的一次。此示例在 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;
}
}
My client
我的客户
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());