原创

Spring Cloud 学习笔记5 zuul实现路由网关

之前的文章Spring Cloud 学习笔记4 Eureka 结合Hystrix实现断路器,这篇记录下路由网关的实现。

本次代码都已经提交在github上,点击这里访问

本篇代码基于上篇文章的代码,见学习笔记4

1.新建zuul server Module

5.png

3.png

2.修改blog2的pom.xml文件 增加zuulserver 的依赖

<modules>
        <module>eurekaserver</module>
        <module>eurekaclient</module>
        <module>ribbon</module>
        <module>feign</module>
        <module>zuulserver</module>
    </modules>

3.修改zuulserver 的pom.xml文件 修改parent节点为

<parent>
    <groupId>cn.djc8</groupId>
    <artifactId>blog2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

修改dependencies节点,增加

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

现在zuulserver的pom.xml为

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.djc8</groupId>
        <artifactId>blog2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>cn.djc8</groupId>
    <artifactId>zuulserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuulserver</name>
    <description>zuulserver</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR7</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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

4.修改ZuulserverApplication.java 增加注解

@EnableZuulProxy
@EnableEurekaClient

5.增加application.yml文件

server:
  port: 7012

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/


spring:
  application:
    name: zuulserver

zuul:
  routes:
    api-a:
      path: /api-ribbon/**
      serviceId: ribbon
    api-b:
      path: /api-feign/**
      serviceId: feign

前面的三个配置(server,eureka,spring)都不用多说了,讲下zuul配置的说明 api-a,指定了path为/api-ribbon/**代表所有的api-ribbon请求都转发到ribbon服务上。api-b一样的道理,请求都转发到feign,这样实现了路由网关的功能。

6.依次启动5个服务,启动顺序:eurekaserver-->eurekaclient-->feign-->ribbon-->zuulserver 在启动之前,先编译下,具体如下:

6.png

7.尝试 访问http://localhost:7012/api-ribbon/welcome?name=cn.djc8 浏览器显示cn.djc8 PORT:7004 访问http://localhost:7012/api-feign/welcome?name=cn.djc8 浏览器显示cn.djc8 PORT:7004 这说明zuul起到了路由的作用

8.zuul过滤器功能 zuul的过滤器简单易用,直接继承对应的ZuulFilter 就行了。 9.创建类zuulserver\src\main\java\cn\djc8\zuulserver\filter\TokenFilter.java

package cn.djc8.zuulserver.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
@Component
public class TokenFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){}

            return null;
        }
        return null;
    }
}

使用@Component让spring自动注入 当token参数为空的时候,会提示token is empty 10.测试 访问http://localhost:7012/api-ribbon/welcome?name=cn.djc8 浏览器显示token is empty 访问http://localhost:7012/api-ribbon/welcome?name=cn.djc8&token=111 浏览器显示cn.djc8 PORT:7004 代表过滤器正常运行

参考文献 1.ZUUL-API网关 2.史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)(Finchley版本)

本文来自:Spring Cloud 学习笔记5 zuul实现路由网关-小码农,转载请保留本条链接,感谢!

温馨提示:
本文最后更新于 2020年08月19日,已超过 1,555 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
该篇文章的评论功能已被站长关闭
本文目录