聊一下微服务框架SpringCloud和使用-zuul路由网关 [第六篇]

聊一下微服务框架SpringCloud和使用-zuul路由网关 [第六篇]

Scroll Down

完整源码-仅供参考

https://github.com/ChannD/SpringCloudDemo

1. 概述

1. 是什么

  • Zuul包含了对请求的路由和过滤两个最主要的功能:
  • 其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
  • 对无服务的名字提供一种相对而言的保护.
  • 注意:Zuul服务最终还是会注册进Eureka
  • [总结]提供=代理+路由+过滤三大功能

2. 能干嘛

  • 路由
  • 过滤

3. Github官网资料


2. 路由基本配置

  1. 新建Module模块microservicecloud-zuul-gateway-9527
  2. POM [完整代码]
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>microservicecloud</artifactId>
        <groupId>com.chann.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <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.chann.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>
  1. YML
  2. hosts修改
  3. 主启动类[创建 Zuul_9527_StartSpringCloudApp]
package com.chann.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);
    }
}

  1. 启动
  • 三个eureka集群
  • 一个服务提供类microservicecloud-provider-dept-8001
  • 一个路由
  1. 测试
// 20191010225211
// http://localhost:8001/dept/get/2

{
  "deptno": 2,
  "dname": "人事部",
  "db_source": "cloudDB01"
}
// 20191010225304
// http://myzuul.com:9527/microservicecloud-dept/dept/get/2

{
  "deptno": 2,
  "dname": "人事部",
  "db_source": "cloudDB01"
}

3. 路由访问映射规则

1. 工程microservicecloud-zuul-gateway-9527

2. 代理名称

  • YML[新增配置]
zuul: 
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

3. 原真实服务名忽略

  • YML[修改yml]
 
zuul: 
  ignored-services: microservicecloud-dept 
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
  • 单个具体,多个可以用"*"
ignored-services: "*"

4. 设置统一公共前缀

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

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

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

info:
  app.name: chann-microservicecloud
  company.name: www.jiangdalong.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$