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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

https://start.spring.io/

注冊中心

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

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

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

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

啟動主類

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

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

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

負載均衡

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

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

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

    集群服務訪問地址:http:// 集群服務應用名稱 + 服務模塊請求路徑

    集群應用

    微服務集群搭建可通過spring boot 配置文件中的 spring.application.name 統(tǒng)一集群下所有應用名稱,集群中的每個服務通過 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

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

    集群中服務測試代碼,匹配請求路徑并返回調用結果,如下圖:

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

    訪問 Ribbon Server服務地址: http://localhost:9001/system/getMessage,結果如下圖:


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

    總結

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。