01.SpringCloud Eureka (服务注册与发现)
1.Eureka基础知识
什么是服务治理
什么是服务注册
Eureka两组件
2.单机Eureka构建步骤
EurekaServer端服务注册中心
IDEA生成eurekaServer端服务注册中心类似物业公司
新建立cloud-eureka-server7001模块pom
<?xml version="1.0" encoding="UTF-8"?>
springcloud2020
com.chl.springcloud
1.0-SNAPSHOT
4.0.0
cloud-eureka-server7001
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
com.chl.springcloud
cloud-api-commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
test
junit
junit
yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索拉取服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址主启动
@EnableEurekaServer //启用eureka server 的注解
@SpringBootApplication
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}测试http://localhost:7001/
EurekaClient端(支付服务提供方)
EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider,类似学校对学生提供授课服务
cloud-provider-payment8001pom
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
1.X和2.X的对比说明
yml添加eureka的配置
#eureka
eureka:
client:
register-with-eureka: true #注册中心注册自己
fetchRegistry: true #去服务中心拉去服务列表需要去检索服务
service-url:
#单机版的注册 eureka的服务中心注册地址
defaultZone: http://localhost:7001/eureka主启动@EnableEurekaClient #启用eureka客户端依赖
@SpringBootApplication
public class Payment8001 {
public static void main(String[] args) {
SpringApplication.run(Payment8001.class,args);
}
}测试先要启动EurekaServerhttp://localhost:7001/ 微服务注册名配置说明自我保护机制EurekaClient端(订单服务消费方)
EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer,类似来学校上课消费的各位同学cloud-consumer-order80pom
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
yml
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka主启动@EnableEurekaClient #启用eureka客户端依赖
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(Payment8001.class,args);
}
}测试- 先要启动EurekaServer,7001服务
- 再要启动服务提供者provider,8001服务
- 再启动服务消费方,80服务
bug说明
3.集群Eureka构建步骤
Eureka集群原理说明
EurekaServer集群环境构建步骤
参考cloud-eureka-server7001,新建cloud-eureka-server7002修改映射配置找到C:\Windows\System32\drivers\etc路径下的hosts文件修改映射配置添加进hosts文件最后面127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com改yml以前的yml现在的yml 7001
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#单机版
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
#defaultZone: http://eureka7001.com:7001/eureka/
#集群版
defaultZone: http://eureka7002.com:7002/eureka/ 现在的yml 7002server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#集群版
defaultZone: http://eureka7001.com:7001/eureka/ 主启动(复制cloud-eureka-server7001的主启动类到7002即可)@EnableEurekaServer
@SpringBootApplication
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7002.class,args);
}
}支付服务提供方注册到集群
yml #集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka订单服务消费方注册到集群
yml #集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka简单测试01
先要启动EurekaServer,7001/7002服务再要启动服务提供者provider,8001服务再要启动消费者,80http://localhost/consumer/payment/get/31支付服务提供者集群环境构建
参考cloud-provider-payment8001新建cloud-provider-payment8002ymlserver:
port: 8002
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/springcloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapperLocations: classpath:mapper/*/*.xml
type-aliases-package: com.chl.springcloud.entity
#eureka
eureka:
client:
register-with-eureka: true #注册中心注册自己
fetchRegistry: true #去服务中心拉去服务列表需要去检索服务
service-url: #单机版的注册 eureka的服务中心注册地址
#defaultZone: http://localhost:7001/eureka
#集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka主启动@EnableEurekaClient
@SpringBootApplication
public class Payment8002 {
public static void main(String[] args) {
SpringApplication.run(Payment8002.class,args);
}
}修改8001/8002的Controller将端口信息放进返回的结果中4.负载均衡
改为服务名替换ip加端口
订单服务访问地址不能写死,用于负载均衡改为这个:
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";@LoadBalanced注解
使用@LoadBalanced注解赋予RestTemplate负载均衡的能力测试02
先要启动EurekaServer,7001/7002服务再要启动服务提供者provider,8001/8002服务http://localhost/consumer/payment/get/1 结果负载均衡效果达到8001/8002端口交替出现Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号,且该服务还有负载功能了5.actuator微服务信息完善
主机名称:服务名称修改
eureka服务面板显示的实例名称访问信息有ip信息提示
把鼠标放在实例名称上,右下角浏览器会显示ip,方便排查6.服务发现Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息在启动类添加@EnableDiscoveryClient修改cloud-provider-payment8001的PaymentController注入DiscoveryClient,使用 相应的服务发现的API
//注入DiscoveryClient,使用 相应的服务发现的API
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/payment/discovery")
public CommonResult discovery(){
//获取拉取的全部服务 全是服务名的集合
List services = discoveryClient.getServices();
for (String service : services) {
log.info(service);
}
// 根据服务名获取 这个服务的全部实例
List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
//打印服务实例的相应信息
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return new CommonResult(200,"查询成功12端口:"+serverPort,this.discoveryClient);
} 测试先要启动EurekaServer,7001/7002服务再启动8001主启动类,需要稍等一会http://localhost:8001/payment/discovery 7.Eureka自我保护
故障现象
导致原因
一句话:某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存属于CAP里面的AP分支
怎么禁止自我保护(一般生产环境中不会禁止自我保护)
注册中心eureakeServer端7001
出厂默认,自我保护机制是开启的使用- eureka.server.enable-self-preservation = false可以禁用自我保护模式
- eureka.server.eviction-interval-timer-in-ms 设置多少秒剔除服务
生产者客户端eureakeClient端8001
默认- eureka.instance.lease-renewal-interval-in-seconds=30 单位为秒(默认是30秒)
- eureka.instance.lease-expiration-duration-in-seconds=90 单位为秒(默认是90秒)