您现在的位置是:首页 >技术杂谈 >【springcloud开发教程】路由网关——zuul网站首页技术杂谈
【springcloud开发教程】路由网关——zuul
简介【springcloud开发教程】路由网关——zuul
官方资料:https://github.com/Netflix/zuul/
什么是Zuul?
Zuul包含了两个主要的功能:路由和过滤
路由功能将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础。Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他服务的消息,使得以后的访问微服务都是通过Zuul跳转后获得。
详细步骤
1.新建springcloud-zuul模块,并导入依赖
<dependencies>
<!--导入zuul依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--Hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--dashboard依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboar</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--Ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--Eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--实体类+web-->
<dependency>
<groupId>com.haust</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
2.配置application.yml
server:
port: 9527
spring:
application:
name: springcloud-zuul
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: zuul9527.com
prefer-ip-address: true
#info
info:
app.name: com.diviner
company.name: oj.diivner.com
zuul:
routes:
mydept.serviceid: springcloud-provider-dept
mydept.path: /mydept/**
ignored-services: springcloud-provider-dept #不能在使用原路径访问 "*" 隐藏全部服务
prefix: /diviner # 设置公共前缀
3.编写主启动类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableZuulProxy //
public class ZuulApplication_9527 {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication_9527.class,args);
}
}
4.启动7001、hystrix_8001、zuul_9527、80进行测试
可以看到Zull路由网关被注册到Eureka注册中心
通过路由网关进行访问:http://www.diviner.com:9527/springcloud-provider-dept/dept/get/1
配置ignored—services:隐藏掉该服务
通过配置的路径进行访问:http://www.diviner.com:9527/mydept/dept/get/1
配置prefix: /deviner
通过配置的路径进行访问:http://www.diviner.com:9527/diviner/mydept/dept/get/1
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。