没有Spring-boot的Eureka服务发现

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

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

那就是我这样做的方式。 基本上它比我预想的要容易得多。 以下是从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一起运行。

   com.netflix.archaius archaius-core 0.7.3   com.netflix.servo servo-core 0.10.0  

Eureka2完全支持JDK7。

您可以使用没有spring-cloud的netflix-eureka-client,并且必须自己配置(这意味着复制EurekaDiscoveryClientConfiguration)

或者你可以运行边车服务。 sidecar包括一个zuul-proxy,它将代理eureka发现的服务。 看看Spring Cloud Docs – Sideg的Polyglot支持

希望从遗产弹簧(非启动)访问Eureka也很简单,如@EnableEureka和@EnableFeignClient

这是我能让它工作的最接近的。 此示例在Git Hub中的Eureka示例中提供

 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; } } 

我的客户

 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());