簡體   English   中英

沒有 Spring-boot 的 Eureka 服務發現

[英]Eureka service discovery without Spring-boot

我已經寫了一個 spring boot 微服務和一個 REST 客戶端。 客戶端是另一個模塊的一部分,並對微服務進行 RESTful 調用。 微服務向 Eureka 注冊中心注冊,我希望我的客戶端(不是 Spring Boot 項目)使用 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 一起運行。

    <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。

要么你使用netflix-eureka-client 不帶spring-cloud 並且必須自己配置所有(這意味着復制EurekaDiscoveryClientConfiguration)

或者你可以運行一個 sidecar 服務。 Sidecar 包含一個 zuul-proxy,它將代理 eureka 發現的服務。 查看Spring Cloud Docs - Sidecar 的多語言支持

Wish 從傳統 spring ( non-boot ) 訪問 Eureka 也變得簡單,比如 @EnableEureka 和 @EnableFeignClient

這是我能讓它工作的最接近的。 此示例在 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 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()); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM