Spring Cloud学习--Spring Cloud Consul服务注册与发现

妖狐艹你老母 2022-05-26 10:51 303阅读 0赞
  1. 之前我们已经在几篇博客中介绍了以Eureka为注册中心服务注册与发现机制,接下来我们学习了解一下以Consul为注册中心的服务注册与发现。

Consul简介

  1. Consul HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式”,内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。使用起来也较 为简单。Consul使用Go语言编写,因此具有天然可移植性(支持LinuxwindowsMac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合
  2. 1Consul 下载地址:[https://www.consul.io/downloads.html][https_www.consul.io_downloads.html]
  3. 2、执行如下命令启动Consulconsul agent -dev

70

  1. 3、在浏览器中打开consul提供的管理页面

70 1

Consul 服务提供者

  1. 服务提供者将服务信息注册到注册中心consul,在工程的application.yml中需要添加consul相关的地址信息

1、pom.xml引入consul相关jar

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>1.5.10.RELEASE</version>
  7. </parent>
  8. <dependencyManagement>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-dependencies</artifactId>
  13. <version>Edgware.SR3</version>
  14. <type>pom</type>
  15. <scope>import</scope>
  16. </dependency>
  17. </dependencies>
  18. </dependencyManagement>
  19. <modelVersion>4.0.0</modelVersion>
  20. <groupId>com.tianjunwei</groupId>
  21. <artifactId>TJWspringCloud-consul</artifactId>
  22. <version>1.0.0-SNAPSHOT</version>
  23. <packaging>jar</packaging>
  24. <name>TJWspringCloud-consul</name>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-web</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>ch.qos.logback</groupId>
  36. <artifactId>logback-core</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>ch.qos.logback</groupId>
  40. <artifactId>logback-classic</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>ch.qos.logback</groupId>
  44. <artifactId>logback-access</artifactId>
  45. </dependency>
  46. </dependencies>
  47. <build>
  48. <resources>
  49. <resource>
  50. <directory>src/main/java</directory>
  51. <includes>
  52. <include>**/*.xml</include>
  53. </includes>
  54. </resource>
  55. <resource>
  56. <directory>src/test/java</directory>
  57. <includes>
  58. <include>**/*.xml</include>
  59. </includes>
  60. </resource>
  61. <resource>
  62. <directory>src/main/resources</directory>
  63. <includes>
  64. <include>**/*.*</include>
  65. </includes>
  66. </resource>
  67. </resources>
  68. <plugins>
  69. <plugin>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-maven-plugin</artifactId>
  72. </plugin>
  73. <plugin>
  74. <groupId>org.apache.maven.plugins</groupId>
  75. <artifactId>maven-compiler-plugin</artifactId>
  76. <version>2.0.2</version>
  77. <configuration>
  78. <source>1.8</source>
  79. <target>1.8</target>
  80. </configuration>
  81. </plugin>
  82. </plugins>
  83. </build>
  84. </project>

2、application.yml中添加相关配置

  1. spring:
  2. cloud:
  3. consul:
  4. #consul地址
  5. host: localhost
  6. #consul 对外提供服务的端口
  7. port: 8500
  8. discovery:
  9. #服务提供者对外提供的健康检查接口
  10. healthCheckPath: /health
  11. healthCheckInterval: 15s
  12. #服务实例ID
  13. instance-id: consul-tjw
  14. #应用名称
  15. application:
  16. name: consul-tjw
  17. #对外提供服务的端口
  18. server:
  19. port: 8502

3、添加服务启动代码,并对外提供服务

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @SpringBootApplication
  7. @EnableDiscoveryClient
  8. @RestController
  9. public class ConsumerApplication {
  10. @RequestMapping("/hi")
  11. public String home() {
  12. return "hi ,i'm tjw";
  13. }
  14. /*
  15. * 自检
  16. */
  17. @RequestMapping("/health")
  18. public String health() {
  19. return "health";
  20. }
  21. public static void main(String[] args) {
  22. SpringApplication.run(ConsumerApplication.class, args);
  23. }
  24. }

4、启动服务提供者之后会在consul的管理页面中看到consul-tjw相关的服务信息

70 2

Consul 服务消费者

  1. Consul的服务消费者不需要注册到consul(当然也可以注册到consul中),其只需要在使用服务时通过Consul对外提供的接口获取服务信息即可(应该与Eureka类似)

1、pom.xml中配置

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>1.5.10.RELEASE</version>
  7. </parent>
  8. <dependencyManagement>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-dependencies</artifactId>
  13. <version>Edgware.SR3</version>
  14. <type>pom</type>
  15. <scope>import</scope>
  16. </dependency>
  17. </dependencies>
  18. </dependencyManagement>
  19. <modelVersion>4.0.0</modelVersion>
  20. <groupId>com.tianjunwei</groupId>
  21. <artifactId>TJWspringCloud-consul-client</artifactId>
  22. <version>1.0.0-SNAPSHOT</version>
  23. <packaging>jar</packaging>
  24. <name>TJWspringCloud-consul-client</name>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>ch.qos.logback</groupId>
  32. <artifactId>logback-core</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>ch.qos.logback</groupId>
  36. <artifactId>logback-classic</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>ch.qos.logback</groupId>
  40. <artifactId>logback-access</artifactId>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <resources>
  45. <resource>
  46. <directory>src/main/java</directory>
  47. <includes>
  48. <include>**/*.xml</include>
  49. </includes>
  50. </resource>
  51. <resource>
  52. <directory>src/test/java</directory>
  53. <includes>
  54. <include>**/*.xml</include>
  55. </includes>
  56. </resource>
  57. <resource>
  58. <directory>src/main/resources</directory>
  59. <includes>
  60. <include>**/*.*</include>
  61. </includes>
  62. </resource>
  63. </resources>
  64. <plugins>
  65. <plugin>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-maven-plugin</artifactId>
  68. </plugin>
  69. <plugin>
  70. <groupId>org.apache.maven.plugins</groupId>
  71. <artifactId>maven-compiler-plugin</artifactId>
  72. <version>2.0.2</version>
  73. <configuration>
  74. <source>1.8</source>
  75. <target>1.8</target>
  76. </configuration>
  77. </plugin>
  78. </plugins>
  79. </build>
  80. </project>

2、application.properties需要配置consul注册中心的地址,并且配置不需要注册到注册中心即可

  1. server.port=8503
  2. spring.application.name=Consul-Client
  3. spring.cloud.consul.host=127.0.0.1
  4. spring.cloud.consul.port=8500
  5. #设置不需要注册到consul中
  6. spring.cloud.consul.discovery.register=false

3、服务消费者可以根据服务名远程调用服务提供者提供的接口

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.DiscoveryClient;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  7. import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import org.springframework.web.client.RestTemplate;
  13. @SpringBootApplication
  14. @EnableDiscoveryClient
  15. @RestController
  16. public class ConsulMainClient {
  17. @Autowired
  18. RestTemplate restTemplate;
  19. @Autowired(required=true)
  20. private LoadBalancerClient loadBalancer;
  21. @Autowired(required=true)
  22. private DiscoveryClient discoveryClient;
  23. /**
  24. * 从所有服务中选择一个服务(轮询)
  25. */
  26. @RequestMapping("/discover")
  27. public Object discover() {
  28. return loadBalancer.choose("consul-tjw").getUri().toString();
  29. }
  30. @Bean
  31. @LoadBalanced
  32. public RestTemplate restTemplate() {
  33. return new RestTemplate();
  34. }
  35. @RequestMapping(value = "/hi", method = RequestMethod.GET)
  36. public String add() {
  37. return restTemplate.getForEntity("http://consul-tjw/hi", String.class).getBody();
  38. }
  39. /**
  40. * 获取所有服务
  41. */
  42. @RequestMapping("/services")
  43. public Object services() {
  44. return discoveryClient.getInstances("consul-tjw");
  45. }
  46. public static void main( String[] args ) {
  47. SpringApplication.run(ConsulMainClient.class, args);
  48. }
  49. }

通过调用http://localhost:8503/hi即可获取到服务提供者提供的数据信息

70 3

通过调用http://localhost:8503/services可以看到consul中提供的相关的服务的信息,存储的信息很简单类似Eureka注册中心

70 4

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读