springboot整合gateway實現(xiàn)網(wǎng)關(guān)功能的示例代碼
1.使用場景:
網(wǎng)關(guān)可提供請求路由與組合、協(xié)議轉(zhuǎn)換、安全認(rèn)證、服務(wù)鑒權(quán)、流量控制與日志監(jiān)控等服務(wù)。可選的網(wǎng)關(guān)有不少,比如 Nginx、、Linkerd 、eureka、 Spring Cloud Gateway、consul等。
Spring Cloud Gateway 針對進來的請求做各種判斷和處理,比如說判斷請求的合法性、權(quán)限驗證,請求地址改寫,請求參數(shù)、頭信息、cookie 信息的分析和改寫,請求速率控制,日志留存等。而這些都可以方便的通過 Predicate 和 GatewayFilter 來組合實現(xiàn)。
2.代碼實現(xiàn)
1創(chuàng)建gateway-service服務(wù)
引入依賴
<dependency> ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-gateway</artifactId> ? ? ? ? ? ? <version>3.0.4</version> ? ? ? ? </dependency> ? ? ? ? <!--服務(wù)注冊/發(fā)現(xiàn)中心依賴--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.alibaba.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> ? ? ? ? </dependency> ? ? ? ? <!--服務(wù)的配置中心依賴--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.alibaba.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> ? ? ? ? </dependency> ? ? ? ? <!--fegin組件--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-openfeign</artifactId> ? ? ? ? ? ? <version>3.0.2</version> ? ? ? ? </dependency> ? ? ? ? <!-- Feign Client for loadBalancing --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-loadbalancer</artifactId> ? ? ? ? ? ? <version>3.0.2</version> ? ? ? ? </dependency> ? ? ? ? <!--客戶端負載均衡loadbalancer--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-loadbalancer</artifactId> ? ? ? ? </dependency>
yml配置
server: ? port: 8001 spring: ? application: ? ? name: gateway-service #服務(wù)名 ? profiles: ? ? active: dev #環(huán)境設(shè)置 ? cloud: ? ? gateway: ? ? ? routes: ? ? ? ? # 透傳服務(wù) ? ? ? ? - id: gateway-client #設(shè)置路由id(理論上是可以隨便寫的) ? ? ? ? ? uri: lb://gateway-client ?#設(shè)置路由的url lb://nacos服務(wù)注冊名稱 ? ? ? ? ? predicates: ? ? ? ? ? ? - Path=/client/** #路徑匹配規(guī)則 ? ? ? ? ? filters: ? ? ? ? ? ? - StripPrefix=1 ? ? ? ? - id: gateway-consumer ? ? ? ? ? uri: lb://gateway-consumer ? ? ? ? ? predicates: ? ? ? ? ? ? - Path=/consumer/** ? ? ? ? ? filters: ? ? ? ? ? ? - StripPrefix=1
跨域配置
@Configuration
public class CorsConfig {
? ? @Bean
? ? public CorsWebFilter corsFilter() {
? ? ? ? CorsConfiguration config = new CorsConfiguration();
? ? ? ? config.addAllowedMethod("*");
? ? ? ? config.addAllowedOrigin("*");
? ? ? ? config.addAllowedHeader("*");
? ? ? ? UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
? ? ? ? source.registerCorsConfiguration("/**", config);
? ? ? ? return new CorsWebFilter(source);
? ? }
}2創(chuàng)建gateway-client服務(wù)
引入依賴
<dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId> ? ? ? ? ? ? <scope>test</scope> ? ? ? ? </dependency> ? ? ? ? <!--服務(wù)注冊--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> ? ? ? ? ? ? <version>0.2.1.RELEASE</version> ? ? ? ? </dependency> ? ? ? ? <!--服務(wù)調(diào)用--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-openfeign</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? </dependency>
yml配置
server: ? port: 8002 spring: ? application: ? ? name: gateway-client #服務(wù)名 ? profiles: ? ? active: dev #環(huán)境設(shè)置 ? cloud: ? ? nacos: ? ? ? discovery: ? ? ? ? server-addr: 127.0.0.1:8848 #nacos服務(wù)注冊
控制層請求
@RestController
public class TestController {
? ? @RequestMapping("/index")
? ? public String index(){
? ? ? ? return "gateway-client";
? ? }
}啟動類
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayClientApplication {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(GatewayClientApplication.class, args);
? ? }
}3.實現(xiàn)效果
采用nacos作為注冊中心,啟動nacos后再啟動gateway-service, gateway-client項目
在nacos發(fā)現(xiàn)服務(wù)注冊成功

在瀏覽器發(fā)起請求
? ?http://localhost:8001/client/index??
實際上網(wǎng)關(guān)把請求發(fā)送到gateway-client服務(wù),返回結(jié)果

到此這篇關(guān)于springboot整合gateway實現(xiàn)網(wǎng)關(guān)功能的示例代碼的文章就介紹到這了,更多相關(guān)springboot gateway網(wǎng)關(guān) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?file.delete刪除文件失敗,Windows磁盤出現(xiàn)無法訪問的文件問題
這篇文章主要介紹了Java?file.delete刪除文件失敗,Windows磁盤出現(xiàn)無法訪問的文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Java中Redis存儲String類型會有亂碼的問題及解決方案
在java中使用Redis存儲String類型的數(shù)據(jù)時,會出現(xiàn)亂碼,我寫了一條存儲key為name,值為虎哥的字符串,然后獲取一下這個key為name的值,打印得到的值,下面通過實例代碼介紹Java中Redis存儲String類型會有亂碼的問題及解決方案,一起看看吧2024-04-04
springboot的類加載器(org.springframework.boot.loader)過程詳解
這篇文章主要介紹了springboot的類加載器(org.springframework.boot.loader),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Java CompletableFuture如何實現(xiàn)超時功能
這篇文章主要為大家介紹了實現(xiàn)超時功能的基本思路以及CompletableFuture(之后簡稱CF)是如何通過代碼實現(xiàn)超時功能的,需要的小伙伴可以了解下2025-01-01
Quarkus集成open api接口使用swagger ui展示
這篇文章主要為大家介紹了Quarkus集成open?api接口使用swagger?ui的展示示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02
Java求s=a+aa+aaa+aaaa+aa...a 5個數(shù)相加的值
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制2017-02-02

