GateWay動(dòng)態(tài)路由與負(fù)載均衡詳細(xì)介紹
概述
從之前的配置里面我們可以看到我們的 URL 都是寫死的,這不符合我們微服務(wù)的要求,我們微服務(wù)是只要知道服務(wù)的名字,根據(jù)名字去找,而直接寫死就沒有負(fù)載均衡的效果了 默認(rèn)情況下 Gateway 會(huì)根據(jù)注冊(cè)中心的服務(wù)列表,以注冊(cè)中心上微服務(wù)名為路徑創(chuàng)建動(dòng)態(tài)路 由進(jìn)行轉(zhuǎn)發(fā),從而實(shí)現(xiàn)動(dòng)態(tài)路由的功能。
需要注意的是 uri 的協(xié)議為 lb( load Balance ),表示啟用 Gateway 的負(fù)載均衡功能。
lb://serviceName 是 spring cloud gateway 在微服務(wù)中自動(dòng)為我們創(chuàng)建的負(fù)載均衡 uri
協(xié)議:就是雙方約定的一個(gè)接頭暗號(hào)
http : //
項(xiàng)目實(shí)例
1.gateway-server模塊
1.1.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.3.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.it</groupId> <artifactId>gateway-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR12</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
1.2.application.yml文件
server:
port: 80
spring:
application:
name: gateway-server
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true #開啟動(dòng)態(tài)路由 開啟通過應(yīng)用名稱找到服務(wù)的功能
lower-case-service-id: true #開啟服務(wù)名稱小寫
eureka:
client:
service-url:
defaultZone: http://192.168.174.133:8761/eureka
registry-fetch-interval-seconds: 3
instance:
hostname: localhost
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
1.3.主函數(shù)類
package com.it; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class GatewayServerApplication { public static void main(String[] args) { SpringApplication.run(GatewayServerApplication.class, args); } }
2.login-service模塊
2.1.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.3.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.it</groupId> <artifactId>login-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>login-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR12</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-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
2.2.application.yml文件
server:
port: 8081
spring:
application:
name: login-service
eureka:
client:
service-url:
defaultZone: http://192.168.174.133:8761/eureka
registry-fetch-interval-seconds: 3
instance:
hostname: localhost
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
2.3.LoginController文件
package com.it.controller; import org.springframework.web.bind.annotation.GetMapping; import java.util.UUID; public class LoginController { @GetMapping("doLogin") public String doLogin(String name,String pwd){ System.out.println(name); System.out.println(pwd); String s = UUID.randomUUID().toString(); return s; } }
2.4.主函數(shù)類
package com.it; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class LoginServiceApplication { public static void main(String[] args) { SpringApplication.run(LoginServiceApplication.class, args); } }
3.功能測(cè)試
啟動(dòng)動(dòng)態(tài)路由后,訪問路徑時(shí)需要在localhost后面加上,要訪問服務(wù)的名稱,后面再跟上這個(gè)服務(wù)中的方法接口名
如果不加服務(wù)名稱,還是按照以前的方法寫,會(huì)導(dǎo)致訪問報(bào)404
到此這篇關(guān)于GateWay動(dòng)態(tài)路由與負(fù)載均衡詳細(xì)介紹的文章就介紹到這了,更多相關(guān)GateWay動(dòng)態(tài)路由與負(fù)載均衡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring?Cloud?Gateway動(dòng)態(tài)路由Apollo實(shí)現(xiàn)詳解
- GateWay路由規(guī)則與動(dòng)態(tài)路由詳細(xì)介紹
- SpringCloud Gateway使用redis實(shí)現(xiàn)動(dòng)態(tài)路由的方法
- Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動(dòng)態(tài)路由
- SpringCloud?Gateway詳細(xì)分析實(shí)現(xiàn)負(fù)載均衡與熔斷和限流
- springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡
相關(guān)文章
Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法
本篇文章介紹了,Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法。需要的朋友參考下2013-05-05SpringBoot中攔截器和動(dòng)態(tài)代理的區(qū)別詳解
在?Spring?Boot?中,攔截器和動(dòng)態(tài)代理都是用來實(shí)現(xiàn)功能增強(qiáng)的,所以在很多時(shí)候,有人會(huì)認(rèn)為攔截器的底層是通過動(dòng)態(tài)代理實(shí)現(xiàn)的,所以本文就來盤點(diǎn)一下他們兩的區(qū)別,以及攔截器的底層實(shí)現(xiàn)吧2023-09-09如何解決Spring的UnsatisfiedDependencyException異常問題
這篇文章主要介紹了如何解決Spring的UnsatisfiedDependencyException異常問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04springboot?整合表達(dá)式計(jì)算引擎?Aviator?使用示例詳解
本文詳細(xì)介紹了Google?Aviator?這款高性能、輕量級(jí)的?Java?表達(dá)式求值引擎,并通過詳細(xì)的代碼操作演示了相關(guān)API的使用以及如何在springboot項(xiàng)目中進(jìn)行集成,感興趣的朋友一起看看吧2024-08-08SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息
RocketMQ 是一款開源的分布式消息中間件,由阿里巴巴開源,它具有高可用性、高性能、低延遲等特點(diǎn),廣泛應(yīng)用于阿里巴巴集團(tuán)內(nèi)部以及眾多外部企業(yè)的業(yè)務(wù)系統(tǒng)中,本文給大家介紹了SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息,需要的朋友可以參考下2024-04-04Spring AOP訪問目標(biāo)方法的參數(shù)操作示例
這篇文章主要介紹了Spring AOP訪問目標(biāo)方法的參數(shù)操作,結(jié)合實(shí)例形式詳細(xì)分析了spring面向切面AOP訪問目標(biāo)方法的參數(shù)相關(guān)實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01spring中WebClient如何設(shè)置連接超時(shí)時(shí)間以及讀取超時(shí)時(shí)間
這篇文章主要給大家介紹了關(guān)于spring中WebClient如何設(shè)置連接超時(shí)時(shí)間以及讀取超時(shí)時(shí)間的相關(guān)資料,WebClient是Spring框架5.0引入的基于響應(yīng)式編程模型的HTTP客戶端,它提供一種簡(jiǎn)便的方式來處理HTTP請(qǐng)求和響應(yīng),需要的朋友可以參考下2024-08-08