SpringCloud学到刚好够用之六-路由网关(zuul).md

Posted by lizhao on 07-09,2019

SpringCloud学到刚好够用之六-路由网关(zuul)

[toc]

总览

spring cloud相关技术栈在现在各个互联网公司的普及率已经很高了,这次去面试,基本面试官都有提到这一块,但是有没有实际接触过,只能遗憾的摇摇头。这里记录下学习过程,将一些踩坑的经验写出来,供大家参考。这个系列只会写到很浅的一部分,如有兴趣深入,可以看"这个系列",将会给大家一个方向哈

基于SpringCloud的Greenwich.SR1,Jdk11,Maven,Idea开发

看的不过瘾

进阶使用

源码分析

简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。

zuul有以下功能:

Authentication Insights Stress Testing Canary Testing Dynamic Routing Service Migration Load Shedding Security Static Response handling Active/Active traffic management

作者:方志朋 来源:CSDN 原文:https://blog.csdn.net/forezp/article/details/69939114 版权声明:本文为博主原创文章,转载请附上博文链接!

准备活动

  1. 一个eureka,pro002-eureka
  2. 一个hi服务,pro002-eureka-test
  3. 随便2个对外提供api的服务,这里使用(pro003-ribbon,pro004-feign)
  4. 一个网关pro006-zuul

路由功能

主要步骤

引入相关包spring-cloud-starter-netflix-zuul

配置文件,路由转发配置zuul: routes:

启动类application添加注解@EnableZuulProxy

上述步骤关键代码及图片

  1. 引入相关包spring-cloud-starter-netflix-zuul
<?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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.lizhaoblog.scl</groupId>
  <artifactId>pro006-zuul</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>pro006-zuul</name>
  <description>zuul project for Spring cloud</description>

  <properties>
    <java.version>11</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</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>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

  1. 配置文件,路由转发配置zuul: routes:
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8767
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: service-ribbon
    api-b:
      path: /api-b/**
      serviceId: service-feign


  1. 启动类application添加注解@EnableZuulProxy
package com.lizhaoblog.scl.pro006zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class Pro006ZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(Pro006ZuulApplication.class, args);
    }

}

测试:访问zuul,看是否进行了分发

主要步骤

运行eureka,hi服务1,ribbon服务,feign服务

打开eureka面板查看服务数

请求http://localhost:8767/api-a/hi?name=lizhao

请求http://localhost:8767/api-b/hi?name=lizhao

上述步骤关键代码及图片

  1. 运行eureka,hi服务1,ribbon服务,feign服务
  2. 打开eureka面板查看服务数
DS Replicas
Instances currently registered with Eureka
Application	AMIs	Availability Zones	Status
SERVICE-FEIGN	n/a (1)	(1)	UP (1) - localhost:service-feign:8765
SERVICE-HI	n/a (1)	(1)	UP (1) - localhost:service-hi:8762
SERVICE-RIBBON	n/a (1)	(1)	UP (1) - localhost:service-ribbon:8764
SERVICE-ZUUL	n/a (1)	(1)	UP (1) - localhost:service-zuul:8767
  1. 请求http://localhost:8767/api-a/hi?name=lizhao
  2. 请求http://localhost:8767/api-b/hi?name=lizhao

这2个请求,有数据出来一般就是转发成功了

hi lizhao,i am from port:8762

总结

广告时间

我是小王,一个喜欢抛转的码人。