2016-02-15 46 views
7

我寫了一個spring引導微服務和一個REST客戶端。客戶端是另一個模塊的一部分,並對微服務進行RESTful調用。微服務向Eureka註冊中心註冊,我希望我的客戶端(不是Spring引導項目)使用Eureka來查詢和獲取服務端點。Eureka服務發現沒有Spring引導

我的問題是因爲客戶端不是Spring-Boot應用程序我不能使用像@SpringBootApplication,@EnableDiscoveryClientDiscoveryClient這樣的註釋未被自動連接到應用程序。無論如何,手動將DiscoveryClient bean自動連接到客戶端而不使用註釋?

+0

檢查'@ EnableDiscoveryClient'註釋,發現該進口和複製該到自己的項目配置。 –

回答

12

那麼這就是我做到的。基本上它比我預想的要容易得多。下面是從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()); 

複製你也需要一個配置文件添加到類路徑。尤里卡客戶端使用該文件來讀取有關尤里卡服務器的信息。

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

此外,您必須提供尤里卡客戶端作爲依賴項。雖然Eureka1的某些部分已經使用JDK8構建,但它支持JDK7。不過,我必須提供舊版本的「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。

2

無論您使用Netflix的-尤里卡客戶端無彈簧雲和必須自己配置所有(這意味着複製EurekaDiscoveryClientConfiguration)

或者你可以運行一個邊車服務。該邊車包括一個zuul-代理,用於代理由尤里卡發現的服務。看看int Spring Cloud Docs - Polyglot support with Sidecar

0

願望從傳統彈簧(非引導)訪問尤里卡也由簡單的像@EnableEureka和@EnableFeignClient

這是最接近我能得到它的工作。這個例子是在尤里卡例子可以在Git的樞紐

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()); 
相關問題