服务发现/注册中心

启动一个 eureka 非常简单

只需要添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

在 spring 应用的主类加上注解

@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {

    public static void main(String[] args) {
        SpringApplication.run(RegistryApplication.class, args);
    }
}

一点点配置

eureka:
  instance:
    prefer-ip-address: true
  client:
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0
server:
  port: ${PORT:8761}

eureka 的服务注册/发现中心即配置完成了。

服务发现/注册客户端

客户端向中心注册自己,创建需要:

引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

主类注解

@EnableDiscoveryClient
@SpringBootApplication
public class SpringApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringApplication.class, args);
    }
}

元信息配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://${REGISTRY_HOST:localhost}:${REGISTRY_PORT:8761}/eureka/

面板

spring cloud eukera 是有控制面板的,地址为 host:port/ (如果没有设置 context path)。