java - 没有 Spring-boot 的 Eureka 服务发现

标签 java spring spring-boot microservices netflix-eureka

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

我的问题是因为客户端不是 Spring-Boot 应用程序,所以我不能使用像 @SpringBootApplication 这样的注释, @EnableDiscoveryClientDiscoveryClient不会自动连接到应用程序。无论如何手动自动连接 DiscoveryClient bean 到客户端而不使用注释?

最佳答案

我就是这样做的。基本上它比我预期的要容易得多。以下内容摘自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 客户端使用此文件读取有关 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。

关于java - 没有 Spring-boot 的 Eureka 服务发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35409492/

相关文章:

java - 在客户端 Spring Boot 应用程序上配置自定义 OAuth2AccessToken

java - Java编译如何在另一个类中创建一个类的对象?

java - 多类java小程序到jar到html

java - Mockito 测试具有调用网关服务步骤的方法

java - Hibernate ManyToMany双向映射抛出DDL异常

java - 将有效负载获取到 Rest 端点

mysql - 如何在不依赖数据库的情况下使用 Hibernate 5 (JPA) 启动 Spring Boot 应用程序 2?

java - HttpSecurity permitAll 和 WebSecurity 忽略未验证 URL 的功能?

javascript - 单击 anchor 标记后,jQuery Mobile 不会移动到下一页

java - 在 Duration 步骤中迭代 ZonedDateTime 间隔