前言
在工作中遇到一種情況,一個父項(xiàng)目中有兩個子項(xiàng)目。實(shí)際使用時,需要外網(wǎng)可以訪問,寶信軟件只能將一個端口號發(fā)布在外網(wǎng)上,所以需要運(yùn)用網(wǎng)關(guān)技術(shù),通過一個端口號訪問兩個項(xiàng)目。
之前已經(jīng)試用nacos搭建了注冊中心
新建gateway子項(xiàng)目
pom.xml
導(dǎo)入依賴時注意SpringCloudAlibaba與gateway依賴的版本是否對應(yīng),否則啟動時會報(bào)錯。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>testmaven32springcloud</artifactId>
<groupId>com.hzx</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway-project</artifactId>
<dependencies>
<!-- 此依賴已經(jīng)在父項(xiàng)目pom中導(dǎo)入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
配置文件
將gateway服務(wù)注冊到nacos中
server:
port: 8901
spring:
cloud:
nacos:
discovery:
server-addr: http://192.168.0.248:8848
namespace: 42d5d19a-8564-4da2-8b97-7e4eac1c53a3
gateway:
discovery:
locator:
enabled: true
globalcors:
cors-configurations:
'[/**]': # 允許跨域訪問的資源
allowedOrigins: "*" #跨域允許來源
allowedMethods: "*"
allowedHeaders: "*"
default-filters:
- DedupeResponseHeader=Vary Access-Control-Allow-Origin Access-Control-Allow-Credentials, RETAIN_FIRST
application:
name: nacos-gateway
配置文件中:spring.cloud.gateway.globalcors.cors-configurations是專門配置跨域的。也可以用配置類的方法(但我試過后沒有成功。。。)需注意配置類中UrlBasedCorsConfigurationSource的包名。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter(){
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*"); //允許的method
config.addAllowedOrigin("*"); //允許的來源
config.addAllowedHeader("*"); //允許的請求頭參數(shù)
//允許訪問的資源
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**",config);
return new CorsWebFilter(source);
}
}
啟動類
在啟動類中需要加上注解:@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class StartGatewayApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(StartGatewayApplication.class, args);
}
}
以上就是gateway相關(guān)的代碼,啟動成功后就可以使用了。
我在學(xué)習(xí)的時候沒想到這么簡單。
訪問接口方式
http://ip:網(wǎng)關(guān)端口/nacos中注冊的服務(wù)名稱/controller層路徑
具體內(nèi)容見測試部分
測試
上一篇講nacos的文章中,我創(chuàng)建了兩個子項(xiàng)目,分別是:nacos-provider-project、nacos-consumer-project,連同gateway項(xiàng)目啟動后,在nacos可以看到注冊的服務(wù)。
在nacos-provider-project項(xiàng)目添加接口
@RequestMapping(value = "/send/provider/{msg}",method = RequestMethod.GET)
public String sendMessageProvider(@PathVariable String msg){
return "調(diào)用生產(chǎn)者端接口,向生產(chǎn)者發(fā)送消息:"+msg;
}
在nacos-consumer-project項(xiàng)目添加接口
@RequestMapping(value = "/send/consumer/{msg}",method = RequestMethod.GET)
public String sendMessageConsumer(@PathVariable String msg){
return "調(diào)用消費(fèi)者端接口,向消費(fèi)者發(fā)送消息:"+msg;
}
通過接口文檔測試上面的兩個接口
首先是直接通過項(xiàng)目本身的端口號訪問接口。其中8081和8091分別是兩個項(xiàng)目的端口號。
然后通過網(wǎng)關(guān)端口分別訪問兩個接口。其中8901為gateway項(xiàng)目端口號,nacos-provider與nacos-consumer分別為兩個項(xiàng)目在nacos注冊中心的服務(wù)名稱。文章來源:http://www.zghlxwxcb.cn/news/detail-730791.html
拓展
本文中使用的是gateway默認(rèn)配置網(wǎng)關(guān)的方法,開發(fā)者還可以自定義配置路由,也可以不通過注冊在nacos中的服務(wù)名就能訪問接口,但這兩種方法目前本人還不需要,所以文章中沒有寫出。文章來源地址http://www.zghlxwxcb.cn/news/detail-730791.html
到了這里,關(guān)于SpringCloud Alibaba【三】Gateway的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!