Gateway 网关
管理众多接口的链接,让网关一个url调控全部的子链接,不再需要运维人员或者开发人员修改大量的配置文件,防止url改错。
基础
https://blog.csdn.net/qq_41520636/article/details/115795547
开始
创建一个新项目,api-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.hikktn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-server</artifactId>
<dependencies>
<!--引入gateway 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 80
spring:
application:
name: api-server
cloud:
# 网关配置
gateway:
# 路由配置:转发规则
routes: #集合。
# id: 唯一标识。默认是一个UUID
# uri: 转发路径
# predicates: 条件,用于请求网关路径的匹配规则
# filters:配置局部过滤器的
- id: hystrix-provider
# 静态路由
# uri: http://localhost:8001/
# 动态路由
uri: lb://hystrix-provider
predicates:
- Path=/goods/**
# filters:
# - AddRequestParameter=username,zhangsan
- id: hystrix-consumer
# uri: http://localhost:9000
uri: lb://hystrix-consumer
predicates:
- Path=/order/**
# 微服务名称配置
discovery:
locator:
enabled: true # 设置为true 请求路径前可以添加微服务名称
lower-case-service-id: true # 允许为小写
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
启动类
package com.hikktn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @ClassName ApiApp
* @Description TODO
* @Author lisonglin
* @Date 2021/4/18 21:11
* @Version 1.0
*/
@SpringBootApplication
@EnableEurekaClient
public class ApiApp {
public static void main(String[] args){
SpringApplication.run(ApiApp.class,args);
}
}
测试
三个项目启动
- eureka-server
- api-server
- hystrix-consumer
- hystrix-provider
下面这个就是默认80端口访问
拦截器
局部拦截器
spring:
cloud:
# 网关配置
gateway:
- id: hystrix-provider
filters:
- AddRequestParameter=username,zhangsan
hystrix-provider
GoodsController.java中
public AbstractResult<Goods> findOne(@PathVariable("id") int id,String username) {
Goods goods = goodsService.findOne(id);
//如果id == 1 ,则出现异常,id != 1 则正常访问
if (id == 1) {
//1.造个异常
int i = 3 / 0;
}
// //当前线程睡2秒
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
goods.setTitle(goods.getTitle() + ":" + port + ":" + username + ",服务端版本:" + version);//将端口号,设置到了 商品标题上
AbstractResult<Goods> result = new AbstractResult<Goods>(1, 200, "服务端正常访问", goods);
return result;
}
测试就不演示了。
全局拦截器
package com.hikktn.filters;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @ClassName MyFilter
* @Description TODO
* @Author lisonglin
* @Date 2021/4/18 21:22
* @Version 1.0
*/
@Component
public class MyFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("自定义全局过滤器执行了。");
// 放行
return chain.filter(exchange);
}
/**
* 过滤器排序
* @return 数值越小 越优先执行
*/
@Override
public int getOrder() {
return 0;
}
}
只有通过网关访问,全局拦截器才会起作用。
演示
结果
网关访问
结果