I am start busy now that boring for job,I want quiet and only one.
let us go to this diary.
>-------------------------------服务治理,Spring Cloud Eureka
1.服务治理模块主要用于微服务的注册与发现,伴随着服务的增多,配置文件不易于维护需要大力的人力,所以我们需要一个完美的服务治理模块。Service governace is used miroservices that it registers and exposes.Because as servces increase,the static config file is boring and waste many energy ,so we need a perfect service governace.
The service governace is made up of services registration and service expose.
服务治理主要由服务注册和服务发现组成。
Eureka can be clustered.If one of them was died that not influnce the others ,Eureka will enter PROTECTION MODE and the error will be separated until it is normal,then eureka will sync normal states with it.
它可以用于集群部署,当有故障时,它会进入保护模式,隔离错误的分片直到它正常,eurake会同步正常状态给它。
2.Eureka配置和实现
wow,the NBA new season's first game was comed
2.1服务注册中心
在springboot项目中加入eureka依赖,正常的springboot启动类中注入@EnableEurekaServer将会开启springeureka功能。
在默认的设置下,服务注册中心将会作为客户端来注册自己,如果需要禁用,需要在application.properties中配置。
eureka.client.register-with-eureka=false
代表服务注册中心不注册自己
eureka.client.fetch-registry=false
由于注册中心职责就是维护服务实例,它不需要检索服务,所以设置也是false
2.2注册服务提供者
与注册服务中心类似,在启动类加入注解@DiscoveryClient,开始服务发现模式
spring.application.name=hello-service 为服务命名
eureka.client.serviceUrl.defaultZone 指定服务注册中心的地址
3.双注册服务中心,cluster
分别创建不同的application-peer1.properties和application-peer2.properties两个文件,并将eureka.client.serviceUrl.defaultZone分别指向不同的注册中心,此时就形成了两个服务注册中心
设置多节点的服务注册中心以后,服务提供方需要改变配置
eureka.client.serviceUrl.defaultZone需要指向多个中心
>>-------------------------服务发现与消费
服务发现与注册由eureka实现,服务的消费则由ribbon来完成。
ribbon是一个http和tcp的客户端负载均衡器,它可以通过客户端配置的ribbonServiceList服务端列表去轮询访问以达到负载均衡的作用。当ribbon和eureka联合使用时,RibbonServiceList会被DiscoveryEnableNIWSServiceList重写,扩展成从服务注册中心获取服务端列表。同时将服务端是否启动的心跳机制也交给了eureka去管理。
在主类创建@RestTemplate的SpringBean实例,通过@LoadBalanced开启客户端的负载均衡
随后创建服务消费类,需要注入resttemplate实例,进行服务的访问和消费
负载均衡的机制采用轮训的方式,轮训DiscoveryEnableNIWSServiceList维护的服务列表。