如何在SpringCloud2023中快速集成注册中央

一给  金牌会员 | 2024-5-14 20:32:23 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 864|帖子 864|积分 2592

你好,这里是codetrend专栏“SpringCloud2023实战”。欢迎点击关注检察往期文章。
注册中央在前文提到有很多选型,在这里以Spring Cloud Zookeeper为例说明注册中央的集成和使用。
选择Spring Cloud Zookeeper作为注册中央原因如下:

  • 依靠更少,只依靠zookeeper单体或集群的部署。
  • 配置更通用,Eureka和zookeeper的切换只需要少量配置切换即可完成。
  • 集成方便,注解通用,集成starter即可。
客户端引入


  • 修改子工程的pom.xml,引入Spring Cloud Zookeeper。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          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>io.rainforest</groupId>
  7.         <artifactId>banana</artifactId>
  8.         <version>1.0</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>banana-client1</artifactId>
  12.     <description>spring cloud banana-client1</description>
  13.     <packaging>jar</packaging>
  14.     <dependencies>
  15.         <dependency>
  16.             <groupId>org.springframework.boot</groupId>
  17.             <artifactId>spring-boot-starter-web</artifactId>
  18.         </dependency>
  19.         <dependency>
  20.             <groupId>org.springframework.boot</groupId>
  21.             <artifactId>spring-boot-starter-undertow</artifactId>
  22.         </dependency>
  23.         
  24.         <dependency>
  25.             <groupId>org.springframework.cloud</groupId>
  26.             <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  27.         </dependency>
  28.         <dependency>
  29.             <groupId>org.springframework.cloud</groupId>
  30.             <artifactId>spring-cloud-starter-openfeign</artifactId>
  31.         </dependency>
  32.         
  33.         <dependency>
  34.             <groupId>org.springframework.cloud</groupId>
  35.             <artifactId>spring-cloud-openfeign-core</artifactId>
  36.             <optional>true</optional>
  37.         </dependency>
  38.         
  39.         <dependency>
  40.             <groupId>io.rainforest</groupId>
  41.             <artifactId>banana-common-core</artifactId>
  42.         </dependency>
  43.         <dependency>
  44.             <groupId>cn.hutool</groupId>
  45.             <artifactId>hutool-crypto</artifactId>
  46.         </dependency>
  47.         <dependency>
  48.             <groupId>cn.hutool</groupId>
  49.             <artifactId>hutool-http</artifactId>
  50.         </dependency>
  51.         <dependency>
  52.             <groupId>com.github.xiaoymin</groupId>
  53.             <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
  54.         </dependency>
  55.     </dependencies>
  56.     <build>
  57.         <plugins>
  58.             <plugin>
  59.                 <groupId>org.springframework.boot</groupId>
  60.                 <artifactId>spring-boot-maven-plugin</artifactId>
  61.             </plugin>
  62.         </plugins>
  63.     </build>
  64. </project>
复制代码
启动zookeeper


  • 前置条件,zookeeper依靠JVM,所以运行前得配置java情况变量。新建 JAVA_HOME 情况变量。
  • 下载zookeeper安装包,linux和windows的版本是同一个。 官方网站是: https://zookeeper.apache.org/releases.html
  • 目录格式如下
  1. |-- LICENSE.txt
  2. |-- NOTICE.txt
  3. |-- README.md
  4. |-- README_packaging.md
  5. |-- bin
  6. |-- conf
  7. |-- data
  8. |-- docs
  9. |-- lib
复制代码

  • 修改配置文件,此处为单体非集群版配置,在conf/zoo.cfg。
  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=E:/DevTool/apache-zookeeper-3.8.2-bin/data
  13. # the port at which the clients will connect
  14. clientPort=2181
复制代码

  • 启动zookeeper。
  1. # windows系统
  2. bin\zkServer.cmd
  3. # Linux系统
  4. bin\zkServer.sh
复制代码
启动应用


  • 在启动类上增加注解,进行服务发布。
  1. package io.rainforest.banana.client1;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @SpringBootApplication
  7. @EnableDiscoveryClient
  8. @EnableFeignClients
  9. public class Application {
  10.         public static void main(String[] args) {
  11.                 SpringApplication.run(Application.class, args);
  12.         }
  13. }
复制代码

  • 修改应用配置。
  1. spring.application.name: client1
  2. spring:
  3.   cloud:
  4.     zookeeper:
  5.       connect-string: localhost:2181 ## 注册中心的配置
  6. server:
  7.   port: 10101
  8.   servlet:
  9.     context-path: /client1
复制代码

  • 启动应用之前先启动zookeeper。
检察注册数据
  1. get /services/client1/b257e7f5-a451-4119-ba20-e7622f3aeaba
  2. {"name":"client1","id":"b257e7f5-a451-4119-ba20-e7622f3aeaba","address":"cat","port":10101,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"client1","name":"client1","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1698715221404,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
复制代码
默认的zookeeper注册中央的数据放在 /services/客户端名称/实例名称 。
源码信息

和“SpringCloud实战”对应的源码信息如下:
关于作者

来自一线全栈步调员nine的八年探索与实践,连续迭代中。欢迎关注公众号“雨林寻北”或添加个人卫星codetrend(备注技术)。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

一给

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表