学习spring cloud 组件之zuul网关
一句话概括zuul,过滤和筛选请求,统一管理请求地址。
第一步:pom.xml 引入zuul
<!--zuul调用服务--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
我是配合nacos 用的,整体pom.xml配置如下:
<!--服务注册中心nacos--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--FeignClient调用服务--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> <version>10.4.0</version> </dependency> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon</artifactId> <version>2.7.18</version> <scope>runtime</scope> </dependency> <!--zuul调用服务--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
第二步:application.yml添加zuul 需要的配置
server:
port: 9527
#spring 配置
spring:
application:
name: pringcloud-zuul
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
prefix: nacos
discovery:
server-addr: 127.0.0.1:8848
zuul:
routes:
auth:
path: /mydept/** #相当于项目别名
serviceId: pringcloud-provider-dept #表示代理服务名为pringcloud-provider-dept的所有请求
ignored-services: "*" #不能再使用以前的服务名+映射地址访问
prefix: /yang #设置公共的前缀,开启之后,所有的服务访问都必须加上才能访问
第三步:配置启动类开启zuul,由于我集成了nacos ,这里我就是两个开启注解
@SpringBootApplication @EnableZuulProxy //开启zuul @EnableDiscoveryClient //开启nacos public class ZuulApplication_9527 { public static void main(String[] args) { SpringApplication.run(ZuulApplication_9527.class,args); } }
然后启动测试结果如下: