SpringCloud07:zuul路由网关

本系列对应的是尚硅谷周阳Spring Cloud的思维导图整理的笔记,用来方便自己后面的知识点回顾。分别以每个知识点作为一篇文章详细讲述。

知识点传送门:
项目源码

  • SpringCloud01:微服务概述与SpringCloud
  • SpringCloud02:Rest微服务构建案例工程模块
  • SpringCloud03:Eureka服务注册与发现
  • SpringCloud04:Ribbon负载均衡
  • SpringCloud05:Feign负载均衡
  • SpringCloud06:Hystrix断路器
  • SpringCloud07:zuul路由网关
  • SpringCloud08:SpringCloud Config分布式配置中心

一、概述

1.是什么

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

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

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

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

官网资料

二、路由基本配置

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

2.修改POM文件




  4.0.0


  
   com.atguigu.springcloud
   microservicecloud
   0.0.1-SNAPSHOT
  


  microservicecloud-zuul-gateway-9527


  
   
   
     org.springframework.cloud
     spring-cloud-starter-zuul
    
   
     org.springframework.cloud
     spring-cloud-starter-eureka
   
   
   
     org.springframework.boot
     spring-boot-starter-actuator
   
   
   
     org.springframework.cloud
     spring-cloud-starter-hystrix
   
   
     org.springframework.cloud
     spring-cloud-starter-config
   
   
   
     com.atguigu.springcloud
     microservicecloud-api
     ${project.version}
   
   
     org.springframework.boot
     spring-boot-starter-jetty
   
   
     org.springframework.boot
     spring-boot-starter-web
   
   
     org.springframework.boot
     spring-boot-starter-test
   
   
   
     org.springframework
     springloaded
   
   
     org.springframework.boot
     spring-boot-devtools
   
  

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$

4.hosts修改

127.0.0.1 myzuul.com

5.主启动类

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);
  }
}

6.启动

  1. 三个eureka集群
  2. 一个服务提供类microservicecloud-provider-dept-8001
  3. 一个路由

7. 测试

不用路由: http://localhost:8001/dept/get/2
启用路由: http://myzuul.com:9527/microservicecloud-dept/dept/get/2

三、路由访问映射规则

1.工程microservicecloud-zuul-gateway-9527

2.代理名称

问题:
路由访问OK:http://myzuul.com:9527/mydept/dept/get/1
原路径访问OK:http://myzuul.com:9527/microservicecloud-dept/dept/get/2

3.原真实服务名忽略

修改YML文件

单个服务

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

多个服务

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

4.设置统一公共前缀

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

访问地址 http://myzuul.com:9527/atguigu/mydept/dept/get/1

5.最后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$
 

你可能感兴趣的:(SpringCloud07:zuul路由网关)