1、Eureka基础知识
- 什么是服务治理

- 什么是服务注册


- Eureka两组件

2、单机Eureka构建步骤
-
IDEA生成EurekaServer端服务注册中心
-
建Module
cloud-eureka-server7001
-
改POM
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> -
写YML
eureka: instance: hostname: localhost #eureka服务端的实例名称 client: #false表示不向注册中心注册自己 register-with-eureka: false #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/

-
-
IDEA生成Eurekaclient端注册到服务端
-
配置Pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> -
yam
eureka: client: #false表示不向注册中心注册自己 register-with-eureka: true #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: true service-url: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://localhost:7001/eureka -
主启动
@SpringBootApplication @EnableEurekaClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }- 测试
1、先启动EurekaServer
2、http://localhost:7001/
3、测试

4、自我保护机制
-

注意
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
yam的配置注意缩进以及空格问题
3、集群Eureka构建步骤

解决办法: 搭建Eureka注册中心集群,实现负载均衡+故障容错
互相注册,互相守望
-
新建cloud-eureka-server7002
参考cloud-eureka-server7001
-
改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.atguigu.springcloud</groupId> <artifactId>cloud-api-common</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> -
修改映射配置
1、修改映射配置添加hosts文件
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
2、刷新hosts文件
ipconfig /flushdns
-
配yml
7001中的yaml
server: port: 7001 spring: application: name: cloud-eureka-service eureka: instance: # eureka服务端的实例名称 hostname: eureka7001.com client: # false表示不向注册中心注册自己 register-with-eureka: false # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务 fetch-registry: false service-url: # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://eureka7002.com:7002/eureka/7002中的yaml
server: port: 7002 spring: application: name: cloud-eureka-service2 eureka: instance: hostname: eureka7002.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/主启动
@SpringBootApplication @EnableEurekaServer public class EurekaMain7002 { public static void main(String[] args) { SpringApplication.run(EurekaMain7002.class,args); } }将支付服务8001微服务发布到上面2台Eureka集群配置中
- 配yaml
eureka: client: #false表示不向注册中心注册自己 register-with-eureka: true #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: true service-url: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 # defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka将订单服务80微服务发布到上面2台Eureka集群配置中
- 配yaml
server: port: 80 spring: application: name: Cloud-order-service eureka: client: #false表示不向注册中心注册自己 register-with-eureka: true #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: true service-url: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 # defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka测试01
- 先要启动EurekaServer,7001/7002服务
- 再要启动服务提供者provider,8001
- 再要启动消费者,80
- http://localhost/consumer/payment/get/31
4、支付服务提供者8001集群环境搭建
-
新建cloud-provider-payment8002
参考cloud-provider-payment8001
-
改POM
与cloud-provider-payment8001相同
<dependencies> <!-- eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-common</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.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</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> -
YAML
eureka: client: #false表示不向注册中心注册自己 register-with-eureka: true #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: true service-url: #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 # defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka -
主启动
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8002.class,args);
}
}
-
业务类
和8001一样
-
修改8001/8002的controller

负载均衡
- 订单服务访问地址不能写死


-
使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力 public RestTemplate getRestTemplate(){ return new RestTemplate(); } } -
ApplicationContextBean
Ribbon的负载均衡功能
-
测试

5、actuator微服务信息完善
-
主机名称:服务名称修改以及没有IP提示
当前问题

修改cloud-provoder-payment8001、cloud-provoder-payment8001
eureka:
client:
#false表示不向注册中心注册自己
register-with-eureka: true
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: true
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001\8002
prefer-ip-address: true
效果

6、服务发现Discovery
对于注册eureka里面的微服务,可以通过服务发现来获得该服务的信息
-
修改cloud-provider-payment8001的Controller
import org.springframework.cloud.client.discovery.DiscoveryClient; @Resource private DiscoveryClient discoveryClient; @GetMapping(value = "/payment/discovery") public Object discovery(){ List<String> services = discoveryClient.getServices(); for (String element : services) { log.info("########element:"+element); } List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-SERVICE"); for (ServiceInstance instance : instances) { log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri()); } return this.discoveryClient; } -
8001的启动类
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } } -
自测

-
结果

6、eureka自我保护
- 故障现象

- 原因

一句话:某时刻 一个微服务不可用了,Eureka不会立刻清理,依旧会对该服务的信息进行保存
属于CAP里面的AP分支
-
怎么禁止自我保护
注册中心eurekaServer端7001
-
出产默认,自我保护机制是开启的
eureka.server.enable-self-preservation=true
-
使用eureka.server.enable-self-preservation=false 可以禁用自我保护模式
-

自我保护机制关闭后的结果:

生产者客户端eurekaClient端8001
-
默认
eureka.instance.lease-renewal-interval-in-seconds=30 eureka.instance.lease-expiration-duration-in-seconds=90 -
配置

- 测试

欢迎转载和分享,记得点赞呀

本文详细介绍了微服务架构下Eureka服务注册与发现机制的构建过程,包括单机与集群环境下Eureka服务器的搭建,以及如何将支付与订单服务注册到Eureka集群中。同时,探讨了Eureka自我保护机制及其禁用方法。
168万+

被折叠的 条评论
为什么被折叠?



