• 实现服务的注册与发现
    • 开发环境
    • 更改配置
    • 一个最简单的 Eureka Client
    • 运行
    • 源码

    实现服务的注册与发现

    本章节,我们将创建一个micro-weather-eureka-client 作为客户端,并演示如何让将自身向注册服务器进行注册,让其可以其他服务都调用。

    开发环境

    • Gradle 4.0
    • Spring Boot 2.0.0.M3
    • Spring Cloud Netflix Eureka Client Finchley.M2

    更改配置

    增加如下配置:

    1. dependencies {
    2. //...
    3. compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    4. //...
    5. }

    一个最简单的 Eureka Client

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. @RestController
    4. public class Application {
    5. @RequestMapping("/hello")
    6. public String home() {
    7. return "Hello world";
    8. }
    9. public static void main(String[] args) {
    10. SpringApplication.run(Application.class, args);
    11. }
    12. }

    项目配置:

    1. spring.application.name: micro-weather-eureka-client
    2. eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

    运行

    分别在 8081 和 8082 上启动了客户端示例。

    1. java -jar micro-weather-eureka-client-1.0.0.jar --server.port=8081
    2. java -jar micro-weather-eureka-client-1.0.0.jar --server.port=8082

    可以在 Eureka Server 上看到这两个实体的信息。

    eurake-client

    源码

    本章节源码,见micro-weather-eureka-servermicro-weather-eureka-client