1.Zuul简介
Zuul包含了对请求的路由和过滤两个最主要的功能。
路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。
过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注:Zuul服务最终还是会注册进Eureka
Zuul提供代理、路由、过滤三大功能。
https://github.com/Netflix/zuul/wiki/Getting-Started |
2.Zuul配置

(1).创建工程
新建Module模块microservicecloud-zuul-gateway-9527


(2).配置pom
[1].修改部分
<!-- zuul路由网关 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> |
[2].完整部分
<?="1.0" encoding="UTF-8"?> <project ="http://maven.apache.org/POM/4.0.0" xsi="http://www.w3.org/2001/ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 > <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-zuul-gateway-9527</artifactId> <dependencies> <!-- zuul路由网关 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- actuator监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- hystrix容错--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 日常标配 --> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
</project> |
(3).applicaiton.yml
server: port: 9527
spring: application: name: microservicecloud-zuul-gateway
eureka: client: service-url: defaultZone: instance: instance-id: gateway-9527.com prefer-ip-address: true
info: app.name: hosystem-microcloud company.name: www.hosystem.com build.artifactId: $project.artifactId$ build.version: $project.version$ |
(4).修改hosts
127.0.0.1 myzuul.com |

(5).主启动类
创建主启动类Zuul_9527_StartSpringCloudApp,并添加注解@EnableZuulProxy.
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication @EnableZuulProxy public class Zuul_9527_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args); } } |
(6).启动项目
[1].启动eureka7001、eureka7002、eureka7003
启动microservicecloud-eureka-7001、microservicecloud-eureka-7002、microservicecloud-eureka-7003

[2].启动provider8001
启动microservicecloud-provider-dept-8001

[3].启动zuul9527
启动microservicecloud-zuul-gateway-9527

(7).测试
[1].未启用路由
[2].启动路由
|





没有评论:
发表评论