GateWay的简单使用
GateWay的简单使用
一、引入pom
<!-- 网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 服务注册/发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 整合hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
二、修改配置
bootstrap.yml
#bootstrap中的内容优先执行,并且不可被覆盖
spring:
#应用名称
application:
name: hgk-gateway
#当前使用的环境
profiles:
active: prod
#nacos配置
cloud:
nacos:
#配置中心地址
config:
server-addr: 192.168.90.138:8848
namespace: 38be2182-ebf0-427d-995c-6c907466f48f
#服务注册地址
discovery:
server-addr: 192.168.90.138:8848
application.yml
#配置项目的端口和默认路径
server:
servlet:
context-path: /
#nacos
#路由相关配置
spring:
#网关配置
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
# 允许携带认证信息
# 允许跨域的源(网站域名/ip),设置*为全部
# 允许跨域请求里的head字段,设置*为全部
# 允许跨域的method, 默认为GET和OPTIONS,设置*为全部
# 跨域允许的有效期
allow-credentials: true
allowed-origins: "*"
allowed-headers: "*"
allowed-methods: "*"
max-age: 3600
#default-filters[0]: DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials,RETAIN_FIRST
routes:
- id: hgk-admin
uri: lb://hgk-admin
predicates:
- Path=/api/admin/** #访问 /api/thirdparty 这个路径就路由到
filters: #路径重写 成一下 这里中间逗号, 前面是截取的路径,后面是替换的路径
#也就是你看以为你实际访问是 /api/thirdparty/oss/policy,实际上它是吧 /api//api/thirdparty这一段给丢了,只要只要oss/policy
#
- RewritePath=/api/(?<segment>.*),/$\{segment}
- id: hgk-im
uri: lb://hgk-im
predicates:
- Path=/api/im/** #访问 /api/thirdparty 这个路径就路由到
filters: #路径重写 成一下 这里中间逗号, 前面是截取的路径,后面是替换的路径
#也就是你看以为你实际访问是 /api/thirdparty/oss/policy,实际上它是吧 /api//api/thirdparty这一段给丢了,只要只要oss/policy
#
- RewritePath=/api/(?<segment>.*),/$\{segment}
default-filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/fallback #返回路径
#hystrix配置
hystrix:
command:
fallbackcmd:
execution:
isolation:
thread:
#断路器的超时时间ms,默认1000
timeoutInMilliseconds: 2000
circuitBreaker:
#是否启动熔断器,默认为true,false表示不要引入Hystrix。
enabled: true
#当在配置时间窗口内达到此数量的失败后,进行短路
requestVolumeThreshold: 20
#出错百分比阈值,当达到此阈值后,开始短路。默认50%)
errorThresholdPercentage: 50%
#短路多久以后开始尝试是否恢复,默认5s)-单位ms
sleepWindowInMilliseconds: 30000
application-dev.yml
#配置项目的端口和默认路径
server:
port: 80
三、启动器
package com.sinosoft;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author rayfoo@qq.com
* @version 1.0
* <p>启动器</p>
* @date 2020/12/27 23:47
*/
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
public class SinoGatewayRunner {
public static void main(String[] args) {
SpringApplication.run(SinoGatewayRunner.class, args);
}
}