springboot整合gateway實(shí)現(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 針對進(jìn)來的請求做各種判斷和處理,比如說判斷請求的合法性、權(quán)限驗(yàn)證,請求地址改寫,請求參數(shù)、頭信息、cookie 信息的分析和改寫,請求速率控制,日志留存等。而這些都可以方便的通過 Predicate 和 GatewayFilter 來組合實(shí)現(xiàn)。
2.代碼實(shí)現(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> ? ? ? ? <!--客戶端負(fù)載均衡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.實(shí)現(xiàn)效果
采用nacos作為注冊中心,啟動nacos后再啟動gateway-service, gateway-client項(xiàng)目
在nacos發(fā)現(xiàn)服務(wù)注冊成功
在瀏覽器發(fā)起請求
? ?http://localhost:8001/client/index??
實(shí)際上網(wǎng)關(guān)把請求發(fā)送到gateway-client服務(wù),返回結(jié)果
到此這篇關(guān)于springboot整合gateway實(shí)現(xiàn)網(wǎng)關(guān)功能的示例代碼的文章就介紹到這了,更多相關(guān)springboot gateway網(wǎng)關(guān) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java swing實(shí)現(xiàn)酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java swing實(shí)現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02java實(shí)現(xiàn)輸出文件夾下某個(gè)格式的所有文件實(shí)例代碼
這篇文章主要介紹了java實(shí)現(xiàn)輸出文件夾下某個(gè)格式的所有文件,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06MyBatis入門之增刪改查+數(shù)據(jù)庫字段和實(shí)體字段不一致問題處理方法
這篇文章主要介紹了MyBatis入門之增刪改查+數(shù)據(jù)庫字段和實(shí)體字段不一致問題處理方法,需要的朋友可以參考下2017-05-05Python單元測試_使用裝飾器實(shí)現(xiàn)測試跳過和預(yù)期故障的方法
下面小編就為大家?guī)硪黄狿ython單元測試_使用裝飾器實(shí)現(xiàn)測試跳過和預(yù)期故障的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Java SpringBoot模板引擎之 Thymeleaf入門詳解
jsp有著強(qiáng)大的功能,能查出一些數(shù)據(jù)轉(zhuǎn)發(fā)到JSP頁面以后,我們可以用jsp輕松實(shí)現(xiàn)數(shù)據(jù)的顯示及交互等,包括能寫Java代碼。但是,SpringBoot首先是以jar的方式,不是war;其次我們的tomcat是嵌入式的,所以現(xiàn)在默認(rèn)不支持jsp2021-10-10spring boot 集成 shiro 自定義密碼驗(yàn)證 自定義freemarker標(biāo)簽根據(jù)權(quán)限渲染不同頁面(推薦
這篇文章主要介紹了spring-boot 集成 shiro 自定義密碼驗(yàn)證 自定義freemarker標(biāo)簽根據(jù)權(quán)限渲染不同頁面,需要的朋友可以參考下2018-12-12RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法講解
這篇文章主要介紹了RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12SpringBoot 使用 OpenAPI3 規(guī)范整合 knife4j的詳細(xì)過程
Swagger工具集使用OpenAPI規(guī)范,可以生成、展示和測試基于OpenAPI規(guī)范的API文檔,并提供了生成客戶端代碼的功能,本文給大家介紹SpringBoot使用OpenAPI3規(guī)范整合knife4j的詳細(xì)過程,感興趣的朋友跟隨小編一起看看吧2023-12-12IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼
這篇文章主要介紹了IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03