目录
1.5、将EurekaClient端8001注册进EurekaServer成为服务提供者provider
1.6、将EurekaClient端80注册进EurekaServer成为服务消费者consumer
1.7.3、将支付服务8001和订单服务80微服务发布到集群配置中
1、Eureka服务注册与发现
1.1、服务治理
通俗理解:病人看病,门诊前台就是注册中心,病人通过门诊前台挂号,查看哪些医生有时间问诊
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200604170255644.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2OTAzMjYx,size_16,color_FFFFFF,t_70)
1.2、服务注册
1.3、Eureka两组件
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200604172054158.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2OTAzMjYx,size_16,color_FFFFFF,t_70)
1.4、单机Eureka构建步骤
IDEA生成eurekaServer端服务注册中心
1. 建module
cloud-eureka-server7001
2. 改pom
server端依赖对比:
在pom中添加
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.angenin.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3. 写yml
在resources目录下新建application.yml文
4. 主启动
在java包下新建com.angenin.springcloud.EurekaMain7001
@EnableEurekaServer //表示此项目是eureka的服务注册中心
@SpringBootApplication
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
5. 测试
启动项目,在浏览器输入http://localhost:7001/
1.5、将EurekaClient端8001注册进EurekaServer成为服务提供者provider
client端依赖对比
1、引入依赖
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、在yml文件中添加
eureka:
client:
#true表示向注册中心注册自己,默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3、在主配置类上加上@EnableEurekaClient
注解,表示这个项目是eureka的客户端。
4、启动项目,然后刷新页面,成功注册进注册中心。
在yml文件中application.name就是注册进注册中心时的应用名。
1.6、将EurekaClient端80注册进EurekaServer成为服务消费者consumer
-
引入依赖
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、在yml文件中添加
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3、在主配置类上加上@EnableEurekaClient
注解。
4、启动项目,刷新页面
1.7、集群Eureka构建步骤
1.7.1、原理说明
搭建Eureka注册中心集群,实现负载均衡+故障容错。
Eureka集群:相互注册,相互守望。
1.7.2、构建eurekaServer集群环境
- 1、参照cloud-eureka-server7001新建cloud-eureka-server7002和cloud-eureka-server7003
- 2、Windows系统的兄弟就跟着老师修改hosts文件,因为我的是mac系统的,接下来写的步骤是mac的。
- 打开终端,输入sudo vim /etc/hosts来修改hosts文件。(权限不够需要加上sudo并输入密码)在最后一行加入:
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
- 然后
:wq!
保存退出。 - 3、修改7001的yml文件
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false
fetch-registry: false
service-url:
# 单机
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群版 相互注册,相互守望
defaultZone: http://eureka7002.com:7002/eureka/, http://eureka7003.com:7003/eureka/
# defaultZone是固定写法,如果想自定义,需要按以下写法才行:
# region: eureka-server
# availability-zones:
# eureka-server: server1,server2
# service-url:
# server1: http://eureka7002.com:7002/eureka/
# server2: http://eureka7003.com:7003/eureka/
修改7002的yml文件
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false
fetch-registry: false
service-url:
#集群版 相互注册,相互守望
defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7003.com:7003/eureka/ #相互注册,相互守望
修改7003的yml文件
eureka:
instance:
hostname: eureka7003.com #eureka服务端的实例名称
client:
register-with-eureka: false
fetch-registry: false
service-url:
#集群版 相互注册,相互守望
defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/ #相互注册,相互守望
然后启动这三个项目
eureka7001.com:7001
eureka7002.com:7002
eureka7003.com:7003
1.7.3、将支付服务8001和订单服务80微服务发布到集群配置中
把两个项目的yml文件中的defaultZone改为:
#集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
启动5个项目进行测试:(先启动集群,再启动8001,最后启动80)
集群后台截图:
1.7.4、构建支付服务提供者集群环境
注意:服务提供者以 统一的服务名称 cloud-payment-service对外提供服务。服务消费者调用 cloud-payment-service即可。而服务消费者需要通过负载均衡判断是访问8001,还是8002等具体的服务。
- 按照8001新建8002:cloud-provider-payment8002(只多建了一个提供者,建多了怕电脑受不了)。(除了要yml文件中需要改端口号和主配置类,其他直接复制8001的,yml文件中的应用名不需要改,因为是集群,所以应用名需要一致)
- 分别在所有的提供者的PaymentController中加入:(这个@Value是spring的注解)
3.修改消费者的OrderController,把写死的url改为服务名称:
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
4. 然后在消费者的ApplicationContextConfig里的restTemplate方法上加上@LoadBalanced,开启负载均衡功能。效果:8001/8002/8003端口交替出现
Ribbo和Eureka整合后Consumer可以直接调用服务而不用关系地址和端口号。
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@Slf4j
public class OrderController {
// public static final String PAYMENT_URL = "http://localhost:8001";
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
}
5. 启动eurekaServer集群,启动提供者集群,启动消费者。
如果启动提供者后出现,这个错误:Public Key Retrieval is not allowed
请在yml文件中的datasource.datasource.url后加上&allowPublicKeyRetrieval=true即可解决。
另外两个eurekaserver也一样,就不截图。
在浏览器中输入http://localhost/consumer/payment/get/1
,多次刷新可以看到,提供服务的应用在不同的切换,实现负载均衡的效果。
1.8、actuator微服务信息完善
修改三个微服务的yml文件
#######8001#######
# client:
# ... instance要和client对齐
instance:
instance-id: payment8001
prefer-ip-address: true #访问路径可以显示ip地址
#######8002#######
instance:
instance-id: payment8002 #修改显示的主机名
prefer-ip-address: true #访问路径可以显示ip地址
#######80#######
instance:
instance-id: consumer80 #修改显示的主机名
prefer-ip-address: true #访问路径可以显示ip地址
修改前:
修改后:
1.9、服务发现Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息。
1.9.1、修改提供者集群的controller
- 在主配置类上加上@EnableDiscoveryClient注解,启用发现客户端。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
- 在两个提供者的PaymentController中加入:
@Resource
private DiscoveryClient discoveryClient; //springframework的DiscoveryClient(不要导错包了)
@GetMapping("/payment/discovery")
public Object discovery(){
//获取服务列表的信息
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("*******element:" + element);
}
//获取CLOUD-PAYMENT-SERVICE服务的所有具体实例
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
//getServiceId服务器id getHost主机名称 getPort端口号 getUri地址
log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
}
return this.discoveryClient;
}
}
服务注册中心上有两个服务:CLOUD-ORDER-SERVICE 和 CLOUD-PAYMENT-SERVICE
CLOUD-ORDER-SERVICE具体的实例是:payment80
CLOUD-PAYMENT-SERVICE具体的实例是:payment8002 和 8001
测试
对8001进行测试,在浏览器输入:http://localhost:8001/payment/discovery
1.10、Eureka自我保护
只有在一定时间内丢失大量服务的心跳才开启自我保护模式。
先把cloud-eureka-server7001和cloud-provider-payment8001都切回单机版测试禁止自我保护。
cloud-eureka-server7001的yml文件:
# client
# ... server与client对齐
server:
#关闭自我保护,默认为true
enable-self-preservation: false
#心跳的间隔时间,单位毫秒
eviction-interval-timer-in-ms: 2000
cloud-provider-payment8001的yml文件:
#Eureka客户端向服务端发送心跳的时间间隔,单位秒(默认30秒)
lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待的时间上限,单位秒(默认90秒),超时剔除服务
lease-expiration-duration-in-seconds: 2
启动注册中心和提供者:
然后关闭提供者(模拟网络延时),提供者直接被剔除。
原文链接:https://blog.csdn.net/qq_36903261/article/details/106551120