一、服务端
1. 依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!-- eureka 服务端依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- 安全配置,服务之间注册需要校验 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency> <!-- 健康检查,用于服务之间的心跳检测 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
2. 启动类添加注解
3.1 节点一的yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| server: port: 9991 servlet: context-path: / spring: application: name: yrl-eureka security: user: name: eureka password: eureka eureka: instance: hostname: eureka1.com
instance-id: ${spring.application.name}:${server.port}:@project.version@ prefer-ip-address: false metadata-map: test: 123 client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka2.com:9992/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka3.com:9993/eureka/ registry-fetch-interval-seconds: 30 server: enable-self-preservation: false eviction-interval-timer-in-ms: 1000 renewal-percent-threshold: 0.85 use-read-only-response-cache: false response-cache-update-interval-ms: 1000
|
3.2 节点二的yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| server: port: 9992 servlet: context-path: / spring: application: name: yrl-eureka security: user: name: eureka password: eureka eureka: instance: hostname: eureka2.com
instance-id: ${spring.application.name}:${server.port}:@project.version@ prefer-ip-address: false metadata-map: test: 456 client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka3.com:9993/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka1.com:9991/eureka/ registry-fetch-interval-seconds: 30 server: enable-self-preservation: false eviction-interval-timer-in-ms: 1000 renewal-percent-threshold: 0.85 use-read-only-response-cache: false response-cache-update-interval-ms: 1000
|
3.3 节点三的yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| server: port: 9993 servlet: context-path: / spring: application: name: yrl-eureka security: user: name: eureka password: eureka eureka: instance: hostname: eureka3.com
instance-id: ${spring.application.name}:${server.port}:@project.version@ prefer-ip-address: false metadata-map: test: 789 client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka1.com:9991/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka2.com:9992/eureka/ registry-fetch-interval-seconds: 30 server: enable-self-preservation: false eviction-interval-timer-in-ms: 1000 renewal-percent-threshold: 0.85 use-read-only-response-cache: false response-cache-update-interval-ms: 1000
|
二、 客户端
1. 依赖
1 2 3 4 5 6 7 8 9 10
| <!-- eureka 客户端依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--健康检查--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
2. 启动类添加注解
3. yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server: port: 9302 spring: application: name: test-pay eureka: client: service‐url: defaultZone: http://eureka:eureka@eureka1.com:9991/eureka/,http://eureka:eureka@eureka2.com:9992/eureka/,http://eureka:eureka@eureka3.com:9993/eureka/ fetch-registry: true register-with-eureka: true instance: prefer‐ip‐address: true
management: endpoints: web: exposure: include: '*'
|
三、控制台
当服务出现在available-replicas中才算搭建成功
四、健康检查
1 2 3
| managementUrl=http://192.168.243.1:9993/actuator healthUrl=http://192.168.243.1:9993/actuator/health serviceUrl=http://192.168.243.1:9993/
|
五、cap原则
1 2 3 4 5
| 一致性(Consistency) 可用性(Availability) 分区容错性(Partition tolerance) 1.eureka是ap,去中心化,每个节点是平等的,15分钟内,可用实例低于85%,开启心跳保护,不会剔除心跳异常的实例,保证可用性,仍然能够接受新服务注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用),所以实例调用需要有熔断机制保证 2.zookpeer是cp,由leader提供服务,ZooKeeper选举Leader时候会造成服务一定时间内的不可用,保证了一致性,不保证可用性
|