基础步骤

1、添加引用

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>
  6. </dependencies>

2、添加启动注解

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class SpringCloudEurekaApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringCloudEurekaApplication.class, args);
  6. }
  7. }

3、配置文件

  1. spring:
  2. application:
  3. name: eureka-server
  4. # 单节点
  5. server:
  6. port: 8761
  7. eureka:
  8. client:
  9. service-url:
  10. defaultZone: http://localhost:8761/eureka/
  11. register-with-eureka: false
  12. fetch-registry: false

多节点

和单节点差不多,只是配置文件稍微修改一下而已

双节点

  1. spring:
  2. application:
  3. name: eureka-server
  4. ---
  5. # 启动参数 --spring.profiles.active=server1
  6. spring:
  7. profiles: server1
  8. server:
  9. port: 8761
  10. eureka:
  11. client:
  12. service-url:
  13. defaultZone: http://localhost:8762/eureka/
  14. ---
  15. # 启动参数 --spring.profiles.active=server2
  16. spring:
  17. profiles: server2
  18. server:
  19. port: 8762
  20. eureka:
  21. client:
  22. service-url:
  23. defaultZone: http://localhost:8761/eureka/

三节点

  1. ---
  2. spring:
  3. application:
  4. name: spring-cloud-eureka
  5. profiles: peer1
  6. server:
  7. port: 8000
  8. eureka:
  9. instance:
  10. hostname: peer1
  11. client:
  12. serviceUrl:
  13. defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/
  14. ---
  15. spring:
  16. application:
  17. name: spring-cloud-eureka
  18. profiles: peer2
  19. server:
  20. port: 8001
  21. eureka:
  22. instance:
  23. hostname: peer2
  24. client:
  25. serviceUrl:
  26. defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/
  27. ---
  28. spring:
  29. application:
  30. name: spring-cloud-eureka
  31. profiles: peer3
  32. server:
  33. port: 8002
  34. eureka:
  35. instance:
  36. hostname: peer3
  37. client:
  38. serviceUrl:
  39. defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/