欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合gateway實(shí)現(xiàn)網(wǎng)關(guān)功能的示例代碼

 更新時(shí)間:2022年02月08日 15:06:13   作者:灰太狼_cxh  
本文主要介紹了springboot整合gateway實(shí)現(xiàn)網(wǎng)關(guān)功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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)文章

最新評論