Eureka service discovery without Spring-boot

后端 未结 3 1774
日久生厌
日久生厌 2020-12-15 10:29

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

3条回答
  •  粉色の甜心
    2020-12-15 11:03

    Wish accessing Eureka from legacy spring ( non-boot ) is also made simple like @EnableEureka and @EnableFeignClient

    This is the closest I could get it working . This example is available in Eureka-examples in Git Hub

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

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

提交回复
热议问题