亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

1.項(xiàng)目目錄:

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

2.代碼實(shí)現(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項(xiàng)目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>

? ? ? ? <!--服務(wù)注冊(cè)-->
? ? ? ? <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>

? ? </dependencies>

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ù)注冊(cè)

控制層

@RestController
public class ClientController {

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

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

啟動(dòng)類添加注解

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayClientApplication {

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

}

創(chuàng)建module項(xiàng)目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>
? ? ? ? <!--服務(wù)注冊(cè)/發(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>

? ? ? ? <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 #服務(wù)名
? profiles:
? ? active: dev #環(huán)境設(shè)置
? cloud:
? ? gateway:
? ? ? routes:
? ? ? ? # 透?jìng)鞣?wù)
? ? ? ? - id: gateway-client #設(shè)置路由id
? ? ? ? ? uri: lb://gateway-client ?#設(shè)置路由的url lb://nacos服務(wù)注冊(cè)名稱
? ? ? ? ? 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.實(shí)現(xiàn)效果:

啟動(dòng)nacos后,再啟動(dòng)gateway-client, gateway-service項(xiàng)目,打開(kāi)瀏覽器http://localhost:8001/client/index

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

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

相關(guān)文章

  • 找出鏈表倒數(shù)第n個(gè)節(jié)點(diǎn)元素的二個(gè)方法

    找出鏈表倒數(shù)第n個(gè)節(jié)點(diǎn)元素的二個(gè)方法

    本文提供了找出鏈表倒數(shù)第n個(gè)節(jié)點(diǎn)元素的二個(gè)方法,其中一個(gè)方法是JAVA代碼實(shí)現(xiàn)
    2013-11-11
  • Spring底層核心源碼原理解析

    Spring底層核心源碼原理解析

    這篇文章主要介紹了Spring底層核心源碼原理解析,當(dāng)在某個(gè)方法上加了@Transactional注解后,就表示該方法在調(diào)用時(shí)會(huì)開(kāi)啟Spring事務(wù),而這個(gè)方法所在的類所對(duì)應(yīng)的Bean對(duì)象會(huì)是該類的代理對(duì)象,需要的朋友可以參考下
    2023-09-09
  • Spring聲明式事務(wù)@Transactional知識(shí)點(diǎn)分享

    Spring聲明式事務(wù)@Transactional知識(shí)點(diǎn)分享

    在本篇文章里小編給大家整理了關(guān)于Spring聲明式事務(wù)@Transactional詳解內(nèi)容,需要的朋友們可以參考下。
    2020-02-02
  • springboot 使用zookeeper實(shí)現(xiàn)分布式隊(duì)列的基本步驟

    springboot 使用zookeeper實(shí)現(xiàn)分布式隊(duì)列的基本步驟

    這篇文章主要介紹了springboot 使用zookeeper實(shí)現(xiàn)分布式隊(duì)列,通過(guò)ZooKeeper的協(xié)調(diào)和同步機(jī)制,多個(gè)應(yīng)用程序可以共享一個(gè)隊(duì)列,并按照先進(jìn)先出的順序處理隊(duì)列中的消息,需要的朋友可以參考下
    2023-08-08
  • 一篇文章帶你入門(mén)Java接口

    一篇文章帶你入門(mén)Java接口

    這篇文章主要介紹了JAVA中接口的定義和接口的實(shí)現(xiàn),文中講解非常細(xì)致,配合代碼更好的幫大家學(xué)習(xí)參考,感興趣的朋友可以了解下
    2021-08-08
  • Dubbo之降級(jí)Mock源碼分析

    Dubbo之降級(jí)Mock源碼分析

    這篇文章主要為大家介紹了Dubbo降級(jí)Mock源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • MyBatis-Plus通過(guò)version機(jī)制實(shí)現(xiàn)樂(lè)觀鎖的思路

    MyBatis-Plus通過(guò)version機(jī)制實(shí)現(xiàn)樂(lè)觀鎖的思路

    version機(jī)制的核心思想就是,假設(shè)發(fā)生并發(fā)沖突的幾率很低,只有當(dāng)更新數(shù)據(jù)的時(shí)候采取檢查是否有沖突,而判斷是否有沖突的依據(jù)就是version的值是否被改變了,這篇文章主要介紹了MyBatis-Plus通過(guò)version機(jī)制實(shí)現(xiàn)樂(lè)觀鎖的思路,需要的朋友可以參考下
    2021-09-09
  • Spring依賴注入底層原理詳解

    Spring依賴注入底層原理詳解

    這篇文章主要介紹了Spring依賴注入底層原理詳解,??依賴注入是一種設(shè)計(jì)模式,它將對(duì)象之間的依賴關(guān)系從代碼中移除,并由容器來(lái)管理這些依賴關(guān)系,依賴注入的主要目的是降低代碼的耦合度,使代碼更加靈活和可維護(hù),需要的朋友可以參考下
    2023-09-09
  • Java基于Base64實(shí)現(xiàn)編碼解碼圖片文件

    Java基于Base64實(shí)現(xiàn)編碼解碼圖片文件

    這篇文章主要介紹了Java基于Base64實(shí)現(xiàn)編碼解碼圖片文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • java顯示當(dāng)前的系統(tǒng)時(shí)間

    java顯示當(dāng)前的系統(tǒng)時(shí)間

    這篇文章主要介紹了java如何顯示當(dāng)前的系統(tǒng)時(shí)間,代碼很簡(jiǎn)單,自己可以自定義顯示的系統(tǒng)時(shí)間的顏色和字體,需要的朋友可以參考下
    2015-10-10

最新評(píng)論