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

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

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

1.使用場景:

網(wǎng)關可提供請求路由與組合、協(xié)議轉(zhuǎn)換、安全認證、服務鑒權(quán)、流量控制與日志監(jiān)控等服務??蛇x的網(wǎng)關有不少,比如 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服務

引入依賴

<dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-gateway</artifactId>
? ? ? ? ? ? <version>3.0.4</version>
? ? ? ? </dependency>
? ? ? ? <!--服務注冊/發(fā)現(xiàn)中心依賴-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
? ? ? ? </dependency>

? ? ? ? <!--服務的配置中心依賴-->
? ? ? ? <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 #服務名
? profiles:
? ? active: dev #環(huán)境設置
? cloud:
? ? gateway:
? ? ? routes:
? ? ? ? # 透傳服務
? ? ? ? - id: gateway-client #設置路由id(理論上是可以隨便寫的)
? ? ? ? ? uri: lb://gateway-client ?#設置路由的url lb://nacos服務注冊名稱
? ? ? ? ? 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服務

引入依賴

<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>

? ? ? ? <!--服務注冊-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
? ? ? ? ? ? <version>0.2.1.RELEASE</version>
? ? ? ? </dependency>
? ? ? ? <!--服務調(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 #服務名
? profiles:
? ? active: dev #環(huán)境設置
? cloud:
? ? nacos:
? ? ? discovery:
? ? ? ? server-addr: 127.0.0.1:8848 #nacos服務注冊

控制層請求

@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)服務注冊成功

在瀏覽器發(fā)起請求

? ?http://localhost:8001/client/index??

實際上網(wǎng)關把請求發(fā)送到gateway-client服務,返回結(jié)果

到此這篇關于springboot整合gateway實現(xiàn)網(wǎng)關功能的示例代碼的文章就介紹到這了,更多相關springboot gateway網(wǎng)關 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論