服务信息显示优化 1 2 3 4 5 6 7 8 9 10 eureka: client: register-with-eureka: true fetchRegistry: true service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka instance: instance-id: payment8001 prefer-ip-address: true
服务发现 Discovery 注册进 eureka 的微服务,可以通过服务发现来获得服务的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package ml.guest997;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class Payment { public static void main (String[] args) { SpringApplication.run(Payment.class, args); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Resource private DiscoveryClient discoveryClient;@GetMapping (value = "/payment/discovery" )public Object discovery ( ) { List<String > services = discoveryClient.getServices(); for (String element : services) { log.info("服务:" + element); } List<ServiceInstance> instances = discoveryClient.getInstances("PAYMENT" ); for (ServiceInstance instance : instances) { log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri()); } return this .discoveryClient; }
浏览器访问 127.0.0.1:8001/payment/discovery,页面和浏览器都会打印出服务信息。
Eureka 自我保护机制 默认情况下,如果 Eureka Server 在一定时间内没有接收到某个服务实例的心跳,Eureka Server 将会注销该实例(默认90秒)。但是当网络分区故障发生时,服务与 Eureka Server 之间无法正常通信,以上行为可能变得非常危险了,因为服务本身其实是健康的,此时本不应该注销这个服务。Eureka 通过自我保护机制来解决这个问题。当 Eureka Server 节点在短时间内丢失过多客户端时,那么这个节点就会进入自我保护模式。在自我保护模式中,Eureka Server 会保护服务注册表中的信息,不再注销任何服务实例。
关闭自我保护机制:
1 2 3 eureka: server: enable-self-preservation: false
更改自我保护配置:
1 2 3 4 5 6 7 8 eureka: instance: lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2
服务注册 Zookeeper Zookeeper 的安装就不赘述了。
准备工作 创建一个 Module,一个空的 Maven 项目。并导入下面的依赖。
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 <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > ml.guest997</groupId > <artifactId > Commons</artifactId > <version > $ {project.version} </version > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-zookeeper-discovery</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-devtools</artifactId > <scope > runtime</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > </dependencies >
编写配置文件 1 2 3 4 5 6 7 8 9 server: port: 8003 spring: application: name: payment cloud: zookeeper: connect-string: 127.0 .0 .1 :2181
主启动类 1 2 3 4 5 6 7 8 9 10 11 12 13 package ml.guest997;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication @EnableDiscoveryClient public class Payment3 { public static void main (String[] args) { SpringApplication.run(Payment3.class, args); } }
测试 启动该模块后,通过 zkCli 获取服务信息。
注意:Zookeeper 的服务节点是临时节点,一段时间检测不到服务发来的心跳,就会将其剔除。
备注: 上面是支付模块的注册,订单模块的注册其实是差不多的,就不再赘述了。