当前位置:   article > 正文

SpringCloud学习(八)-- Gateway服务引入_springcloud 引入gateway

springcloud 引入gateway

1,引入必要的包

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-gateway</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. </dependencies>

2,编写配置文件

  1. server:
  2. port: 10010
  3. spring:
  4. application:
  5. name: api-gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: user-service-route #路由id
  10. #uri: http://127.0.0.1:9091 #代理的服务地址
  11. uri: lb://user-service #lb 从Eureka中获取具体的服务
  12. predicates: #路由断言:可以匹配映射路径
  13. - Path=/user/**
  14. eureka:
  15. client:
  16. service-url:
  17. defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
  18. instance:
  19. prefer-ip-address: true
  20. ip-address: 127.0.0.1

3,启动类

  1. @SpringBootApplication
  2. @EnableDiscoveryClient//启用Eureka注册中心
  3. public class GatewayApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(GatewayApplication.class,args);
  6. }
  7. }

4,添加默认路由器:

全局和局部如下

  1. spring:
  2. application:
  3. name: api-gateway
  4. cloud:
  5. gateway:
  6. routes:
  7. - id: user-service-route #路由id
  8. #uri: http://127.0.0.1:9091 #代理的服务地址
  9. uri: lb://user-service #lb 从Eureka中获取具体的服务
  10. predicates: #路由断言:可以匹配映射路径
  11. - Path=/**
  12. filters: #局部路由过滤器
  13. - PrefixPath=/user #添加请求路径的前缀
  14. default-filters: #全局路由过滤器
  15. - AddResponseHeader=X-Reponse-Foo,Bar

5,添加自定义局部过滤器

通过观察gateway服务自带的默认路由器,发现都是继承一个 AbstractGatewayFilterFactory的类,如下图所示

所以,我们的自定义局部过滤器也如此实现

1)添加配置文件

  1. spring:
  2. application:
  3. name: api-gateway
  4. cloud:
  5. gateway:
  6. routes:
  7. - id: user-service-route #路由id
  8. #uri: http://127.0.0.1:9091 #代理的服务地址
  9. uri: lb://user-service #lb 从Eureka中获取具体的服务
  10. predicates: #路由断言:可以匹配映射路径
  11. - Path=/**
  12. filters: #局部路由过滤器
  13. - PrefixPath=/user #添加请求路径的前缀
  14. - MyParam=name #添加自定义过滤器
  15. default-filters: #全局路由过滤器
  16. - AddResponseHeader=X-Reponse-Foo,Bar

2)实现自定义过滤器

  1. package com.wayne.filter;
  2. import org.springframework.cloud.gateway.filter.GatewayFilter;
  3. import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
  4. import org.springframework.http.server.reactive.ServerHttpRequest;
  5. import org.springframework.stereotype.Component;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. @Component
  9. public class MyParamGatewayFilterFactory extends AbstractGatewayFilterFactory<MyParamGatewayFilterFactory.Config> {
  10. final static String PARAM_NAME = "param";
  11. public MyParamGatewayFilterFactory(){
  12. super(Config.class);
  13. }
  14. public List<String> shortcutFieldOrder(){
  15. return Arrays.asList(PARAM_NAME);
  16. }
  17. @Override
  18. public GatewayFilter apply(Config config) {
  19. return ((exchange, chain) -> {
  20. ServerHttpRequest request = exchange.getRequest();
  21. if(request.getQueryParams().containsKey(config.param)){
  22. request.getQueryParams().get(config.param)
  23. .forEach(value -> System.out.printf("----局部过滤器---%s = %s---\n",config.param,value));
  24. }
  25. return chain.filter(exchange);
  26. });
  27. }
  28. public static class Config{
  29. private String param;
  30. public Config(){
  31. }
  32. public String getParam() {
  33. return param;
  34. }
  35. public void setParam(String param) {
  36. this.param = param;
  37. }
  38. }
  39. }

7,配置全局过滤器

全局过滤器直接用即可,实现GlobalFilter接口。如果对过滤器的顺序有要求,可同时实现 Ordered接口

  1. package com.wayne.filter;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
  4. import org.springframework.cloud.gateway.filter.GlobalFilter;
  5. import org.springframework.core.Ordered;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.web.server.ServerWebExchange;
  9. import reactor.core.publisher.Mono;
  10. @Component
  11. public class TokenCheckFilter implements GlobalFilter, Ordered {
  12. @Override
  13. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  14. System.out.println("--------这个是全局过滤器MyGlobalFilter-----");
  15. String token = exchange.getRequest().getQueryParams().getFirst("token");
  16. if(StringUtils.isBlank(token)){
  17. exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
  18. return exchange.getResponse().setComplete();
  19. }
  20. return chain.filter(exchange);
  21. }
  22. @Override
  23. public int getOrder() {
  24. //值越小,越优先
  25. return 1;
  26. }
  27. }

6,测试

浏览器输入 http://127.0.0.1:10010/1?name=Wyane&token=abd

出现如下信息

控制台

如果没有加token,则浏览器出现401错误

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/301453
推荐阅读
相关标签
  

闽ICP备14008679号