1.Eureka基础知识
1). 什么是服务治理?
Spring Cloud封装Netflix公司开发的Eureka模块来实现服务治理;
传统的RPC远程调用框架中,服务与服务之间的依赖关系复杂,所以需要使用服务治理,管理服务与服务之间的依赖关系,实现服务调用、负载均衡、容错等,实现服务发现与注册等;
2). 什么是服务注册与发现?
Eureka采用CS架构,Eureka Server作为服务注册功能的服务器,是服务注册中心;系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行;
服务器启动时,提供者会将自己的服务器信息(服务地址、通讯地址等以别名形式)注册到注册中心上,消费者会以别名的方式去注册中心上获取实际的服务通讯地址,然后再实现本地RPC调用;
3). Eureka的两个组件?
Eureka包含两个组件:Eureka Server 和 Eureka Client
Eureka Server 提供服务注册功能
各个微服务节点通过配置启动后,会在Eureka Server中进行注册,Eureka Server中的服务中注册表会存储所有可用服务节点的信息,服务节点的信息在界面上可以直观看到;
Eureka Client 通过注册中心进行访问
Eureka Client是一个Java客户端,用于简化Eureka Server的交互,客户端具备一个内置的、使用轮询负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30S),如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把服务节点移除(默认周期为90S).
2. 单机Eureka构建步骤
1). 构建EurekaServer服务端注册中心;
创建cloud-eureka-server7001 Maven工程;
创建POM.xml文件;
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.lichenglin</groupId> <artifactId>cloud2020</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>cloudeurekaserver7001</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--eureka-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.lichenglin</groupId> <artifactId>cloudapicommons</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--boot web actuator--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
创建YAML文件;
server:
port: 7001
eureka:
instance:
# Eureka服务端的实例名称
hostname: localhost
client:
# false表示自己不会向注册中心注册自己
register-with-eureka: false
# false表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务;
fetch-registry: false
# 设置与Eureka Server交互的查询服务和注册服务都去依赖的地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
创建启动类;
package com.lichenglin.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication // 指定该服务作为服务注册中心 @EnableEurekaServer public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class,args); } }
在启动类上添加注解@EnableEurekaServer,将其标注为Eureka服务端;
访问http://localhost:7001;

3. 支付微服务8001入驻进入Eureka Server
导入新增的EurekaClient的jar包;
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
修改YAML配置文件;
eureka:
client:
# 表示自己是否注册进EurekaServer,默认为true;
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,默认为true;单节点无所谓,集群必须设置为true,才能配合Ribbon使用负载均衡;
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
启动类上新增@EnableEurekaClient注解;
package com.lichenglin.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
4. 订单微服务80入驻进入Eureka Server
导入新增EurekaClient的jar包;
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
修改YAML配置文件;
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
启动类新增@EnableEurekaClient注解;
@SpringBootApplication @EnableEurekaClient public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); } }
5. Eureka集群环境搭建

创建cloud-eureka-server7002工程;
修改集群中两个工程的YAML配置文件;(互相守望)
server:
port: 7001
eureka:
instance:
# Eureka服务端的实例名称
hostname: eureka7001.com
client:
# false表示自己不会向注册中心注册自己
register-with-eureka: false
# false表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务;
fetch-registry: false
# 设置与Eureka Server交互的查询服务和注册服务都去依赖的地址
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
在hosts文件中新增域名解析信息;
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
将Payment、Order服务注册进入Eureka集群;
eureka:
client:
# 表示自己是否注册进EurekaServer,默认为true;
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,默认为true;单节点无所谓,集群必须设置为true,才能配合Ribbon使用负载均衡;
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
创建cloud-provider-payment8002工程,构建多个服务提供者;
修改订单服务的OrderController中的访问地址,从固定地址修改为注册的服务名;
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
为RestTemplate开启负载均衡功能@LoadBalanced;
@Configuration public class ApplicationContextConfig { @Bean // 开启RestTemplate的负载均衡机制 @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
6. actuator微服务信息完善
1). 主机名称:服务名称修改
2). 访问信息有IP信息提示
eureka:
instance:
instance-id: payment8002
prefer-ip-address: true
7. 服务发现Discovery
自动注入DiscoveryClient对象,获取服务的相关信息;
@Autowired DiscoveryClient discoveryClient; @GetMapping("/payment/discovery") public List<String> getServerInfo(){ List<String> retList = new ArrayList<>(); List<String> services = discoveryClient.getServices(); for (String service : services) { List<ServiceInstance> instances = discoveryClient.getInstances(service); for (ServiceInstance instance : instances) { String info = instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getScheme() + "\t" + instance.getPort() + "\t" + instance.getServiceId() + "\t" + instance.getUri(); retList.add(info); } } return retList; }
在启动类上添加注解@EnableDiscoveryClient,开启服务发现的功能;
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
8. Eureka自我保护
保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护服务注册表中的信息,不在删除服务注册表中的数据,即不会注销任何微服务;
当在Eureka Server首页看到如下的提示,则说明Eureka进入保护模式:
Eureka自我保护机制的意义:
防止EurekaClient可以正常运行,但是与Eureka Server网络不通的情况下,Eureka Server不会立刻将EurekaClient服务剔除;
什么是自我保护模式?
默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(90S)。当由于网络分区故障,导致微服务与EurekaServer之间无法正常通信,短时间内丢失过多客户端时,节点会进入自我保护模式;
禁止自我保护?
在EurekaServer端,修改YAML配置文件;
eureka:
instance:
# Eureka服务端的实例名称
hostname: eureka7001.com
client:
# false表示自己不会向注册中心注册自己
register-with-eureka: false
# false表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务;
fetch-registry: false
# 设置与Eureka Server交互的查询服务和注册服务都去依赖的地址
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
在Payment8001、Payment8002、Order80服务上,修改YAML配置文件;
eureka:
instance:
instance-id: order80
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1
lease-expiration-duration-in-seconds: 2
浙公网安备 33010602011771号