首先要?jiǎng)?chuàng)建一個(gè)新的springboot的項(xiàng)目猿挚,作為gateway的項(xiàng)目
1.引入pom.xml中的依賴( SpringCloud gateway基于webflux實(shí)現(xiàn)的乌奇,不是基于SpringBoot-web,所以應(yīng)該刪除spring-boot-starter-web依賴 )
? ? ? <parent>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? ? <version>2.0.5.RELEASE</version>
? ? ? ? <relativePath/> <!-- lookup parent from repository -->
? ? </parent>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-gateway</artifactId>
? ? ? ? ? ? <version>2.0.0.RELEASE</version>
? ? ? ? </dependency>
? ? ? ? <!-- nacos整合服務(wù)注冊與發(fā)現(xiàn)-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
? ? ? ? ? ? <version>0.2.2.RELEASE</version>
? ? ? ? </dependency>
? ? </dependencies>
2.配置application.yml
server:
? port: 80
####服務(wù)網(wǎng)關(guān)名稱
spring:
? application:
? ? name: pitch-gateway
? cloud:
? ? gateway:
? ? ?####解決跨域的問題
? ? ? globalcors:
? ? ? ? ? corsConfigurations:
? ? ? ? ? ? '[/**]':
? ? ? ? ? ? ? ? allowedHeaders:"*"
? ? ? ? ? ? ? ? allowedOrigins: "http://www.xinyues.com"? ? ? ? ? ?
? ? ? ? ? ? ? ? allowedMethods:
? ? ? ? ? ? ? ? ?- GET
#超時(shí)設(shè)置
? ? ? httpclient:
? ? ? ? ?connect-timeout: 5000? ?#毫秒
? ? ? ? ?response-timeout: 5s
? ? ? discovery:
? ? ? ? locator:
? ? ? ? ? ####允許從注冊中心獲取地址
? ? ? ? ? enabled: true
? ? ? routes:
? ? ? ? ###路由id? 自定義唯一,這個(gè)沒有什么保證唯一就行了
? ? ? ? - id: pitch
? ? ? ? ? ####pitch-member為會員服務(wù)的名稱轰驳,這個(gè)可以用lb這個(gè)后面跟微服務(wù)的名稱會用到負(fù)載均衡厚掷,如果用http? ? ? ? ? ?####不會進(jìn)行負(fù)責(zé)均衡
? ? ? ? ? uri: lb://pitch-member/
####這個(gè)是gateway提供的一些過濾器
? ? ? ? ?filters:
? ? ? ? ? ? - StripPrefix=1
? ? ? ? ? ###路由匹配規(guī)則 表示當(dāng)客戶端訪問http://ip:80/member/**地址時(shí), gateway就會轉(zhuǎn)發(fā)到pitch-member會員服務(wù)的具體地址
? ? ? ? ? predicates:
? ? ? ? ? ? - Path=/member/**
? ? nacos:
? ? ? discovery:
? ? ? ? ###將網(wǎng)關(guān)服務(wù)注冊到nacos
? ? ? ? server-addr: 127.0.0.1:8848
? ? ? ? enabled: true
3.就是根據(jù)業(yè)務(wù)進(jìn)行一些自定義的過濾規(guī)則了弟灼,自定義過濾規(guī)則的時(shí)候要實(shí)現(xiàn)globalfilter,有選擇的實(shí)現(xiàn)ordered
例子:
①實(shí)現(xiàn)權(quán)限管理冒黑,日志管理 參考
②使用全局過濾器解決跨域問題
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class CrossOriginFilter implements GlobalFilter {
? ? @Override
? ? public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
? ? ? ? // 允許跨域請求
? ? ? ? ServerHttpRequest request = exchange.getRequest();
? ? ? ? ServerHttpResponse response = exchange.getResponse();
? ? ? ? HttpHeaders headers = response.getHeaders();
? ? ? ?//在生產(chǎn)環(huán)境上最好指定域名田绑,以免產(chǎn)生跨域安全問題
? ? ? ? headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
? ? ? ? headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, OPTIONS, DELETE, PATCH");
? ? ? ? headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
? ? ? ? headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*");
? ? ? ? headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
? ? ? ? return chain.filter(exchange);
? ? }
}
gateway 的配置可以在數(shù)據(jù)庫中進(jìn)行配置,可參考https://blog.csdn.net/xiaobo5264063/article/details/105205974