Git源码: https://github.com/chenhang666/SpringCloud
1、概述
- 路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口,而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
- Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,
即以后的访问微服务都是通过Zuul跳转后获得。
2、路由基本配置
新增Zuul模块
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chen.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-Zuul-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.chen.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>
application.yml配置文件
server:
port: 9527
spring:
application:
name: microservicecloud-zuul
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
instance:
instance-id: zuul.com #自定义服务名称信息
prefer-ip-address: true #访问路径可以显示IP地址
主启动类
package com.chen.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_SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(Zuul_9527_SpringBootApp.class, args);
}
}
启动eureka集群、生产者微服务、zuul服务
访问测试: http://zuul.com:9527/microservicecloud-dept/dept/list
zuul.com修改hosts文件添加映射
microservicecloud-dept为eureka中注册的微服务名
3、路由访问映射规则
修改application.yml
zuul:
prefix: /wuhan #添加统一的公共前缀
ignored-services: microservicecloud-dept #忽略该服务名称
#ignored-services: "*" 忽略掉所有微服务名
routes: #映射名
dept.serviceId: microservicecloud-dept
dept.path: /hanyang/**
配置文件生效后访问路径变更为http://zuul.com:9527/wuhan/hanyang/dept/list