服务网关(zuul)又称路由中心,用来统一访问所有api接口,维护服务。
Spring Cloud Zuul通过与Spring Cloud Eureka的整合,实现了对服务实例的自动化维护,所以在使用服务路由配置的时候,我们不需要向传统路由配置方式那样去指定具体的服务实例地址,只需要通过Ant模式配置文件参数即可
一. 添加maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
二.启动类中添加注解
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
}
}
注意:网关和注册中心配合使用,因此网关也需要注册到注册中心
三.修改application.properties文件
#在注册中心显示的名字
spring.application.name=zuul-server
server.port=8762
#注册中心的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
#启用熔断的开关
feign.hystrix.enabled=true