日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot 微服务集群 + 注册中心

發(fā)布時間:2025/3/19 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot 微服务集群 + 注册中心 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
spring boot 微服務(wù)框架下載地址:

https://start.spring.io/

注冊中心

Eureka Server提供服務(wù)注冊服務(wù),各個節(jié)點啟動后,會在Eureka Server中進(jìn)行注冊,這樣EurekaServer中的服務(wù)注冊表中將會存儲所有可用服務(wù)節(jié)點的信息,服務(wù)節(jié)點的信息可以在界面中直觀的看到。

通過 IP + 端口 (http://localhost:9000)可查看注冊中心已注冊的服務(wù)信息,如下圖:

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent> <groupId>com.example</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-register</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency> </dependencies> <dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build> </project>

application.properties

# 服務(wù)端口 server.port=9000 # 服務(wù)名稱 spring.application.name=eureka-server # 是否向服務(wù)注冊中心注冊自己 eureka.client.register-with-eureka=false # 是否檢索服務(wù) eureka.client.fetch-registry=true # 服務(wù)注冊中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka # 心跳檢測頻率 eureka.instance.lease-renewal-interval-in-seconds=30 # 無響應(yīng)剔除周期 eureka.instance.lease-expiration-duration-in-seconds=90

啟動主類

添加 @EnableEurekaServer注解,將本服務(wù)指定為eureka server 服務(wù)端,用于發(fā)現(xiàn)服務(wù)、注冊服務(wù)信息。

Eureka Client是一個java客戶端,用于簡化與Eureka Server的交互,客戶端同時也就是一個內(nèi)置的、使用輪詢(round-robin)負(fù)載算法的負(fù)載均衡器。

在應(yīng)用啟動后,將會向Eureka Server發(fā)送心跳,默認(rèn)周期為30秒,如果Eureka Server在多個心跳周期內(nèi)沒有接收到某個節(jié)點的心跳,Eureka Server將會從服務(wù)注冊表中把這個服務(wù)節(jié)點移除(默認(rèn)90秒)。

負(fù)載均衡

Spring Cloud Ribbon是一個基于HTTP和TCP的客戶端負(fù)載均衡工具。它基于Netflix Ribbon實現(xiàn),通過Spring Cloud的封裝,可以讓我們輕松地將面向服務(wù)的REST模版請求自動轉(zhuǎn)換成客戶端負(fù)載均衡的服務(wù)調(diào)用。

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>riboon-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-ribbon-server</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency> </dependencies> <dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build> </project>

application.prorperties

# 服務(wù)名稱 spring.application.name=ribbon-server # 服務(wù)端口 server.port=9001 # 注冊中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka
  • 使用 @EnableEurekaClient 注解,將本服務(wù)作為eureka client 并將服務(wù)信息注冊在 eureka server 服務(wù)端。
  • 在啟動時實例化 RestTemplate 對象用于集群服務(wù)調(diào)用,RestTemplate 添加 @LoadBalanced 注解,在集群服務(wù)請求時實現(xiàn)客戶端負(fù)載均衡。
  • 本服務(wù)用于接收外界請求,通過 RestTemplate 調(diào)用集群服務(wù)返回結(jié)果。

    集群服務(wù)訪問地址:http:// 集群服務(wù)應(yīng)用名稱 + 服務(wù)模塊請求路徑

    集群應(yīng)用

    微服務(wù)集群搭建可通過spring boot 配置文件中的 spring.application.name 統(tǒng)一集群下所有應(yīng)用名稱,集群中的每個服務(wù)通過 server.port 配置不同端口。

    pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>client_1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client-server</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency> </dependencies> <dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build> </project>

    application.properties

    # 服務(wù)名稱 spring.application.name=eureka-client # 服務(wù)端口 server.port=9011 # 注冊中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka

    集群中服務(wù)測試代碼,匹配請求路徑并返回調(diào)用結(jié)果,如下圖:

    啟動主類添加 @EnableEurekaClient 注解,將當(dāng)前服務(wù)作為 eureka client 添加至 eureka server 注冊中心管理。

    訪問 Ribbon Server服務(wù)地址: http://localhost:9001/system/getMessage,結(jié)果如下圖:


    多次訪問會發(fā)現(xiàn)每次顯示的端口不同,這是因為 Ribbon 實現(xiàn)了服務(wù)輪詢,真正實現(xiàn)了負(fù)載均衡。

    總結(jié)

    以上是生活随笔為你收集整理的spring boot 微服务集群 + 注册中心的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。