maven配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.0.0.RELEASE</version>
<exclusions>
<!-- 需要去除这个依赖,不然启动会报错 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
</dependencies>
yml配置
server:
port: 80
####服务网关名称
spring:
application:
name: mayikt-gateway
cloud:
gateway:
###路由策略
routes:
###根据我们的服务名称查找地址实现调用
- id: member
####转发feign-member,注册到nacos的服务
uri: lb://feign-member/
filters:
- StripPrefix=1
###匹配规则
predicates:
- Path=/member/**
discovery:
locator:
###允许通过注册中心获取地址调用,默认是false
enabled: true
nacos:
discovery:
server-addr: 127.0.0.1:8848
启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
@SpringBootApplication
public class AppGateWay {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class);
}
}
请求地址
注:这里需要开启feign-member服务,并且注册到nacos中
http://127.0.0.1/member/memberIndex?id=2