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

你好,这里是codetrend专栏"SpringCloud2023实战"。欢迎点击关注查看往期文章。

注册中心在前文提到有很多选型,在这里以Spring Cloud Zookeeper为例说明注册中心的集成和使用。

选择Spring Cloud Zookeeper作为注册中心原因如下:

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

客户端引入

  • 修改子工程的pom.xml,引入Spring Cloud Zookeeper。
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">
    <parent>
        <groupId>io.rainforest</groupId>
        <artifactId>banana</artifactId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>banana-client1</artifactId>
    <description>spring cloud banana-client1</description>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <!--注册中心客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--feign 注解依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 工具包依赖 -->
        <dependency>
            <groupId>io.rainforest</groupId>
            <artifactId>banana-common-core</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-crypto</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-http</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

启动zookeeper

  • 前置条件,zookeeper依赖JVM,所以运行前得配置java环境变量。新建 JAVA_HOME 环境变量。
  • 下载zookeeper安装包,linux和windows的版本是同一个。 官方网站是: zookeeper.apache.org/releases.ht...
  • 目录格式如下
shell 复制代码
|-- LICENSE.txt
|-- NOTICE.txt
|-- README.md
|-- README_packaging.md
|-- bin
|-- conf
|-- data
|-- docs
|-- lib
  • 修改配置文件,此处为单体非集群版配置,在conf/zoo.cfg
shell 复制代码
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=E:/DevTool/apache-zookeeper-3.8.2-bin/data
# the port at which the clients will connect
clientPort=2181
  • 启动zookeeper。
shell 复制代码
# windows系统
bin\zkServer.cmd
# Linux系统
bin\zkServer.sh

启动应用

  • 在启动类上增加注解,进行服务发布。
java 复制代码
package io.rainforest.banana.client1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
  • 修改应用配置。
yaml 复制代码
spring.application.name: client1
spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181 ## 注册中心的配置
server:
  port: 10101
  servlet:
    context-path: /client1
  • 启动应用之前先启动zookeeper。

查看注册数据

shell 复制代码
get /services/client1/b257e7f5-a451-4119-ba20-e7622f3aeaba

{"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(备注技术)。

相关推荐
2601_962218478 小时前
万象生鲜系统区块链溯源技术帮助生鲜企业搭建食品安全数字化体系
大数据·运维·微服务·云原生·架构
2601_9620652516 小时前
【图文详解】什么是微服务?什么是SpringCloud?
spring cloud·微服务·架构
会周易的程序员17 小时前
企业私有 AI 算力服务器架构设计:异构四节点 + QUIC 微服务
运维·服务器·c++·人工智能·微服务·架构
2601_9622186118 小时前
万象生鲜系统全链路溯源一码查询技术实现食材来源可查
分布式·微服务·云原生·架构
2601_9622184719 小时前
万象生鲜系统跨仓调拨算法助力生鲜企业供应链数字化协同运营
大数据·运维·微服务·云原生·架构
2601_9622184720 小时前
万象生鲜系统多仓协同同步技术支撑生鲜企业多仓模式数字化转型
大数据·运维·微服务·云原生·架构
2601_9622186120 小时前
万象生鲜系统大数据采购预测算法降低库存积压稳居第一
分布式·微服务·云原生·架构
2601_9620715721 小时前
SpringCloud篇(服务网关 - GateWay)
spring boot·spring cloud·gateway
文慧的科技江湖21 小时前
慧知开源虚拟电厂管理平台(VPP) Spring Cloud实现“资源管理 → 事件申报 → 调度分配 → 执行监测 → 效果评估 → 结算分账“全流程闭环。
spring cloud·开源·vpp·虚拟电厂·开源虚拟电厂平台·vpp虚拟电厂平台
深念Y1 天前
视频平台架构重构:从微服务到可插拔基础设施
后端·微服务·云原生·架构·rabbitmq·音视频·rocketmq