springcloud-zuul总结
一,依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>二,配置文件
spring.application.name=zuul-gateway
server.port=8080
eureka.client.register-with-eureka=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ps:
可以这么去测试:
(ps:{spring.application.name}表示注册在Eureka中的instance的项目的名字,
当访问的时候,只需要在zuul-gateway的hostname:port下加上其他项目的名字然后在加上其他的参数就可以做负载均衡和网关处理了
)
http://localhost:8080/{spring.application.name}
eg:http://localhost:8080/springcloud_eureka_provider
三,启动类
@SpringBootApplication
@EnableZuulProxy
public class GatewayServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServiceZuulApplication.class, args);
}
}四,网关的默认路由规则(Zuul会代理所有注册到Eureka Server的微服务)
如果后端服务多达十几个的时候,每一个都这样配置也挺麻烦的,spring cloud zuul已经帮我们做了默认配置。
默认情况下,Zuul会代理所有注册到Eureka Server的微服务,并且Zuul的路由规则如下:http://host:port/serviceId/** 会被转发到serviceId对应的微服务;版权声明:本文为ziwenCSDN原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。