1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.cloud</groupId>
    4. <artifactId>spring-cloud-starter-gateway</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.alibaba.cloud</groupId>
    8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    9. </dependency>
    10. </dependencies>

    网关服务里 pom.xml里加上如上命令

    yml文件:

    1. server:
    2. port: 80
    3. spring:
    4. cloud:
    5. gateway:
    6. discovery:
    7. locator:
    8. enabled: true # 启用gateway 服务发现功能
    9. lower-case-service-id: true # 启用小驼峰的服务名称访问服务
    10. routes: # 自定义路由机制
    11. -id: admin-service-router # /admin/**--->admin-service
    12. uri: lb://admin-service
    13. predicates:
    14. -Path = admin/**
    15. -id: member-service-router # /member/**--->member-service
    16. uri: lb://member-service
    17. predicates:
    18. -Path = member/**
    19. - id: finance-service-router # /finance/**--->finance-service
    20. uri: lb://finance-service
    21. predicates:
    22. - Path = finance/**
    23. - id: chan-service-router # /chan/**--->chan-service
    24. uri: lb://chan-service
    25. predicates:
    26. - Path = chan/**
    27. - id: statistics-service-router # /statistics/**--->statistics-service
    28. uri: lb://statistics-service
    29. predicates:
    30. - Path=statistics/**
    31. - id: match-service-router # /match/**--->match-service
    32. uri: lb://match-service
    33. predicates:
    34. - Path=match/**
    35. - id: exchange-service-router # /exchange/**--->exchange-service
    36. uri: lb://exchange-service
    37. predicates:
    38. - Path=match/**
    39. nacos:
    40. server-addr: 192.168.19.128:8848

    config文件夹里有一个解决跨域的类:

    1. package com.oy.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.web.cors.CorsConfiguration;
    5. import org.springframework.web.cors.reactive.CorsWebFilter;
    6. import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
    7. /**
    8. * 解决跨域问题
    9. */
    10. @Configuration
    11. public class CorsConfig {
    12. @Bean
    13. public CorsWebFilter corsWebFilter(){
    14. CorsConfiguration corsConfiguration = new CorsConfiguration();
    15. corsConfiguration.addAllowedHeader("*");
    16. corsConfiguration.addAllowedOrigin("*");
    17. corsConfiguration.addAllowedMethod("*");
    18. UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
    19. urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
    20. return new CorsWebFilter(urlBasedCorsConfigurationSource) ;
    21. }
    22. }

    JWTFilter: 拦截用户请求,判断用户是否需要token

    1. package com.bjsxt.filter;
    2. import com.alibaba.fastjson.JSONObject;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.beans.factory.annotation.Value;
    5. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    6. import org.springframework.cloud.gateway.filter.GlobalFilter;
    7. import org.springframework.core.Ordered;
    8. import org.springframework.core.io.buffer.DataBuffer;
    9. import org.springframework.data.redis.core.StringRedisTemplate;
    10. import org.springframework.http.HttpHeaders;
    11. import org.springframework.http.HttpStatus;
    12. import org.springframework.http.server.reactive.ServerHttpResponse;
    13. import org.springframework.stereotype.Component;
    14. import org.springframework.util.StringUtils;
    15. import org.springframework.web.server.ServerWebExchange;
    16. import reactor.core.publisher.Flux;
    17. import reactor.core.publisher.Mono;
    18. import java.util.Set;
    19. @Component
    20. public class JwtCheckFilter implements GlobalFilter, Ordered {
    21. @Autowired
    22. private StringRedisTemplate redisTemplate ;
    23. @Value("${no.require.urls:/admin/login,/user/gt/register,/user/login,/user/users/register,/user/sms/sendTo,/user/users/setPassword}")
    24. private Set<String> noRequireTokenUris ;
    25. /**
    26. * 过滤器拦截到用户的请求后做啥
    27. * @param exchange
    28. * @param chain
    29. * @return
    30. */
    31. @Override
    32. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    33. // 1 : 该接口是否需要token 才能访问
    34. if(!isRequireToken(exchange)){
    35. return chain.filter(exchange) ;// 不需要token ,直接放行
    36. }
    37. // 2: 取出用户的token
    38. String token = getUserToken(exchange) ;
    39. // 3 判断用户的token 是否有效
    40. if(StringUtils.isEmpty(token)){
    41. return buildeNoAuthorizationResult(exchange) ;
    42. }
    43. Boolean hasKey = redisTemplate.hasKey(token);
    44. if(hasKey!=null && hasKey){
    45. return chain.filter(exchange) ;// token有效 ,直接放行
    46. }
    47. return buildeNoAuthorizationResult(exchange) ;
    48. }
    49. /**
    50. * 给用户响应一个没有token的错误
    51. * @param exchange
    52. * @return
    53. */
    54. private Mono<Void> buildeNoAuthorizationResult(ServerWebExchange exchange) {
    55. ServerHttpResponse response = exchange.getResponse();
    56. response.getHeaders().set("Content-Type","application/json");
    57. response.setStatusCode(HttpStatus.UNAUTHORIZED) ;
    58. JSONObject jsonObject = new JSONObject();
    59. jsonObject.put("error","NoAuthorization") ;
    60. jsonObject.put("errorMsg","Token is Null or Error") ;
    61. DataBuffer wrap = response.bufferFactory().wrap(jsonObject.toJSONString().getBytes());
    62. return response.writeWith(Flux.just(wrap)) ;
    63. }
    64. /**
    65. * 从 请求头里面获取用户的token
    66. * @param exchange
    67. * @return
    68. */
    69. private String getUserToken(ServerWebExchange exchange) {
    70. String token = exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
    71. return token ==null ? null : token.replace("bearer ","") ;
    72. }
    73. /**
    74. * 判断该 接口是否需要token
    75. * @param exchange
    76. * @return
    77. */
    78. private boolean isRequireToken(ServerWebExchange exchange) {
    79. String path = exchange.getRequest().getURI().getPath();
    80. if(noRequireTokenUris.contains(path)){
    81. return false ; // 不需要token
    82. }
    83. return Boolean.TRUE ;
    84. }
    85. /**
    86. * 拦截器的顺序
    87. * @return
    88. */
    89. @Override
    90. public int getOrder() {
    91. return 0;
    92. }
    93. }

    controller:

    1. @RestController
    2. public class GatewayFlowRulesController {
    3. /**
    4. * 获取当前系统的限流策略
    5. */
    6. @GetMapping("/gw/flow/rules")
    7. public Set<GatewayFlowRule> getCurrentGatewayFlowRules(){
    8. return GatewayRuleManager.getRules() ;
    9. }
    10. /**
    11. * 获取我定义的api分组
    12. */
    13. @GetMapping("/gw/api/groups")
    14. public Set<ApiDefinition> getApiGroups(){
    15. return GatewayApiDefinitionManager.getApiDefinitions() ;
    16. }
    17. }


    启动类:

    1. @SpringBootApplication
    2. @EnableDiscoveryClient //注册到nacos
    3. public class GatewayServerApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(GatewayServerApplication.class ,args) ;
    6. }
    7. }


    在启动的时候报错:
    image.png
    百度一直说是因为gateway里有webfull包。把这个webfux包排除就好。
    终于启动好。弄了很久

    image.png

    我最后在pom.xml上做的更改: 这样是能启动成功的。

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>coin-iaas</artifactId>
    7. <groupId>com.bjsxt</groupId>
    8. <version>1.0</version>
    9. </parent>
    10. <modelVersion>4.0.0</modelVersion>
    11. //之前一直好多包都下不下来,在那个右侧的maven那里报红。加上下面这段代码就好了
    12. //主要是 <url>http://maven.apache.org</url>, 我在别的项目有看到
    13. <groupId>com.bjsxt</groupId>
    14. <artifactId>gateway-server</artifactId>
    15. <version>1.0-SNAPSHOT</version>
    16. <name>culture-cloud-gateway</name>
    17. <url>http://maven.apache.org</url>
    18. <dependencies>
    19. //这个依赖我注释掉了。因为spring-cloud-starter-alibaba-nacos-discovery里有引入这个依赖
    20. <!-- <dependency>
    21. <groupId>org.springframework.cloud</groupId>
    22. <artifactId>spring-cloud-starter-gateway</artifactId>
    23. <exclusions>
    24. <exclusion>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>>spring-boot-starter-webflux</artifactId>
    27. </exclusion>
    28. </exclusions>
    29. </dependency>-->
    30. <dependency>
    31. <groupId>com.alibaba.cloud</groupId>
    32. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    33. <exclusions>
    34. <exclusion>
    35. <groupId>org.springframework.boot</groupId>
    36. <artifactId>>spring-boot-starter-webflux</artifactId>
    37. </exclusion>
    38. </exclusions>
    39. </dependency>
    40. <dependency>
    41. <groupId>com.alibaba.cloud</groupId>
    42. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    43. </dependency>
    44. <dependency>
    45. <groupId>com.alibaba.cloud</groupId>
    46. <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    47. </dependency>
    48. <dependency>
    49. <groupId>com.alibaba.csp</groupId>
    50. <artifactId>sentinel-datasource-nacos</artifactId>
    51. </dependency>
    52. <dependency>
    53. <groupId>org.springframework.boot</groupId>
    54. <artifactId>spring-boot-starter-data-redis</artifactId>
    55. </dependency>
    56. <dependency>
    57. <groupId>com.alibaba.csp</groupId>
    58. <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    59. <exclusions>
    60. <exclusion>
    61. <groupId>org.springframework.boot</groupId>
    62. <artifactId>>spring-boot-starter-webflux</artifactId>
    63. </exclusion>
    64. </exclusions>
    65. </dependency>
    66. <dependency>
    67. <groupId>org.springframework.boot</groupId>
    68. <artifactId>spring-boot-starter-websocket</artifactId>
    69. <exclusions>
    70. <exclusion>
    71. <groupId>org.springframework.boot</groupId>
    72. <artifactId>>spring-boot-starter-web</artifactId>
    73. </exclusion>
    74. </exclusions>
    75. </dependency>
    76. </project>

    接下来我想重现一下出错。看到底是哪个地方出的错:
    我把所有排除webflux的地方一个个注释掉启动都能成功。但是如下,把spring-cloud-starter-gateway这个jar包放开,还是不行。报错在如下图:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>coin-iaas</artifactId>
    7. <groupId>com.bjsxt</groupId>
    8. <version>1.0</version>
    9. </parent>
    10. <modelVersion>4.0.0</modelVersion>
    11. <groupId>com.bjsxt</groupId>
    12. <artifactId>gateway-server</artifactId>
    13. <version>1.0-SNAPSHOT</version>
    14. <name>culture-cloud-gateway</name>
    15. <url>http://maven.apache.org</url>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.cloud</groupId>
    19. <artifactId>spring-cloud-starter-gateway</artifactId>
    20. <!-- <exclusions>
    21. <exclusion>
    22. <groupId>org.springframework.boot</groupId>
    23. <artifactId>>spring-boot-starter-webflux</artifactId>
    24. </exclusion>
    25. </exclusions>-->
    26. </dependency>
    27. <dependency>
    28. <groupId>com.alibaba.cloud</groupId>
    29. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    30. <!-- <exclusions>
    31. <exclusion>
    32. <groupId>org.springframework.boot</groupId>
    33. <artifactId>>spring-boot-starter-webflux</artifactId>
    34. </exclusion>
    35. </exclusions>-->
    36. </dependency>
    37. <dependency>
    38. <groupId>com.alibaba.cloud</groupId>
    39. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    40. </dependency>
    41. <dependency>
    42. <groupId>com.alibaba.cloud</groupId>
    43. <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    44. </dependency>
    45. <dependency>
    46. <groupId>com.alibaba.csp</groupId>
    47. <artifactId>sentinel-datasource-nacos</artifactId>
    48. </dependency>
    49. <dependency>
    50. <groupId>org.springframework.boot</groupId>
    51. <artifactId>spring-boot-starter-data-redis</artifactId>
    52. </dependency>
    53. <dependency>
    54. <groupId>com.alibaba.csp</groupId>
    55. <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    56. <!-- <exclusions>
    57. <exclusion>
    58. <groupId>org.springframework.boot</groupId>
    59. <artifactId>>spring-boot-starter-webflux</artifactId>
    60. </exclusion>
    61. </exclusions>-->
    62. </dependency>
    63. <dependency>
    64. <groupId>org.springframework.boot</groupId>
    65. <artifactId>spring-boot-starter-websocket</artifactId>
    66. <!-- <exclusions>
    67. <exclusion>
    68. <groupId>org.springframework.boot</groupId>
    69. <artifactId>>spring-boot-starter-web</artifactId>
    70. </exclusion>
    71. </exclusions>-->
    72. </dependency>
    73. </dependencies>
    74. </project>

    报错图:
    image.png
    image.png

    如下是可以正常启动:把spring-cloud-starter-gateway注释掉。 那引起这个原因其实就是因为spring-cloud-alibaba-sentinel-gateway里有引用spring-cloud-starter-gateway,然后又引用一遍spring-cloud-starter-gateway。重复了。

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>coin-iaas</artifactId>
    7. <groupId>com.bjsxt</groupId>
    8. <version>1.0</version>
    9. </parent>
    10. <modelVersion>4.0.0</modelVersion>
    11. <groupId>com.bjsxt</groupId>
    12. <artifactId>gateway-server</artifactId>
    13. <version>1.0-SNAPSHOT</version>
    14. <name>culture-cloud-gateway</name>
    15. <url>http://maven.apache.org</url>
    16. <dependencies>
    17. <!-- <dependency>
    18. <groupId>org.springframework.cloud</groupId>
    19. <artifactId>spring-cloud-starter-gateway</artifactId>
    20. <exclusions>
    21. <exclusion>
    22. <groupId>org.springframework.boot</groupId>
    23. <artifactId>>spring-boot-starter-webflux</artifactId>
    24. </exclusion>
    25. </exclusions>
    26. </dependency>-->
    27. <dependency>
    28. <groupId>com.alibaba.cloud</groupId>
    29. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    30. <!-- <exclusions>
    31. <exclusion>
    32. <groupId>org.springframework.boot</groupId>
    33. <artifactId>>spring-boot-starter-webflux</artifactId>
    34. </exclusion>
    35. </exclusions>-->
    36. </dependency>
    37. <dependency>
    38. <groupId>com.alibaba.cloud</groupId>
    39. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    40. </dependency>
    41. <dependency>
    42. <groupId>com.alibaba.cloud</groupId>
    43. <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    44. </dependency>
    45. <dependency>
    46. <groupId>com.alibaba.csp</groupId>
    47. <artifactId>sentinel-datasource-nacos</artifactId>
    48. </dependency>
    49. <dependency>
    50. <groupId>org.springframework.boot</groupId>
    51. <artifactId>spring-boot-starter-data-redis</artifactId>
    52. </dependency>
    53. <dependency>
    54. <groupId>com.alibaba.csp</groupId>
    55. <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    56. <!-- <exclusions>
    57. <exclusion>
    58. <groupId>org.springframework.boot</groupId>
    59. <artifactId>>spring-boot-starter-webflux</artifactId>
    60. </exclusion>
    61. </exclusions>-->
    62. </dependency>
    63. <dependency>
    64. <groupId>org.springframework.boot</groupId>
    65. <artifactId>spring-boot-starter-websocket</artifactId>
    66. <!-- <exclusions>
    67. <exclusion>
    68. <groupId>org.springframework.boot</groupId>
    69. <artifactId>>spring-boot-starter-web</artifactId>
    70. </exclusion>
    71. </exclusions>-->
    72. </dependency>
    73. </dependencies>
    74. </project>

    image.png