没有Spring-boot的Eureka服务发现

Upu*_*era 9 java spring spring-boot microservices netflix-eureka

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

我的问题是因为客户端不是Spring-Boot应用程序我不能使用类似的注释@SpringBootApplication,@EnableDiscoveryClient并且DiscoveryClient不会自动连接到应用程序.反正有DiscoveryClient没有使用注释手动将bean 自动连接到客户端?

Upu*_*era 17

那就是我这样做的方式.基本上它比我预想的要容易得多.以下是从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() );
Run Code Online (Sandbox Code Playgroud)

您还必须将配置文件添加到类路径.Eureka客户端使用此文件来读取有关eureka服务器的信息.

eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8761/eureka/
eureka.decoderName=JacksonJson
Run Code Online (Sandbox Code Playgroud)

此外,您必须提供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>
Run Code Online (Sandbox Code Playgroud)

Eureka2完全支持JDK7.