SpringBoot版本:2.7.1
SpringCloud版本:2021.0.3
简单学习了下Gateway的配置
首先导入依赖,下方链接是使用过程中遇到的问题
使用Gateway出现Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--Hystrix-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.10.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.netflix.ribbon/ribbon-loadbalancer -->
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
<version>2.7.18</version>
</dependency>
<!--Eureka 已集成Ribbon、Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!--actuaor 完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
配置文件
server:
port: 9527
spring:
application:
name: springcloud-gateway
cloud:
gateway:
enabled: true
routes:
- id: springcloud_provider_dept #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates: #路由断言,判断请求是否符合规则
- Path=/mydept/dept/** #路径断言,判断路径是否是以/mydept开头,如果是则符合
filters:
- RewritePath=/mydept/(?<segment>/?.*), /$\{
segment}
- id: blog_test
uri: https://www.csdn.net/
predicates:
- Query=url,blog
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: gateway9527.com
prefer-ip-address: true
management:
endpoints:
web:
exposure:
include: "*"
info:
env:
enabled: true
# 暴露端点info
info:
app.name: yl-springcloud
company.name: www.yl.com
build.artifactId: com.yl.springcloud
build.version: 1.0-SNAPSHOT
主启动类
@SpringBootApplication
@EnableEurekaClient
public class ZuulApplication_9527 {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication_9527.class,args);
}
}