Spring-Cloud-Netflix-Eureka

Spring Cloud Eureka 注册中心笔记 http://eureka.masikkk.com/

Eureka服务端

maven添加依赖

<dependencies>
  <!-- Eureka注册中心服务端 -->
  <!-- spring-cloud-dependencies/spring-cloud-netflix-dependencies 中规定了版本 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>
</dependencies>

@EnableEurekaServer开启服务端

package com.masikkk.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

}

application.yml配置

server:
  port: 8761

spring:
  application:
    name: eureka

eureka:
  instance:
    hostname: localhost
  client:
    # 默认配置下,注册中心server也会尝试将自己作为客户端来注册到注册中心,禁用其客户端注册行为
    # 是否将自己注册到Eureka Server,默认true
    registerWithEureka: false
    # 是否从Eureka Server获取注册信息,默认true
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Eureka客户端

不再需要@EnableEurekaClient

Spring Cloud Edgware 之前的版本中,需要在 Application 上加 @EnableEurekaClient 注解开启Eureka客户端注册, 从 Spring Cloud Edgware开始,@EnableDiscoveryClient@EnableEurekaClient 可省略。 只要 classpath 中有 spring-cloud-starter-netflix-eureka-client 依赖,并进行相应配置,即可将微服务注册到服务发现组件上。

配置服务端地址

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/