SpringCloud与微服务Ⅸ --- Zuul路由网关
2024-08-27 13:48:12
一.Zool是什么
Zuul包含了对请求路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
Zuul为我们提供了代理、路由、过滤等三大功能。
https://github.com/Netflix/zuul/wiki
二.路由的基本配置
新建模块microservice-zuul-gateway-9527项目。
pom文件
<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.wang.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent> <artifactId>microservice-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.wang.springcloud</groupId>
<artifactId>microservice-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>
yml配置文件
server:
port: 9527
spring:
application:
name: microservice-zuul-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true
info:
app.name: microservice
company.name: www.gateway.com
build.artifactId: microservice-zuul-gateway-9527
build.version: 1.0-SNAPSHOT
修改host文件
修改域名映射,找到系统的host文件(C:\Windows\System32\Drivers\etc ),新增如下映射关系:
127.0.0.1 myzuul.com
启动类注解:
package com.wang.springcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableZuulProxy
public class ZuulGateWay9527 {
public static void main(String[] args) {
SpringApplication.run(ZuulGateWay9527.class,args);
}
}
启动测试:
启动3个集群,一个服务提供者,一个路由项目。
使用路由访问:http://myzuul.com:9527/microservice-dept/dept/get/2
三.路由访问的映射规则
修改路由的访问路径,隐藏真实的路由,对外暴露一个虚拟的路由,防止泄露微服务的名称等信息。
修改microservice-zuul-gateway-9527项目。
yml修改
zuul:
ignored-services: microservice-dept #隐藏该微服务名称
routes:
mydept.serviceId: microservice-dept
mydept.path: /mydept/**
修改完毕后访问:http://myzuul.com:9527/mydept/dept/get/2
若要禁止掉所有真实微服务名称:
zuul:
ignored-services: "*" #隐藏所有微服务名称
设置统一公共前缀
zuul:
prefix: /wang
配置完成后重启编译然后访问:http://myzuul.com:9527/wang/mydept/dept/get/2
完整的yml配置
server:
port: 9527
spring:
application:
name: microservice-zuul-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true zuul:
prefix: /wang
ignored-services: "*" #忽略真实服务名
routes:
mydept.serviceId: microservice-dept
mydept.path: /mydept/** info:
app.name: microservice
company.name: www.gateway.com
build.artifactId: microservice-zuul-gateway-9527
build.version: 1.0-SNAPSHOT
最新文章
- ";Chinese_PRC_CI_AS"; 和 ";Chinese_PRC_90_CI_AI"; 之间的排序规则冲突问题
- Spark数据传输及ShuffleClient(源码阅读五)
- poj1087 A Plug for UNIX(网络流最大流)
- HDU1102(最小生成树Kruskal)
- Liferay7 BPM门户开发之33: Portlet之间通信的3种方式(session、IPC Render Parameter、IPC Event、Cookies)
- 从苹果apns的feedback服务器获取推送失败的token
- HDU 2082 母函数模板题
- BZOJ1409 : Password
- 设计模式之建造者模式(Builder)
- 简单易用的Rest
- 作为一名JAVA程序员应该有怎样的就业思维
- Node.js入门第一天
- sql执行时间过长,请高手指点!
- DEDECMS首页调用图片集里的多张图片
- VS fopen sprinft ... unsafe 问题
- hiho 1097 最小生成树一&#183;Prim算法 (最小生成树)
- centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡
- stark组件开发之组合搜索高级显示和扩展
- 小程序开发 从简单的 crud 开始
- Spring boot学习1 构建微服务:Spring boot 入门篇