1. 新建服務泼菌,pom文件
新建gateway-server
服務迄薄,pom文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>gateway-server</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>gateway-server</name>
<description>gateway網關服務</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- 測試路由用的琅关,
可訪問 http://ip:port/actuator/gateway/routes
查看路由是否配置生效 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Nacos注冊和服務配置,網關也要放到nacos上的 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Spring Cloud Gateway 的早期版本中讥蔽,Ribbon 被用作默認的負載均衡器, Spring Cloud 2020.0.0 版本開始涣易,Ribbon 被廢棄,Spring Cloud LoadBalancer 成為了推薦的負載均衡方案冶伞。為了靈活性新症,需要手動引入-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- 后續(xù)關聯(lián)sentinel用 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.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.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>gateway-service</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- 編譯打包跳過test -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
特別注意:
- Spring Cloud Gateway 不使用 Web 作為服務器,而是 使用 WebFlux 作為服務器响禽,Gateway 項目已經依賴了
starter-webflux
徒爹,所以這里 千萬不要依賴starter-web
- 由于過濾器等功能依然需要 Servlet 支持,故這里還需要依賴
javax.servlet:javax.servlet-api
2. 跨域配置
因為不是Web 作為服務器芋类,和其他的稍有不同
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
/**
* 跨域配置
*/
@Configuration
public class CorsConfig implements WebFluxConfigurer
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedOrigins("*")
.allowedHeaders("*")
.allowedMethods("*")
.exposedHeaders(HttpHeaders.SET_COOKIE);
}
/**
* 跨域過濾器
* @return
*/
@Bean
public CorsWebFilter corsWebFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addExposedHeader(HttpHeaders.SET_COOKIE);
UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(corsConfigurationSource);
}
}
3. 啟動文件添加@EnableFeignClients
注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
4. nacos中配置信息
# 1.server
server:
port: 10000
# 2.log
logging:
level:
org.springframework.cloud.gateway: info
# 3. openfeign
feign:
sentinel:
enabled: true
# 4. 為了監(jiān)控網關
management:
endpoints:
web:
exposure:
include: "*"
# 5.spring
spring:
# 統(tǒng)一設置返回的時間格式
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: NON_NULL
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
# 自定義路由 ID
- id: service-dict
# 采用 LoadBalanceClient 方式請求隆嗅,以 lb:// 開頭,后面的是注冊在 Nacos 上的服務名
uri: lb://service-dict
# Predicate 翻譯過來是“謂詞”的意思侯繁,必須榛瓮,主要作用是匹配用戶的請求,有很多種用法
predicates:
# 路徑匹配巫击,以 api 開頭禀晓,直接配置是不生效的,看 filters 配置
- Path=/api/dict/**
filters:
# 前綴過濾坝锰,默認配置下粹懒,我們的請求路徑是 http://localhost:9000/myshop-service-consumer-item/** 這時會路由到指定的服務
# 此處配置去掉 1 個路徑前綴,再配置上面的 Path=/api/**顷级,就能按照 http://localhost:9000/api/** 的方式訪問了
- StripPrefix=1
- id: service-file
uri: lb://service-file
predicates:
- Path=/api/file/**
filters:
- StripPrefix=1