Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
1、新建microservicecloud-zuul-gateway-9527工程
2、引入依赖
<dependencies>
<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>
<!-- eureka注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- config服务配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--hystrix服务熔断-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- hystrix-dashboard相关: actuator监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- zuul路由网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies>
3、yml配置
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true
info:
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}
zuul:
prefix: /atguigu #设置统一公共前缀
ignored-services: "*" #禁止使用服务名访问
routes: #路由映射
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
4、找到C:\Windows\System32\drivers\etc路径下的hosts文件。添加域名映射
127.0.0.1 myzuul.com
5、在主启动类上添加@EnableZuulProxy注解
/**
* Zuul包含了对请求的路由和过滤两个最主要的功能:
* 1、路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。
* 2、过滤功能负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
* Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获取其他微服务的消息,以后访问微服务都是通过Zuul跳转后获得。
*/
@SpringBootApplication
@EnableZuulProxy //开启Zuul路由网关
public class ZuulGateWay9527_App {
public static void main(String[] args) {
SpringApplication.run(ZuulGateWay9527_App.class, args);
}
}
6、先启动3个eureka集群后,启动部门提供者8001,再启动zuul-gateway工程。
http://myzuul.com:9527/atguigu/mydept/dept/get/2