Opening Eureka service without Spring-boot

I wrote a spring boot microservice and a REST client. The client is part of another module and calls RESTful calls to the microservice. Microcertification is registered in the Eureka registry, and I want my client (who is not a spring boot project) to use Eureka to request and receive service endpoints.

My problem is that the client is not Spring-Boot applications, I cannot use annotations like @SpringBootApplication , @EnableDiscoveryClient , and DiscoveryClient will not be automatically connected to the application. Do I need to manually manually connect the DiscoveryClient bean to the client without using annotations?

+7
java spring spring-boot microservices netflix-eureka
source share
3 answers

Ok, this is how I did it. Basically, this is a lot easier than I expected. The following was copied from the 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() ); 

You also need to add the configuration file to the class path. The Eureka client uses this file to read information about eureka servers.

 eureka.preferSameZone=true eureka.shouldUseDns=false eureka.serviceUrl.default=http://localhost:8761/eureka/ eureka.decoderName=JacksonJson 

You must also provide the eureka client as a dependency. Eureka1 supports JDK7, although part of it was built using JDK8. However, I had to provide older versions of "archaius-core" and "servo-core" to run it using 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.

+13
source share

Or you use netflix-eureka-client without spring -cloud, and you need to configure everything yourself (which means duplication of EurekaDiscoveryClientConfiguration)

Or you can start the sidecar service. The stroller includes a zuul-proxy that proxies the services open by eureka. Take a look at Spring Cloud Documents - Polyglot support with Sidecar

+2
source share

The desire to access Eureka from an obsolete spring (no download) is as simple as @EnableEureka and @EnableFeignClient

This is the closest I can get it to work. This example is available in Eureka Git Hub 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()); 
0
source share

All Articles