ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 使用网关 1、添加依赖 ~~~ <!-- spring cloud gateway 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> ~~~ 2、resources/application.yml配置文件 ``` # --------------网关系统------------------------- server: port: 9000 spring: cloud: gateway: discovery: locator: enabled: true routes: # ------权限系统--------- - id: HJMALL-SERVICE-AUTH uri: lb://hmall-auth predicates: # 路径匹配,以 api 开头,直接配置是不生效的,看 filters 配置 - Path=/api/auth/** filters: # 前缀过滤,默认配置下,我们的请求路径是 http://localhost:9000/myshop-service-consumer-item/** 这时会路由到指定的服务 # 此处配置去掉 1 个路径前缀,再配置上面的 Path=/api/**,就能按照 http://localhost:9000/api/** 的方式访问了 - StripPrefix=2 # ------账户系统--------- - id: HMALL-SERVICE-ACCOUNT uri: lb://hmall-account predicates: - Path=/api/account/** filters: - StripPrefix=2 # ------商品系统--------- - id: HMALL-SERVICE-PRODUCT uri: lb://hmall-product predicates: - Path=/api/product/** filters: - StripPrefix=2 # ------商户系统--------- - id: HMALL-SERVICE-MECHANT uri: lb://hmall-merchant predicates: - Path=/api/merchant/** filters: - StripPrefix=2 ``` 3、网关启动类 ``` @EnableDiscoveryClient @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class GatewayJwtApplication { public static void main(String[] args) { SpringApplication.run(GatewayJwtApplication.class, args); System.out.println("GatewayJwtApplication启动完成.........................."); } /** * 权限过滤器器 */ @Bean public AuthFilter gatewayFilter(){ return new AuthFilter(); } } ``` >[success] 提示 > 目前已经存在hmall-service-gateway网关服务,用于路由转发、异常处理、限流、降级、接口、鉴权等等。