Eureka——注册中心集群搭建
目的:负载均衡+故障容错
原理:互相注册、相互守望、对外暴露一个接口。
一、实现:
1.建modol
2.改pom
3.修改映射配置
c:\windows\system32\drivers\etc路径下的hosts文件
############SpringCloud2021.07.09##############
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
4.写yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
# false表示不想注册中心注册自己
register-with-eureka: false
# false标志自己端就是注册中心,职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#将自己注册到其他兄弟服务者
defaultZone: http://eureka7002.com:7002/eureka/
二、将微服务发布到Eureka集群中
修改yml,配置集群
eureka:
client:
# true表示把自己注册到注册中心
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,默认为ture,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka/
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
Eureka——服务提供者集群
1.备份服务(注)
(1) 改pom(同)
(2) 写yml(修改端口、配置注册中心集群)
2.配置负载均衡
(1)使用微服务别名调用服务
(2)配置restTempleat负载均衡
在RestTempleate的工具类中的获取方法上开启默认原则的RestTemPleate的负载均衡机制。
@LoadBalanced /