1 微服务架构,看这一篇文章就够了
839 2023-04-03 03:24:35
Eureka Server 作为服务注册功能的服务器,它是服务注册中心,而系统中其他微服务,使用 Eureka 的客户端连接到 Eureka Server 并维持心跳连接,这样系统维护人员就可以通过 Eureka Server来监控各个微服务是否正常运行。
在服务注册与发现中有一个注册中心,服务器启动时,会把当前自己的服务器信息比如服务地址,通信地址等注册到注册中心上,另一方(消费者)以别名的方式在注册中心上获取实际的服务器通讯地址,然后再实现本地RPC调用远程RPC。
<dependencies> <!--eureka-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </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> </dependency> </dependencies>
server: port: 7001eureka: 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// 声明自己是 eureka 的服务端@EnableEurekaServerpublic class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class,args); }}
<!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
eureka: client: #表示是否将自己注册进EurekaServer默认为true register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用 负载均衡 fetch-registry: false service-url: defaultZone: http://localhost:7001/eureka
高可用,如果注册中心只有一个,出了故障就会导致整个服务环境不可用 解决方法:搭建Eureka注册中心集群,实现负载均衡+故障排错
多个 eureka serve 相互注册,保障信息共享。
将7001模块复制
粘贴后修改 pom 文件,yml文件,主启动类
两个eureka相互注册
只需修改application.yml
修改yml
eureka: instance: instance-id: payment8002 # 自定义主机名 prefer-ip-address: true # 设置暴露ip地址
查看http://localhost:7001/页面可以点进查看详情
8001 的 controller添加代码
@Resource private DiscoveryClient discoveryClient; @GetMapping("/payment/discovery") public DiscoveryClient discovery(){ // 得到所有服务名 List<String> services = discoveryClient.getServices(); services.forEach(ele->{ log.info("***service***"+ele); }); // 得到服务名对应的信息 List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); instances.forEach(ele->{ log.info(ele.getServiceId()+"\t"+ele.getHost()+"\t"+ele.getPort()+"\t"+ele.getUri()); }); return discoveryClient; }
8001 主类添加注解 @EnableDiscoveryClient
重启服务,观察日志
为了防止 EurekaClient 可以正常运行,但是在 EurekaServer 网络不通的情况下,EurekaServer 不会立刻将 EurekaClient 服务剔除。2. 什么是自我保护模式?默认情况下,EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90s),但是当网络分区故障发生,微服务与 EurekaServer 之间无法正常通信,以上行为就非常危险。当EurekaServer节点在短时间内丢失过多客户端时,那么这个节点就会进入自我保护模式。
yml文件
# 7001eureka:server: # 关闭自我保护机制 enable-self-preservation: false # 心跳时间默认90s,改为2000ms,即2s eviction-interval-timer-in-ms: 2000# 8001eureka:instance: #eureka客户端发送心跳的时间间隔,默认30s lease-renewal-interval-in-seconds: 1 #eureka服务端在收到最后一次心跳等待的时间上线,默认90s lease-expiration-duration-in-seconds: 2
访问 eureka7001.com:7001/ 可以看到红字THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.代表自我保护关闭
关闭8001服务可以看到在 eureka 页面两秒后 8001 服务消失
服务发现:Consul的客户端可以注册服务,例如 api或mysql,其他客户端可以使用Consul来发现给定服务的提供者。使用DNS或HTTP,应用程序可以轻松找到它们依赖的服务。
运行状况检查:领事客户端可以提供任何数量的运行状况检查,这些检查可以与给定服务(“ Web服务器返回200 OK”)或与本地节点(“内存利用率低于90%”)相关联。操作员可以使用此信息来监视群集的运行状况,服务发现组件可以使用此信息将流量路由到运行状况不佳的主机之外。
KV商店:应用程序可以将Consul的分层键/值存储用于多种目的,包括动态配置,功能标记,协调,领导者选举等。简单的HTTP API使其易于使用。
安全的服务通信:领事可以为服务生成并分发TLS证书,以建立相互TLS连接。 意图 可用于定义允许哪些服务进行通信。可以使用可以实时更改的意图轻松管理服务分段,而不必使用复杂的网络拓扑和静态防火墙规则。
多数据中心:Consul开箱即用地支持多个数据中心。这意味着Consul的用户不必担心会构建其他抽象层以扩展到多个区域。
将下载的exe文件双击即可安装
在 exe 文件的目录下打开黑窗口
输入 consul --version 检测是否安装成功
启动服务:consul agent -dev
通过 http://localhost:8500/ 访问web界面
改pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
建yml
server: port: 8006spring: application: name: cloud-provider-payment cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}
建启动类
建controller类
改pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
建yml
server: port: 8006spring: application: name: cloud-provider-payment cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}
建启动类建controller
CAP
C:Cosistency(强一致性)
A:Availability(可用性)
P:Partition tolerance(分区容错性)
CAP理论关注粒度是数据,而不是整体系统设计的: AP(Eureka) , CP(Zookeeper/Consul)