如何在SpringCloud2023中快速整合註冊中心

落叶微风發表於2024-03-20

你好,這裡是codetrend專欄“SpringCloud2023實戰”。歡迎點選關注檢視往期文章。

註冊中心在前文提到有很多選型,在這裡以Spring Cloud Zookeeper為例說明註冊中心的整合和使用。

選擇Spring Cloud Zookeeper作為註冊中心原因如下:

  • 依賴更少,只依賴zookeeper單體或叢集的部署。
  • 配置更通用,Eureka和zookeeper的切換隻需要少量配置切換即可完成。
  • 整合方便,註解通用,整合starter即可。

客戶端引入

  • 修改子工程的pom.xml,引入Spring Cloud Zookeeper。
<?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的版本是同一個。 官方網站是: https://zookeeper.apache.org/releases.html
  • 目錄格式如下
|-- LICENSE.txt
|-- NOTICE.txt
|-- README.md
|-- README_packaging.md
|-- bin
|-- conf
|-- data
|-- docs
|-- lib
  • 修改配置檔案,此處為單體非叢集版配置,在conf/zoo.cfg
# 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。
# windows系統
bin\zkServer.cmd
# Linux系統
bin\zkServer.sh

啟動應用

  • 在啟動類上增加註解,進行服務釋出。
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);
	}
}
  • 修改應用配置。
spring.application.name: client1
spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181 ## 註冊中心的配置
server:
  port: 10101
  servlet:
    context-path: /client1
  • 啟動應用之前先啟動zookeeper。

檢視註冊資料

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實戰”對應的原始碼資訊如下:

  • github https://github.com/r0ad/spring-cloud-example
  • gitee https://gitee.com/r0ad/spring-cloud-example

關於作者

來自一線全棧程式設計師nine的八年探索與實踐,持續迭代中。歡迎關注公眾號“雨林尋北”或新增個人衛星codetrend(備註技術)。

相關文章