springcloud整合gateway實現(xiàn)網(wǎng)關(guān)的示例代碼
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> ? ? ? ? <!--服務(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> ? ? </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ù)注冊
控制層
@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> ? ? ? ? <!--服務(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> ? ? ? ? <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: ? ? ? ? # 透傳服務(wù) ? ? ? ? - id: gateway-client #設(shè)置路由id ? ? ? ? ? uri: lb://gateway-client ?#設(shè)置路由的url lb://nacos服務(wù)注冊名稱 ? ? ? ? ? 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
到此這篇關(guān)于springcloud整合gateway實現(xiàn)網(wǎng)關(guān)的示例代碼的文章就介紹到這了,更多相關(guān)springcloud gateway網(wǎng)關(guān)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java SpringBoot啟動指定profile的8種方式詳解
這篇文章主要介紹了spring boot 如何指定profile啟動的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
淺談Java 類中各成分加載順序和內(nèi)存中的存放位置
下面小編就為大家?guī)硪黄獪\談Java 類中各成分加載順序和內(nèi)存中的存放位置。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
java前后端傳值,參數(shù)有集合類型的數(shù)據(jù)時的兩種操作方式
這篇文章主要介紹了java前后端傳值,參數(shù)有集合類型的數(shù)據(jù)時的兩種操作方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot自動初始化數(shù)據(jù)庫的方法分享
我們在項目中應(yīng)該經(jīng)常遇到過初始化數(shù)據(jù)的場景,特別是項目部署或者交付的時候,那么有什么方式可以在項目啟動的時候自動初始化數(shù)據(jù)庫呢,下面小編就來和大家分享幾個方法吧2023-08-08
使用Jmeter進(jìn)行http接口測試的詳細(xì)流程
本文主要針對http接口進(jìn)行測試,使用Jmeter工具實現(xiàn), Jmter工具設(shè)計之初是用于做性能測試的,它在實現(xiàn)對各種接口的調(diào)用方面已經(jīng)做的比較成熟,因此,本次直接使用Jmeter工具來完成對Http接口的測試,需要的朋友可以參考下2024-12-12
Java?Thread.currentThread().getName()?和?this.getName()區(qū)別詳
本文主要介紹了Thread.currentThread().getName()?和?this.getName()區(qū)別詳解,TestThread?testThread?=?new?TestThread();2022-02-02
springBoot下實現(xiàn)java自動創(chuàng)建數(shù)據(jù)庫表
這篇文章主要介紹了springBoot下實現(xiàn)java自動創(chuàng)建數(shù)據(jù)庫表的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

