一、服务端

1、POM

  1. <dependencies>
  2. <!--eureka-server-->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  6. </dependency>
  7. <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
  8. <dependency>
  9. <groupId>com.tfjy.springcloud</groupId>
  10. <artifactId>cloud-api-commons</artifactId>
  11. <version>1.0-SNAPSHOT</version>
  12. </dependency>
  13. <!--boot web actuator-->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-web</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-actuator</artifactId>
  21. </dependency>
  22. <!--一般通用配置-->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-devtools</artifactId>
  26. <scope>runtime</scope>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.projectlombok</groupId>
  31. <artifactId>lombok</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>junit</groupId>
  40. <artifactId>junit</artifactId>
  41. </dependency>
  42. </dependencies>

2、YML

  1. server:
  2. port: 7001
  3. eureka:
  4. instance:
  5. hostname: eureka7001.com #eureka服务端的实例名称
  6. client:
  7. register-with-eureka: false #false表示不向注册中心注册自己。
  8. fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  9. service-url:
  10. #集群指向其它eureka
  11. #defaultZone: http://eureka7002.com:7002/eureka/
  12. #单机就是7001自己
  13. defaultZone: http://localhost:7001/eureka/
  14. #server:
  15. #关闭自我保护机制,保证不可用服务被及时踢除
  16. #enable-self-preservation: false
  17. #eviction-interval-timer-in-ms: 2000

3、主启动

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

二、客户端

1、POM

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

2、YML

  1. eureka:
  2. client:
  3. #表示是否将自己注册进EurekaServer默认为true。
  4. register-with-eureka: true
  5. #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
  6. fetchRegistry: true
  7. service-url:
  8. #单机版
  9. defaultZone: http://localhost:7001/eureka
  10. # 集群版
  11. #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  12. instance:
  13. instance-id: payment8001
  14. #访问路径可以显示IP地址
  15. prefer-ip-address: true
  16. #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
  17. #lease-renewal-interval-in-seconds: 1
  18. #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
  19. #lease-expiration-duration-in-seconds: 2

3、主启动

  1. @EnableEurekaClient

https://juejin.cn/post/6844904001444511758