SpringCloud 详解(四)

服务信息显示优化

1
2
3
4
5
6
7
8
9
10
eureka:
client:
register-with-eureka: true
#是否从 Eureka Server 抓取已有的注册信息,默认为 true。单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用负载均衡。
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001 #前缀一般会是公司缩写或项目名称
prefer-ip-address: true #鼠标移动到 eureka 页面下的服务时会在浏览器左下角出现 ip 地址

服务发现 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 //启动 Eureka Client
@EnableDiscoveryClient //启动 Discovery Client
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(); //注册进 eureka 的所有服务
for (String element : services) {
log.info("服务:" + element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("PAYMENT"); //通过服务 id 获取全部实例
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:
#心跳检测与续约时间
#开发时没置小些,保证服务关闭后注册中心能即使剔除服务
#Eureka 客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-renewal-interval-in-seconds: 1
#Eureka 服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务。
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 #zookeeper 地址

主启动类

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 //启动 Discovery Client
public class Payment3 {
public static void main(String[] args) {
SpringApplication.run(Payment3.class, args);
}
}

测试

启动该模块后,通过 zkCli 获取服务信息。

注意:Zookeeper 的服务节点是临时节点,一段时间检测不到服务发来的心跳,就会将其剔除。

备注:

上面是支付模块的注册,订单模块的注册其实是差不多的,就不再赘述了。