SpringCloud笔记06 Gateway服务网关
coconutnut

https://blog.csdn.net/ThinkWon/article/details/103757927

Gateway是在Spring生态系统之上构建的API网关服务,旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能, 例如:熔断、限流、重试等

集成Hystrix的断路器功能和Spring Cloud服务发现功能

创建api-gateway模块

添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

不需要注册?

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server:
port: 9201

service-url:
user-service: http://localhost:8201

spring:
cloud:
gateway:
routes:
# 路由的ID
- id: path_route
# 匹配后路由地址
uri: ${service-url.user-service}/user/{id}
predicates:
# 断言,路径相匹配的进行路由
- Path=/user/{id}

启动consul-server、user-service和api-gateway

测试 http://localhost:9201/user/1

Route Predicate 的使用

  • After Route Predicate : 在指定时间之后的请求会匹配该路由

  • Before Route Predicate : 在指定时间之前的请求会匹配该路由

  • Between Route Predicate : 在指定时间区间内的请求会匹配该路由

  • Cookie Route Predicate : 带有指定Cookie的请求会匹配该路由

    1
    username=jourwon
  • Header Route Predicate : 带有指定请求头的请求会匹配该路由

    1
    X-Request-Id:123
  • Host Route Predicate : 带有指定Host的请求会匹配该路由

    1
    Host:www.jourwon.com
  • Method Route Predicate : 发送指定方法的请求会匹配该路由

    1
    GET
  • Path Route Predicate : 发送指定路径的请求会匹配该路由

    1
    /user/1
  • Query Route Predicate : 带指定查询参数的请求可以匹配该路由

    1
    username=jourwon
  • RemoteAddr Route Predicate : 从指定远程地址发起的请求可以匹配该路由

  • Weight Route Predicate : 使用权重来路由相应请求

    如80%的请求路由到localhost:8201,20%路由到localhost:8202

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    spring:
    cloud:
    gateway:
    routes:
    - id: weight_high
    uri: http://localhost:8201
    predicates:
    - Weight=group1, 8
    - id: weight_low
    uri: http://localhost:8202
    predicates:
    - Weight=group1, 2

Route Filter的使用

可用于修改进入的HTTP请求和返回的HTTP响应,只能指定路由进行使用

  • AddRequestParameter GatewayFilter : 给请求添加参数的过滤器
  • StripPrefix GatewayFilter : 对指定数量的路径前缀进行去除的过滤器
  • PrefixPath GatewayFilter : 与StripPrefix过滤器相反,会对原有路径进行增加操作的过滤器
  • Retry GatewayFilter : 对路由请求进行重试的过滤器,根据路由请求返回的HTTP状态码来确定是否进行重试

Hystrix GatewayFilter

将断路器功能添加到网关路由中

需添加Hystrix的相关依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

添加相关服务降级的处理类FallbackController

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
public class FallbackController {

@GetMapping("/fallback")
public Object fallback() {
Map<String,Object> result = new HashMap<>();
result.put("data",null);
result.put("message","Get request fallback!");
result.put("code",500);
return result;
}

}

配置服务降级

1
2
3
4
5
filters:
- name: Hystrix
args:
name: fallbackcmd
fallback-uri: forward:/fallback

测试(先重启api-gateway)

关闭user-service,调用http://localhost:9201/user/1

发生了降级

RequestRateLimiter GatewayFilter

可以用于限流

添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

添加限流策略的配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration
public class RedisRateLimiterConfig {

@Bean
public KeyResolver userKeyResolver() {
// 根据请求参数中的username进行限流
return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("username"));
}

@Primary
@Bean
public KeyResolver ipKeyResolver() {
// 根据访问IP进行限流
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}

}

添加Redis和RequestRateLimiter的配置

1
2
3
4
5
6
7
8
- name: RequestRateLimiter
args:
# 每秒允许处理的请求数量
redis-rate-limiter.replenishRate: 1
# 每秒最大处理的请求数量
redis-rate-limiter.burstCapacity: 2
# 限流策略,对应策略的Bean
key-resolver: "#{@ipKeyResolver}"

测试

启动服务,多次请求 http://localhost:9201/user/1

报错429

DEBUG:启动失败

报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of method requestRateLimiterGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a single bean, but 2 were found:
- userKeyResolver: defined by method 'userKeyResolver' in class path resource [com/coconutnut/apigateway/config/RedisRateLimiterConfig.class]
- ipKeyResolver: defined by method 'ipKeyResolver' in class path resource [com/coconutnut/apigateway/config/RedisRateLimiterConfig.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed


Process finished with exit code 1

根据提示加上@Primary,解决

DEBUG:Redis连接失败

访问http://localhost:9201/user/1时数据正常,但控制台报错

1
2
3
4
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1199) ~[spring-data-redis-2.2.3.RELEASE.jar:2.2.3.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1178) ~[spring-data-redis-2.2.3.RELEASE.jar:2.2.3.RELEASE]
...

下载安装

http://www.redis.cn/download.html

启动

1
redis-5.0.5 % src/redis-server

好可爱🙀

结合注册中心使用

(弃)

教程给的是eureka,这里尝试改成consul

加依赖

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

改配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring:
cloud:
application:
name: api-gateway
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
gateway:
discovery:
locator:
#开启从注册中心动态创建路由的功能
enabled: true
#使用小写服务名,默认是大写
lower-case-service-id: true

DEBUG:启动失败

报错

1
2
3
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webHandler' defined in class path resource [org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration$EnableWebFluxConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webEndpointReactiveHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping]: Factory method 'webEndpointReactiveHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration': Unsatisfied dependency expressed through field 'registration'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration]: Factory method 'consulRegistration' threw exception; nested exception is java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: 9201
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]

发现启动类忘加注释了,加上先

1
@EnableDiscoveryClient

看下依赖,没有冲突

但是确实是加了consul的依赖之后才报错的

去掉它的依赖就可以正常启动

https://blog.csdn.net/jiazhiyuan0/article/details/84033595

https://blog.csdn.net/maduo_duo/article/details/84866137

确实可能是有冲突

现在的Spring Boot版本是2.2.2.RELEASE,Spring Cloud版本是2.2.0.RELEASE

尝试1

把Spring Boot降到2.2.0

并不行

尝试2

https://github.com/spring-cloud/spring-cloud-gateway/issues/319

Finchley is not compatible with boot 2.1.0. Either downgrade to boot 2.0.6 or use Greenwich.M3

这里用的是Hoxton版本

找下文档

https://spring.io/blog/2019/11/28/spring-cloud-hoxton-released

Consul和Gateway的版本也是2.2.0.RELEASE

https://spring.io/projects/spring-cloud#overview

Hoxton对应的Boot Version是2.2.x

应该没错

好几个地方看到说是spring-boot-starter-web的问题

搜一下,确实引用了

把它排除

1
2
3
4
5
6
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</exclusion>
</exclusions>

并不行

尝试3

找找Spring Cloud Hoxton + Gatway + Consul的教程

https://piotrminkowski.com/2019/11/06/microservices-with-spring-boot-spring-cloud-gateway-and-consul-cluster/

这个教程的依赖是

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

已有

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

把之前spring-boot-starter的依赖改成spring-boot-starter-web

1
2
3
4
5
6
7
8
9
<!--        <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

还是没启动成功,但是报错变成了

1
2
3
4
5
2020-04-25 12:09:53.083 ERROR 4989 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
...

https://blog.csdn.net/fly_west/article/details/101229871?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

还是spring-boot-starter-web冲突的问题

现在再把gateway里的webflux排除试试

1
2
3
4
5
2020-04-25 12:12:38.331 ERROR 5002 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
...

尝试4

依赖全改得和刚那个教程一样试试

hystrix和redis相关的都注释掉

版本

1
2
3
4
5
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
1
<spring-cloud.version>Hoxton.RC1</spring-cloud.version>

依赖

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

还是崩了

发现一个问题,依赖的最下面怎么还有个

1
2
3
4
5
6
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>

估计是创建的时候导入的,删掉

重启

尝试5

还是把版本改回Spring Boot 2.2.0和Spring Cloud Hoxton

现在的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-webflux</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

现在的报错

1
2
3
4
5
2020-04-25 12:32:36.436 ERROR 5170 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
...

刚才看依赖图发现gateway依赖的是webflux

这里依赖的是web

如果改成webflux?

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

报错

1
2
3
4
5
2020-04-25 12:33:57.502 ERROR 5182 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webHandler' defined in class path resource [org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration$EnableWebFluxConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webEndpointReactiveHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping]: Factory method 'webEndpointReactiveHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration': Unsatisfied dependency expressed through field 'registration'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration]: Factory method 'consulRegistration' threw exception; nested exception is java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: 9201
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
...

所以应该是,启动需要web,否则报BeanCreationException

而gateway依赖了webflux,和web冲突,报ApplicationContextException

再把exclusion加上

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-webflux</artifactId>
</exclusion>
</exclusions>
</dependency>
1
2
3
4
5
2020-04-25 12:35:30.492 ERROR 5188 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webHandler' defined in class path resource [org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration$EnableWebFluxConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webEndpointReactiveHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping]: Factory method 'webEndpointReactiveHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration': Unsatisfied dependency expressed through field 'registration'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration]: Factory method 'consulRegistration' threw exception; nested exception is java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: 9201
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
...

把exclusion去掉,webflux删了

1
2
3
4
5
2020-04-25 12:37:30.419 ERROR 5199 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webHandler' defined in class path resource [org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration$EnableWebFluxConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webEndpointReactiveHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping]: Factory method 'webEndpointReactiveHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration': Unsatisfied dependency expressed through field 'registration'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration]: Factory method 'consulRegistration' threw exception; nested exception is java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: 9201
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
...

尝试6

https://github.com/spring-cloud/spring-cloud-gateway/issues/319

之前这个issue里面有人说

spring cloud gateway is not compatible with spring mvc and servlet containers, what you get when you depend on spring-boot-starter-web.

试试把spring-boot-starter-web里的spring mvc和servlet排除了?

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>

不行

https://github.com/spring-cloud/spring-cloud-gateway/issues/1004

spring-cloud-gateway-mvc is not compatible with gateway core. You need to use spring-cloud-gateway-webflux.

https://github.com/spring-cloud/spring-cloud-gateway/issues/1473

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

现在就只有这几个依赖,为什么还是启动不了呢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

难道actuator里面也有web?

把它注释掉

报错变成

1
2
3
4
5
2020-04-25 13:17:36.860 ERROR 5310 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration': Unsatisfied dependency expressed through field 'autoServiceRegistration'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'consulAutoServiceRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'consulAutoServiceRegistration' parameter 3; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration]: Factory method 'consulRegistration' threw exception; nested exception is java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: 9201
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
...

https://github.com/spring-cloud/spring-cloud-gateway/issues/802

Gateway is thought to be used as a separate technical component in your architecture. It usually does not make sense to add a gateway dependency directly to your business code.

适合单独使用…

单独使用…

单独…

fine

暂时就不结合注册中心了

以后有需要再说

再见