欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring cloud之Zuul路由网关

程序员文章站 2022-06-13 21:20:41
...

目录

Zuul概述:是什么

路由基本配置

路由访问映射规则

spring cloud之Zuul路由网关

Zuul概述:是什么

Zuul包含了对请求的路由和过滤两个最主要的功能:

其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.

Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。

    注意:Zuul服务最终还是会注册进Eureka

提供=代理+路由+过滤三大功能

能干嘛:路由+过滤
 

官网资料:https://github.com/Netflix/zuul/wiki/Getting-Started

路由基本配置

新建Module模块springcloud-model-zuul-gateway-9527

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.jiangjy.springcloud</groupId>
    <artifactId>springcloud-model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-model-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.jiangjy.springcloud</groupId>
		<artifactId>springcloud-model-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.xml 文件配置

server:
  port: 9527
  
spring:
  application:
    name: springcloud-model-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

hosts修改

spring cloud之Zuul路由网关

127.0.0.1       myzuul.com

主启动类

package com.jiangjy.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);
	}
}

启动

三个eureka集群

一个服务提供类springcloud-model-provider-dept-8001

一个路由

测试 结果

spring cloud之Zuul路由网关

不用路由: http://localhost:8001/dept/getById/2

测试结果

spring cloud之Zuul路由网关

启用路由: http://myzuul.com:9527/springcloud-model-dept/dept/getById/2

                                   路由网关      真实的微服务地址      result风格查询     

测试结果

spring cloud之Zuul路由网关

路由访问映射规则

工程springcloud-model-zuul-gateway-9527

代理名称

       yml.xml文件配置 添加

       映射前: http://myzuul.com:9527/springcloud-model-dept/dept/getById/2

zuul:
  routes:
    mydept.serviceId: springcloud-model-dept  #将真实的名
    mydept.path: /mydept/**   #做映射  

映射后:http://myzuul.com:9527/mydept/dept/getById/2

spring cloud之Zuul路由网关

此时遇到的问题: 两个访问路径都能成功访问到

       解决方案:如下

原真实服务名忽略

单个忽略使用

zuul:
  ignored-services: springcloud-model-dept  #忽略原真实名(单个)  
  routes: 
    mydept.serviceId: springcloud-model-dept  #将真实的名
    mydept.path: /mydept/**   #做映射   

多个忽略使用

zuul:
  ignored-services: "*"  #忽略原真实名(多个)
  routes: 
    mydept.serviceId: springcloud-model-dept  #将真实的名
    mydept.path: /mydept/**   #做映射

测试结果:

spring cloud之Zuul路由网关

 

spring cloud之Zuul路由网关

设置统一公共前缀

zuul: 
  prefix: /jiangjy    #前缀名称
  #ignored-services: springcloud-model-dept  #忽略原真实名(单个) 
  ignored-services: "*"  #忽略原真实名(多个)
  routes: 
    mydept.serviceId: springcloud-model-dept  #将真实的名
    mydept.path: /mydept/**   #做映射

测试结果:

spring cloud之Zuul路由网关

 

总yml.xml文件配置

server:
  port: 9527
  
spring:
  application:
    name: springcloud-model-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       

zuul: 
  prefix: /jiangjy    #前缀名称
  #ignored-services: springcloud-model-dept  #忽略原真实名(单个) 
  ignored-services: "*"  #忽略原真实名(多个)
  routes: 
    mydept.serviceId: springcloud-model-dept  #将真实的名
    mydept.path: /mydept/**   #做映射