简单路由
1,新建项目
2,POM文件
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies>
3,YML文件
server:
port: 9527
spring:
application:
name: cloud-gateway-service
cloud:
gateway:
routes:
- id: payment_routes1
uri: http://news.baidu.com/
predicates:
- Path=/guoji/**
- id: payment_routes2
uri: http://news.baidu.com/
predicates:
- Path=/guonei/**
3,主启动类
@SpringBootApplication public class Gateway9527Main { public static void main(String[] args) { SpringApplication.run(Gateway9527Main.class,args); } }
访问http://localhost:9527/guonei就可以转发到http://news.baidu.com/guonei
Gateway网关路由有两种配置方式:
1,在配置文件yml中配置。
2,代码中注入ReuteLocator的Bean。
代码注入RouteLocator的Bean
@Bean public RouteLocator cuctomRouteLocator(RouteLocatorBuilder routeLocatorBuilder){ return routeLocatorBuilder.routes().route("route_baidu",r->r.path("/finance").uri("http://news.baidu.com")).build(); }
访问http://localhost:9527/finance就可以转发到http://news.baidu.com/finance。
通过微服务名实现动态路由
默认情况下Gateway会根据注册中心注册的服务列表,以注册中心上微服务为路径创建动态路由进行转发,从而实现动态路由的功能。
1,新建项目
2,POM文件
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies>
3,yml文件
server:
port: 9527
spring:
application:
name: cloud-gateway-service
cloud:
gateway:
routes:
- id: payment_routes1 #路由id 唯一即可
uri: lb://CLOUD-PAYMENT-SERVICE #匹配后提供路由的路由地址
predicates:
- Path=/payment/** #断言 匹配条件
- id: payment_routes2
uri: lb://CLOUD-PAYMENT-SERVICE
predicates:
- Path=/payment/**
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: gateway7001
prefer-ip-address: true
lease-renewal-interval-in-seconds: 30
lease-expiration-duration-in-seconds: 90
4,主启动类
@SpringBootApplication @EnableEurekaClient public class Gateway9527Main { public static void main(String[] args) { SpringApplication.run(Gateway9527Main.class,args); } }
5,测试。启动注册中心,微服务提供者,网关服务。
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。lb://serviceName是Spring Cloud gateway在微服务中自动创建的负载均衡uri。
Gateway Filter Factories
路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器指定路由进行使用。
SpringCloud Gateway内置了多种路由过滤器,都由GatewayFilter的工厂类产生。
过滤器一般使用自定义的,实现GlobalFilter和Ordered接口。
@Component public class MyGatewayFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { System.out.println(" globalFilter "+new Date()); String userName = exchange.getRequest().getQueryParams().getFirst("userName"); if(userName==null){ System.out.println("用户为null,非法用户"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); } @Override public int getOrder() { return 0; } }
这样可以实现网关鉴权,日志记录等功能。
posted on
浙公网安备 33010602011771号