什么是服务治理
Spring Cloud 封装了Netflix公司开发的Eureka模块来实现服务治理在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
什么是服务注册与发现
Eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心桃连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息比如服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用
框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
Eureka系统框架
Eureka包含两个组件:**Eureka Server和Eureka Client **
Eureka Server提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
EurekaClient通过注册中心进行访问
是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
server:
port: 7001
eureka:
instance:
# eureka 服务端实例名
hostname: localhost
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自身是注册中心,只维护服务实例,不检索服务
fetch-registry: false
service-url:
# 设置与 Eureka Server 交互的地址
# 查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerMain {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMain.class,args);
}
}
# Eureka
eureka:
client:
# 表示是否将自己注册到 EurekaServer
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息
# 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用
fetch-registry: true
service-url:
# 注册进注册中心
defaultZone: http://localhost:7001/eureka/
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain {
public static void main(String[] args) {
SpringApplication.run(PaymentMain.class,args);
}
}
spring:
application:
name: consumer-service
# Eureka
eureka:
client:
# 表示是否将自己注册到 EurekaServer
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息
# 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
public class OrderMain {
public static void main(String[] args) {
SpringApplication.run(PaymentMain.class,args);
}
}
相互注册,相互守望
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3.修改映射配置
找到 C:\Windows\System32\drivers\etc 下的 hosts 文件
增加配置
4.yml 配置文件
修改端口7001配置文件
server:
port: 7001
eureka:
instance:
# eureka 服务端实例名
hostname: eureka7001.com
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自身是注册中心,只维护服务实例,不检索服务
fetch-registry: false
service-url:
# 设置与 Eureka Server 交互的地址
# 查询服务和注册服务都需要依赖这个地址
# 相互注册,相互守望
defaultZone: http://eureka7002.com:7002/eureka/
修改端口7002配置文件
server:
port: 7002
eureka:
instance:
# eureka 服务端实例名
hostname: eureka7002.com
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自身是注册中心,只维护服务实例,不检索服务
fetch-registry: false
service-url:
# 设置与 Eureka Server 交互的地址
# 查询服务和注册服务都需要依赖这个地址
# 相互注册,相互守望
defaultZone: http://eureka7001.com:7001/eureka/
修改controller
使用@Value("${server.port}"),注入端口号的值
@Value("${server.port}")
private String serverPort;
@PostMapping(value = "/payment/add")
public CommonResult add(@RequestBody Payment payment){
int result = paymentService.add(payment);
log.info("插入的结果:" + result);
if (result > 0){
return new CommonResult(200,"插入成功,serverPort:" + serverPort, result);
}else {
return new CommonResult(440,"插入失败",null);
}
}
@GetMapping(value = "/payment/query/{id}")
public CommonResult queryPaymentById(@PathVariable(value = "id") Long id){
Payment payment = paymentService.queryPaymentById(id);
log.info("查询的结果:" + payment);
if (!payment.equals(null)){
return new CommonResult(200,"查询结果,serverPort:" + serverPort,payment);
}else {
return new CommonResult(440,"没有对应的记录",null);
}
}
修改yml配置文件
service-url:
# 单机版
# defaultZone: http://localhost:7001/eureka/
# 集群版
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
修改yml配置文件
service-url:
# 单机版
# defaultZone: http://localhost:7001/eureka/
# 集群版
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
负载均衡
修改服务消费者中写死的地址
public final static String PAYMENT_URL = "http://PROVIDER-PAYMENT-SERVICE";
//public final static String PAYMENT_URL = "http://localhost:8001";
开启负载均衡功能 @LoadBalanced 注解
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
问题:包含主机名称,想把主机名称去掉
修改yml配置文件,增加主机名称设置
# Eureka
eureka:
client:
# 表示是否将自己注册到 EurekaServer
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息
# 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用
fetch-registry: true
service-url:
# 单机版
# defaultZone: http://localhost:7001/eureka/
# 集群版
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
# 主机名称
instance:
instance-id: payment8001
增加访问路径显示ip地址
修改yml配置文件,增加设置
# 主机名称
instance:
instance-id: payment8001
# 访问路径可以显示 ip 地址
prefer-ip-address: true
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
@Resource
private DiscoveryClient discoveryClient;
@GetMapping(value = "/payment/discovery")
public Object getMessage(){
// 微服务信息
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("service" + service);
}
// 微服务名称下的具体实例
List<ServiceInstance> instances = discoveryClient.getInstances("PROVIDER-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
// 实例主机名称,服务id,端口号,URI地址
log.info(instance.getHost()+"\t"+instance.getServiceId()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
// 添加开启配置
@EnableDiscoveryClient
保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务。
如果在Eureka Server的首页看到以下这段提示,则说明Eureka进入了保护模式:
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY‘RE NOT.
RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
为什么会产生Eureka自我保护机制?
为了防止EurekaClient可以正常运行,但是与EurekaServer网络不通情况下,EurekaServer不会立刻将EurekaClient服务剔除
默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生(延时、卡顿、拥挤)时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式"来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例
它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留)也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
server:
# 关闭自我保护
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
# 主机名称
instance:
# 心跳检测与续约时间
# Eureka客户端向服务端发送心跳的时间间隔,默认30秒
lease-renewal-interval-in-seconds: 20
# Eureka客户端在收到最后一次心跳后等待的时间上限,默认90秒,超时则删除服务
lease-expiration-duration-in-seconds: 60
原文:https://www.cnblogs.com/hewenhao-blogs/p/13160423.html