springcloud之eureka
springcloud之eureka
*Client-side service discovery* allows services to find and communicate with each other without hard-coding hostname and port.
- client register in eureka server
- send a heartbeat signal to the registry to inform the presence or liveness
Components: a service registry (Eureka Server), a REST service which registers itself at the registry (Eureka Client) and a web application, which is consuming the REST service as a registry-aware client (Spring Cloud Netflix Feign Client).
Eureka HA
server cluster
Eureka can be deployed as a cluster of servers. In case, one of these Eureka servers crash, clients can still connect to the remaining Eureka servers and discover other services.
client cache
Clients retrieve and cache registry information from Eureka server. In case all Eureka servers crash, clients still posses the last healthy snapshot of the registry. This is the default behavior of Eureka clients

cluster
first server
|
Second server
server: |
When Eureka server instances will boot up they will look for each other. All microservices will register with them automatically, so if one goes down the other server instance will be always there. On both Eureka instances you will be able to see all the registered microservices. Like this you can scale-up and have multiple server instances in a production environment
code
Eureka server
- springboot 工程添加spring-cloud-starter-netflix-eureka-server
- @EnableEurekaServer
- 配置
|
server: |
这样就在8761端口setup了一个eureka server
Eureka client
- 添加依赖spring-cloud-starter-netflix-eureka-client
- @EnableDiscoveryClient or @EnableEurekaClient
- 配置
spring: |
FeignClient
discovery-aware Spring RestTemplate using interfaces to communicate with endpoints. This interfaces will be automatically implemented at runtime and instead of service-urls, it is using service-names.
Withou Feign
不使用feign的话,需要注入EurekaClient ,获取服务信息
|
With Feign
@FeignClient(“service-name”)
|
https://www.baeldung.com/spring-cloud-netflix-eureka