Zuul是路由网关,包含了对请求的路由和过滤两个功能。路由负责将外部请求转发到具体的微服务实例上,统一了外部访问的入口,实现请求校验及服务聚合的基础,Zuul将自己注册为Eureka下的服务,同时同Eureka中获取其他服务的消息。
- 参考之前的博客搭建好服务端、eureka
- 新建zuul项目
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
- 配置
server:
port: 5050
eureka:
client:
service-url:
defaultZone: http://localhost:7010/eureka
instance:
instance-id: zuul
prefer-ip-address: true # 显示ip地址
lease-renewal-interval-in-seconds: 2 # 设置心跳的间隔时间单位秒
lease-expiration-duration-in-seconds: 5 # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance
spring:
application:
name: zuul-gateway
# security:
# user:
# name: admin
# password: 123456
#zuul:
# routes:
# microcloud-provider-users: /users-proxy/**
# ignored-services:
# "*"
# PRODUCT-NOAUTH是我要接口提供者的eureka服务名,path是给它配置的路径
zuul:
routes:
noauth:
path: /noauth/**
serviceId: PRODUCT-NOAUTH
ignored-services:
"*"
# prefix: /aa
- 在启动类上添加注解
@EnableZuulProxy
- 在已经建好的feign接口项目中,新建接口,调用Zuul再去调用PRODUCT-NOAUTH提供的接口
@FeignClient(name = "ZUUL-GATEWAY")
public interface ZuulService {
@RequestMapping("/noauth/por-no-auth/get/{id}")
public String get3(@PathVariable("id") String id);
}
- 将相关项目启动