springcloud(一)集成Eureka 服务注册与发现

277 阅读1分钟

注册中心

  • 核心依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
  • 启用Eureka Server 在启动类或者配置类上添加 @EnableEurekaServer
/**
 * 服务注册中心
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    private static CountDownLatch latch = new CountDownLatch(1);

    public static void main(String[] args) throws InterruptedException {
        // registerShutdownHook : 平滑关闭 SpringBoot
        SpringApplication.run(EurekaServerApplication.class, args).registerShutdownHook();
        latch.await();
    }

}
  • 配置
spring:
  application:
    name: eureka-server

#server:
#  port: 8761

eureka:
  instance:
    hostname: eureka-server
  client:
    fetch-registry: false         #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    register-with-eureka: false   #false表示不向注册中心注册自己。
    service-url:
      #eureka 高可用 节点间互相注册
#      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
      defaultZone: http://localhost:8761/eureka
  server:
    #关闭自我保护:用于检测服务的上线率,生产环境 需要置为 true
    enable-self-preservation: false
#    清理无效节点的时间间隔
#    eviction-interval-timer-in-ms: 4000
  • 验证 启动项目后访问 http://localhost:8761

服务注册