服务注册与发现
Eureka
作用
能够自动注册并发现微服务,然后对服务的状态、信息进行集中管理,这样当我们需要获取其他服务的信息时,我们只需要向 Eureka 进行查询就可以了
这样一来,服务之间的强关联性就会被进一步削弱
添加依赖
服务端
1 2 3 4 5
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>4.0.0</version> </dependency>
|
客户端
1 2 3 4 5
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>4.0.0</version> </dependency>
|
配置
服务端
1 2 3 4 5 6 7 8 9
| eureka: client: fetch-registry: false register-with-eureka: false service-url: defaultZone: http://localhost:8010/eurela
|
客户端
1 2 3 4 5 6 7 8
| spring: application: name: xxxService eureka: client: service-url: defaultZone: http://localhost:8010/eureka
|
高可用
开启多个服务端
1 2 3 4 5 6 7 8 9 10 11
| eureka: client: fetch-registry: false register-with-eureka: false service-url: defaultZone: http://eureka02:8020/eureka instance: hostname: eureka01
|
客户端向多个eureka注册
1 2 3 4
| eureka: client: service-url: defaultZone: http://eureka01:8010/eureka, http://eureka02:8020/eureka
|