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

布满荆棘的人生 2022-05-26 10:57 294阅读 0赞
  1. 之前几篇博客我们已经介绍过使用EurekaConsul作为注册中心的实现及代码示例,这边博客我们学习了解一下Spring Cloud给我们提供的用Zookeeper作为注册中心的实现机制。

Zookeeper

  1. Zookeeper是一个高性能,分布式的,开源分布式应用协调服务。它提供了简单原始的功能,分布式应用可以基于它实现更高级的服务,比如同步,配置管理,集群管理,名空间。它被设计为易于编程,使用文件系统目录树作为数据模型。

1、Zookeeper安装启动

  1. 官网下载:http://zookeeper.apache.org/releases.html\#download
  2. 解压运行:zkServer.cmd

70

服务提供者

  1. 服务提供者将自己的地址信息暴露到Zookeeper中即可,简单来说就是一些IP、端口、实例名等基本信息
  2. 1pom.mxl配置
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.10.RELEASE</version>
  9. </parent>
  10. <dependencyManagement>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-dependencies</artifactId>
  15. <version>Edgware.SR3</version>
  16. <type>pom</type>
  17. <scope>import</scope>
  18. </dependency>
  19. </dependencies>
  20. </dependencyManagement>
  21. <modelVersion>4.0.0</modelVersion>
  22. <groupId>com.tianjunwei</groupId>
  23. <artifactId>TJWspringCloud-zookeeper</artifactId>
  24. <version>1.0.0-SNAPSHOT</version>
  25. <packaging>jar</packaging>
  26. <name>TJWspringCloud-zookeeper</name>
  27. <properties>
  28. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  29. </properties>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>ch.qos.logback</groupId>
  41. <artifactId>logback-core</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>ch.qos.logback</groupId>
  45. <artifactId>logback-classic</artifactId>
  46. </dependency>
  47. <dependency>
  48. <groupId>ch.qos.logback</groupId>
  49. <artifactId>logback-access</artifactId>
  50. </dependency>
  51. </dependencies>
  52. <build>
  53. <resources>
  54. <resource>
  55. <directory>src/main/java</directory>
  56. <includes>
  57. <include>**/*.xml</include>
  58. </includes>
  59. </resource>
  60. <resource>
  61. <directory>src/test/java</directory>
  62. <includes>
  63. <include>**/*.xml</include>
  64. </includes>
  65. </resource>
  66. <resource>
  67. <directory>src/main/resources</directory>
  68. <includes>
  69. <include>**/*.*</include>
  70. </includes>
  71. </resource>
  72. </resources>
  73. <plugins>
  74. <plugin>
  75. <groupId>org.springframework.boot</groupId>
  76. <artifactId>spring-boot-maven-plugin</artifactId>
  77. </plugin>
  78. <plugin>
  79. <groupId>org.apache.maven.plugins</groupId>
  80. <artifactId>maven-compiler-plugin</artifactId>
  81. <version>2.0.2</version>
  82. <configuration>
  83. <source>1.8</source>
  84. <target>1.8</target>
  85. </configuration>
  86. </plugin>
  87. </plugins>
  88. </build>
  89. </project>

2、在application.properties中添加服务提供者自身及Zookeeper的相关配置

  1. server.port=8822
  2. spring.application.name=spring-cloud-zookeeper
  3. spring.cloud.zookeeper.connectString=localhost:2181
  4. spring.cloud.zookeeper.discovery.instanceHost=localhost
  5. spring.cloud.zookeeper.discovery.instancePort=${server.port}

3、服务提供者相关代码:

  1. 服务提供者暴露服务名为spring-cloud-zookeeper的服务。
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @Configuration
  9. @EnableAutoConfiguration
  10. @EnableDiscoveryClient
  11. @RestController
  12. public class ZookeeperMain {
  13. @RequestMapping("/hi")
  14. public String home() {
  15. return "Hello zookeeper";
  16. }
  17. public static void main(String[] args) {
  18. SpringApplication.run(ZookeeperMain.class, args);
  19. }
  20. }

服务消费者

  1. 服务消费者同样注册到Zookeeper中,从zk中获取到所需要的服务信息。

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-zookeeper-client</artifactId>
  22. <version>1.0.0-SNAPSHOT</version>
  23. <packaging>jar</packaging>
  24. <name>TJWspringCloud-zookeeper</name>
  25. <properties>
  26. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  27. </properties>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-web</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>ch.qos.logback</groupId>
  39. <artifactId>logback-core</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>ch.qos.logback</groupId>
  43. <artifactId>logback-classic</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>ch.qos.logback</groupId>
  47. <artifactId>logback-access</artifactId>
  48. </dependency>
  49. </dependencies>
  50. <build>
  51. <resources>
  52. <resource>
  53. <directory>src/main/java</directory>
  54. <includes>
  55. <include>**/*.xml</include>
  56. </includes>
  57. </resource>
  58. <resource>
  59. <directory>src/test/java</directory>
  60. <includes>
  61. <include>**/*.xml</include>
  62. </includes>
  63. </resource>
  64. <resource>
  65. <directory>src/main/resources</directory>
  66. <includes>
  67. <include>**/*.*</include>
  68. </includes>
  69. </resource>
  70. </resources>
  71. <plugins>
  72. <plugin>
  73. <groupId>org.springframework.boot</groupId>
  74. <artifactId>spring-boot-maven-plugin</artifactId>
  75. </plugin>
  76. <plugin>
  77. <groupId>org.apache.maven.plugins</groupId>
  78. <artifactId>maven-compiler-plugin</artifactId>
  79. <version>2.0.2</version>
  80. <configuration>
  81. <source>1.8</source>
  82. <target>1.8</target>
  83. </configuration>
  84. </plugin>
  85. </plugins>
  86. </build>
  87. </project>

2、application.properties中添加自身及zk相关的配置

  1. server.port=8833
  2. spring.application.name=spring-cloud-zookeeper-client
  3. spring.cloud.zookeeper.connectString=localhost:2181
  4. spring.cloud.zookeeper.discovery.instanceHost=localhost
  5. spring.cloud.zookeeper.discovery.instancePort=${server.port}

3、服务调用

  1. 在服务消费者通过服务名称来找到对应调用服务提供者的信息。
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.cloud.client.discovery.DiscoveryClient;
  6. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  7. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  8. import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestMethod;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import org.springframework.web.client.RestTemplate;
  15. @Configuration
  16. @EnableAutoConfiguration
  17. @EnableDiscoveryClient
  18. @RestController
  19. public class ZookeeperClientMain {
  20. @Autowired
  21. RestTemplate restTemplate;
  22. @Bean
  23. @LoadBalanced
  24. public RestTemplate restTemplate() {
  25. return new RestTemplate();
  26. }
  27. @RequestMapping(value = "/hi", method = RequestMethod.GET)
  28. public String add() {
  29. return restTemplate.getForEntity("http://spring-cloud-zookeeper/hi", String.class).getBody();
  30. }
  31. public static void main(String[] args) {
  32. SpringApplication.run(ZookeeperClientMain.class, args);
  33. }
  34. }

服务调用结果:

70 1

总结:总体来说使用zk作为注册中心与其他注册中心(consul和eureka)类似,对于开发人员来说几乎可以不用在意注册中心到底是哪种类型,因为在服务进行调用的代码都是一致的。

发表评论

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

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

相关阅读