最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1.項目目錄:

創(chuàng)建項目gateway作為父類

2.代碼實現(xiàn):

父類依賴

?

<parent>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? ? <version>2.6.2</version>
? ? ? ? <relativePath/> <!-- lookup parent from repository -->
? ? </parent>
? ? <groupId>com.cxh</groupId>
? ? <artifactId>gateway</artifactId>
? ? <version>0.0.1-SNAPSHOT</version>
? ? <name>gateway</name>
? ? <description>Demo project for Spring Boot</description>
? ? <packaging>pom</packaging>
? ? <properties>
? ? ? ? <java.version>8</java.version>
? ? ? ? <spring-cloud-alibaba-dependencies.version>2021.1</spring-cloud-alibaba-dependencies.version>
? ? ? ? <spring-cloud-dependencies.version>2021.0.0</spring-cloud-dependencies.version>
? ? </properties>

? ? <dependencyManagement>
? ? ? ? <dependencies>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-cloud-dependencies</artifactId>
? ? ? ? ? ? ? ? <version>${spring-cloud-dependencies.version}</version>
? ? ? ? ? ? ? ? <type>pom</type>
? ? ? ? ? ? ? ? <scope>import</scope>
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>com.alibaba.cloud</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-cloud-alibaba-dependencies</artifactId>
? ? ? ? ? ? ? ? <version>${spring-cloud-alibaba-dependencies.version}</version>
? ? ? ? ? ? ? ? <type>pom</type>
? ? ? ? ? ? ? ? <scope>import</scope>
? ? ? ? ? ? </dependency>
? ? ? ? </dependencies>
? ? </dependencyManagement>

?

創(chuàng)建module項目gateway-client

添加依賴

<parent>
? ? ? ? <groupId>com.cxh</groupId>
? ? ? ? <artifactId>gateway</artifactId>
? ? ? ? <version>0.0.1-SNAPSHOT</version>
? ? ? ? <relativePath/> <!-- lookup parent from repository -->
? ? </parent>
? ? <groupId>com.cxh</groupId>
? ? <artifactId>gateway-client</artifactId>
? ? <version>0.0.1-SNAPSHOT</version>
? ? <name>gateway-client</name>
? ? <description>Demo project for Spring Boot</description>
? ? <properties>
? ? ? ? <java.version>1.8</java.version>
? ? </properties>
? ? <dependencies>
? ? ? ? <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.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>
? ? ? ? <!--服務調用-->
? ? ? ? <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>

? ? </dependencies>

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

    @Value("${server.port}")
    private String port;

    @RequestMapping("/index")
    public String index(){
        return "gateway-client端口:" + port;
    }
}

啟動類添加注解

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayClientApplication {

? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(GatewayClientApplication.class, args);
? ? }

}

創(chuàng)建module項目gateway-service

添加依賴

<parent>
? ? ? ? <groupId>com.cxh</groupId>
? ? ? ? <artifactId>gateway</artifactId>
? ? ? ? <version>0.0.1-SNAPSHOT</version>
? ? ? ? <relativePath/> <!-- lookup parent from repository -->
? ? </parent>
? ? <groupId>com.cxh</groupId>
? ? <artifactId>gateway-service</artifactId>
? ? <version>0.0.1-SNAPSHOT</version>
? ? <name>gateway-service</name>
? ? <description>Demo project for Spring Boot</description>
? ? <properties>
? ? ? ? <java.version>1.8</java.version>
? ? </properties>
? ? <dependencies>
? ? ? ? <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-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>

? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-feign</artifactId>
? ? ? ? ? ? <version>1.4.3.RELEASE</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-loadbalancer</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
? ? ? ? ? ? <version>2.2.10.RELEASE</version>
? ? ? ? </dependency>


? ? </dependencies>

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

跨域配置

@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);
? ? }
}

3.實現(xiàn)效果:

啟動nacos后,再啟動gateway-client, gateway-service項目,打開瀏覽器http://localhost:8001/client/index

返回信息:gateway-client端口:8002

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

相關文章

最新評論

名山县| 松潘县| 大洼县| 康平县| 林口县| 简阳市| 通江县| 甘谷县| 双鸭山市| 邹平县| 芜湖县| 扶沟县| 青川县| 鲁山县| 永年县| 宝应县| 永安市| 西林县| 亚东县| 万荣县| 彰化市| 瑞丽市| 略阳县| 大埔区| 贵阳市| 垫江县| 文水县| 阜新市| 望江县| 定襄县| 万山特区| 玉门市| 灵石县| 伽师县| 贵南县| 阿克苏市| 镇赉县| 阳西县| 商河县| 昌图县| 信阳市|