B站尚硅谷最新版SpringCloud 学习笔记(2)-Eureka服务注册与发现
B站视频:https://www.bilibili.com/video/BV18E411x7eT
上一篇:B站尚硅谷最新版SpringCloud 学习笔记(1)
文章目录
- B站尚硅谷最新版SpringCloud 学习笔记(2)-Eureka服务注册与发现
- 上一篇:[B站尚硅谷最新版SpringCloud 学习笔记(1)](https://blog.csdn.net/weixin_42417934/article/details/112613112)
- 一、Eureka基础知识
- 二、单机Eureka构建步骤
- 三、集群Eureka构建步骤
- 四、actuator微服务信息完善
- 五、服务发现Discovery
- 六、Eureka自我保护
- 下一篇:[B站尚硅谷最新版SpringCloud 学习笔记(3)-Zookeeper服务注册与发现](https://blog.csdn.net/weixin_42417934/article/details/112712676)
一、Eureka基础知识
1.服务治理
2.服务注册
3.Eureka两组件
二、单机Eureka构建步骤
1.IDEA生成eurekaServer端服务注册中心类似物业公司
①创建cloud-eureka-server7001
②改POM
<?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">
<parent>
<artifactId>mycloud2020</artifactId>
<groupId>org.yezhinao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7001</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.yezhinao</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<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>
③创建application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
④创建主启动
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
⑤测试
输入http://localhost:7001/
2.EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider
选中之前的cloud-provider-payment8001
①改写pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
②改写application.yml
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
③改写启动类
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
④测试
启动cloud-eureka-server7001和cloud-provider-payment8001
⑤自我保护机制
3.EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer
选中之前的cloud-consumer-order80
①改写pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
②改写yml
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
③修改启动类
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
④测试
先要启动EurekaServer,7001服务
再要启动服务提供者provider,8001服务
eureka服务器
http://localhost/consumer/payment/get/31
三、集群Eureka构建步骤
1.Eureka集群原理说明
2.EurekaServer集群环境构建步骤
①新建一个cloud-eureka-server7002
②修改pom,添加application.yml,启动类
pom
<?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">
<parent>
<artifactId>mycloud2020</artifactId>
<groupId>org.yezhinao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7002</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.yezhinao</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<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>
application.yml
server:
port: 7002
eureka:
instance:
hostname: localhsot #eureka服务端实例名称
client:
register-with-eureka: false #表示不像注册中心注册自己
fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类
@SpringBootApplication
@EnableEurekaServer
public class Eureka7002 {
public static void main(String[] args) {
SpringApplication.run(Eureka7002.class,args);
}
}
③测试
④修改映射配置
找到C:\Windows\System32\drivers\etc路径下的hosts文件
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
测试,修改成功
⑤修改cloud-eureka-server7001和cloud-eureka-server7002的pom文件
同理eureka7002也是如此
cloud-eureka-server7001配置文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名字
client:
register-with-eureka: false #表示不像注册中心注册自己
fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka
cloud-eureka-server7002配置文件
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名字
client:
register-with-eureka: false #表示不像注册中心注册自己
fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/ #设置与eureka
可见为了区别两台机器的hostname,所以修改了hosts文件
3.cloud-provider-payment8001和cloud-consumer-order80发布到Eureka集群上
①修改pom
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
cloud-provider-payment8001
cloud-consumer-order80
②测试
先要启动EurekaServer,7001/7002服务
再要启动服务提供者provider,8001服务
再要启动消费者,80
http://eureka7001.com:7001/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gXg3M2Hl-1610679375190)(C:\Users\10618\AppData\Roaming\Typora\typora-user-images\image-20210114204244486.png)]
http://localhost/consumer/payment/get/1
4.支付服务提供者8001集群环境构建
新建cloud-provider-payment8002与cloud-provider-payment8001类似
①新建
②修改pom,添加application.yml,添加启动类
pom
<?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">
<parent>
<artifactId>mycloud2020</artifactId>
<groupId>org.yezhinao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8002</artifactId>
<dependencies>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>org.yezhinao</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
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/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.yezhinao.springcloud.entities
启动类
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8002.class,args);
}
}
③复制cloud-provider-payment8001的dao,service,controller,mapper
④修改8001/8002的Controller,修改两者
@RestController
@Slf4j
@RequestMapping(value = "/payment")
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@PostMapping(value = "/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("****插入结果:" + result);
if(result > 0){
return new CommonResult(200,"插入成功,serverPort"+serverPort,result);
}
else {
return new CommonResult(444,"插入失败");
}
}
@GetMapping(value = "/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("*****查询结果:"+payment);
if (payment!=null){ //说明有数据,能查询成功
return new CommonResult(200,"查询成功,serverPort"+serverPort,payment);
}else {
return new CommonResult(444,"没有对应记录,查询ID:"+id,null);
}
}
}
⑤测试
http://localhost:8001/payment/get/1测试访问8001能够访问到,显示自己的端口
http://localhost:8002/payment/get/1测试访问8002能够访问到,显示自己的端口
使用cloud-consumer-order80访问,只能访问8001端口,为什么
原因cloud-consumer-order80的控制层地址写死了
⑥负载均衡
解决上述问题:改造url,换成注册名
不能识别到CLOUD-PAYMENT-SERVICE
使用@LoadBalanced注解赋予RestTemplate负载均衡的能力(这其实是Ribbon的功能)
实现负载均衡
Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号
四、actuator微服务信息完善
两个配置,下面详解
eureka:
instance:
instance-id: payment8001 #主机名称:服务名称修改
prefer-ip-address: true #访问信息有ip信息提示
问题:主机名无法辨认和没有显示ip地址
1.主机名称:服务名称修改
instance:
instance-id: payment8001
将其他客户机修改
效果如下:
2.访问信息有ip信息提示
prefer-ip-address: true
可以看到有IP显示
五、服务发现Discovery
一个注解,下面详解
@EnableDiscoveryClient
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
现只操作cloud-provider-payment8001。
1.修改cloud-provider-payment8001的Controller
@GetMapping("/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("***service:" + service);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info("****instance:" + instance);
log.info("**" + instance.getServiceId() + "\t"
+ instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
}
return this.discoveryClient;
}
2.修改8001主启动类
@EnableDiscoveryClient
3.测试
访问http://localhost:8001/payment/discovery
六、Eureka自我保护
1.故障现象
2.导致原因
某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存
3.禁止自我保护(一般生产环境中不会禁止自我保护)
1.配置注册中心EureakeServer端
server:
# 自我保护模式,当出现出现网络分区、eureka在短时间内丢失过多客户端时,会进入自我保护模式,即一个服务长时间没有发送心跳,eureka 也不会将其删除,默认为true
enable-self-preservation: false
# eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
eviction-interval-timer-in-ms: 2000
2.配置生产者客户端EureakeClient端
#参数用于定义服务续约任务的调用间隔时间,默认30秒。
lease-expiration-duration-in-seconds: 1
#用于定义服务失效的时间,默认90秒。
lease-renewal-interval-in-seconds: 2
3.效果如下
自我保护模式已关闭。如果出现网络/其他问题,这可能无法保护实例过期。
停止Payment8001,服务端立刻剔除
最后一句,Eureka已经停更,但学习其知识还是很有帮助,为后面的注册中心做铺垫
代码:https://gitee.com/yezhinao/SpringCloud-Learn/tree/master/mycloud2020(2)