SpringCloud学习笔记(八):Zuul路由网关

概述

是什么?

Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。

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

提供=代理+路由+过滤三大功能
过滤的时候会对服务的真实名字进行保护。
官网资料:
https://github.com/Netflix/zuul/wiki/Getting-Started

路由基本配置

新建Module模块microservicecloud-zuul-gateway-9527

pom文件
新加

      < dependency > 
      < groupId > org.springframework.cloud </ groupId > 
      < artifactId > spring-cloud-starter-eureka </ artifactId > 
    </ dependency > 
    < dependency > 
      < groupId > org.springframework.cloud </ groupId > 
      < artifactId > spring-cloud-starter-zuul </ artifactId > 
    </ dependency > 

完整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.atguigu.springcloud </ groupId > 
   < artifactId > microservicecloud </ artifactId > 
   < version > 0.0.1-SNAPSHOT </ version > 
  </ parent > 


  < 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 > 

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$ 

注意域名修改
127.0.0.1 myzuul.com

主启动类

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

@EnableZuulProxy

启动

三个eureka集群
一个服务提供类microservicecloud-provider-dept-8001
一个路由
SpringCloud学习笔记(八):Zuul路由网关_第1张图片
说明:
将zuul也当做一个服务注册到了rureka中

测试

不用路由
http://localhost:8001/dept/get/2
启用路由
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
发现中间是通过路由这个服务在去访问具体的服务的。

路由访问映射规则

工程microservicecloud-zuul-gateway-9527

修改代理名称

YML文件:
before
http://myzuul.com:9527/ microservicecloud-dept /dept/get/2

zuul:  
  routes:   
    mydept.serviceId:  microservicecloud-dept   //具体的微服务的名字,不是指的zuul这个微服务
    mydept.path:  /mydept/ ** 

after
http://myzuul.com:9527/ mydept /dept/get/1
说明:将原来的microservicecloud-dept 改为了 /mydept/ ** ,进行保护的作用

此时问题

路由访问OK
http://myzuul.com:9527/mydept/dept/get/1
原路径访问OK
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
两个都是没问题的,这不符合我们的需求,我们希望原始的是不能访问的

原真实服务名忽略

YML

   zuul:   
   ignored-services: microservicecloud-dept  
   routes:   
     mydept.serviceId:  microservicecloud-dept 
     mydept.path:  /mydept/ ** 
 

说明:加入了ignored-services: microservicecloud-dept ,原来的真实服务名字不能再被访问了。

单个具体,多个可以用"*"

如果微服务很多的话,不能每次都写一条ignored-services: microservicecloud-dept 把?可以同*来代替所有
yml

ignored-services: "*" 

设置统一公共前缀

YML

zuul:   
  prefix: /atguigu 
   ignored-services:  "*" 
   routes:   
     mydept.serviceId:  microservicecloud-dept 
     mydept.path:  /mydept/ ** 
 

说明: prefix: /atguigu 新加入这个配置即可
新的访问地址: http://myzuul.com:9527/atguigu/mydept/dept/get/1

最后完整的YML

server:   
   port:  9527 
  
spring:   
   application: 
     name:  microservicecloud-zuul-gateway 
  
zuul:  
  prefix: /atguigu 
  ignored-services: "*" 
  routes:  
    mydept.serviceId: microservicecloud-dept 
    mydept.path: /mydept/** 


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$ 
 

你可能感兴趣的:(#,JAVA,------,SpringCloud)