1.什么是注冊中心
注冊中心主要有三種角色:
-
服務提供者(RPC Server):在啟動時,向 Registry 注冊自身服務,并向 Registry 定期發送心跳匯報存活狀態。
-
服務消費者(RPC Client):在啟動時,向 Registry 訂閱服務,把 Registry 返回的服務節點列表緩存在本地內存中,并與 RPC Sever 建立連接。
-
服務注冊中心(Registry):用于保存 RPC Server 的注冊信息,當 RPC Server 節點發生變更時,Registry 會同步變更,RPC Client 感知后會刷新本地 內存中緩存的服務節點列表。
2.框架版本
spring boot:2.7.13
spring cloud:2021.0.1
3.xxx-discovery-etcd
支持etcd作為服務的注冊中心,在微服務中使用
3.1.使用
pom.xml中引入依賴
<dependency><groupId>x.xx.xxx</groupId><artifactId>xxx-discovery-etcd</artifactId> </dependency>
application.yml中配置
spring:application:name: etcd-provider-examplexxx:discovery:etcd:server-addr: http://192.168.184.133:2379instance-name: provider1 ? ? ? ? ? ? ? ?
啟動主類增加注解@EnableDiscoveryClient
4.Spring Cloud和xxx-etcd-discovery的結合
4.1.etcd-provider-demo
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion> ?<groupId>org.example</groupId><artifactId>etcd-provider-demo</artifactId><version>1.0-SNAPSHOT</version> ?<dependencies><dependency><groupId>x.xx.xxx</groupId><artifactId>xxx-discovery-etcd</artifactId></dependency> ?<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> ?<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency> ?</dependencies> ? </project>
bootstrap.yml
server:port: 18082 spring:application:name: etcd-provider-example ? xxx:discovery:etcd:server-addr: http://192.168.184.133:2379instance-name: provider1 ? logging:level:root: DEBUGio:grpc:netty:NettyClientHandler: INFO ?org:springframework:boot:autoconfigure: INFO
EtcdProviderExampleApplication
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; ? @SpringBootApplication @EnableDiscoveryClient public class EtcdProviderExampleApplication { ?public static void main(String[] args) {SpringApplication.run(EtcdProviderExampleApplication.class, args);} }
EchoController
package x.xx.xxx.etcd.demo; ? import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; ? @RestController public class EchoController {@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)public String echo(@PathVariable String string) {return System.currentTimeMillis() + "--Hello Etcd Discovery " + string;} }
4.2.etcd-consumer-demo
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion> ?<groupId>org.example</groupId><artifactId>etcd-consumer-demo</artifactId><version>1.0-SNAPSHOT</version> ?<dependencies><dependency><groupId>x.xx.xxx</groupId><artifactId>xxx-discovery-etcd</artifactId></dependency> ?<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> ?<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency> ?<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency> ?</dependencies> ? </project>
bootstrap.yml
server:port: 18081 ? spring:application:name: etcd-consumer-example ? xxx:discovery:etcd:server-addr: http://192.168.184.133:2379instance-name: consumer1logging:level:root: DEBUGio:grpc:netty:NettyClientHandler: INFO ?org:springframework:boot:autoconfigure: INFO
EtcdConsumerExampleApplication
package x.xx.xxx.etcd.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient;@SpringBootApplication @EnableDiscoveryClient public class EtcdConsumerExampleApplication {public static void main(String[] args) {SpringApplication.run(EtcdConsumerExampleApplication.class, args);}@LoadBalanced@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}@Bean@LoadBalancedpublic WebClient.Builder loadBalancedWebClientBuilder() {return WebClient.builder();} }
TestController
package x.xx.xxx.etcd.demo;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient;@RestController public class TestController {private final RestTemplate restTemplate;@Autowiredpublic TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)public String echo(@PathVariable String str) {return restTemplate.getForObject("http://etcd-provider-example/echo/" + str, String.class);}@Autowiredprivate WebClient.Builder webClientBuilder;@RequestMapping(value = "/echo/webClient/{str}", method = RequestMethod.GET)public String echoWebClient(@PathVariable String str) {WebClient webClient = webClientBuilder.build();webClient.get().uri("http://etcd-provider-example/echo/" + str).retrieve().bodyToMono(String.class).subscribe(response -> {System.err.println("控制臺響應結果:" + response);});return System.currentTimeMillis() + "--echoWebClient--" + str + "--請查看控制臺是否執行成功!";}}
4.3.啟動主程序
EtcdProviderExampleApplication
EtcdConsumerExampleApplication
通過ETCD Manager
查看,如下表示注冊成功
請求etcd-consumer-demo
中接口,查看服務發現功能,更改測試接口,可查看TestController
http://localhost:18081/echo/6666
瀏覽器返回如下信息表示服務發現成功
1705978409084--Hello Etcd Discovery 6666