1. 程式人生 > >第五篇: 路由閘道器(zuul)(Greenwich版本)

第五篇: 路由閘道器(zuul)(Greenwich版本)

一、在前面的基礎上新建個module,service-zuul。

 1.1、在父工程下新增該module

 <modules>
        <module>eureka-server</module>
        <module>eureka-client</module>
        <module>service-ribbon</module>
        <module>service-feign</module>
        <module>service-zuul</module>
    </modules>

 1.2、在service.zuul工程下設定與父工程的關係,該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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-zuul</name>
    <description>zuul for Spring Boot</description>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>chapter2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    <!-- zuul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </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>
  <!-- 熱編譯-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        </dependencies>
    </dependencyManagement>

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

1.3、啟動類ServiceZuulApplication配置如下:

package com.example;

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

/**
 * @author xiaobu
 * @EnableZuulProxy 開啟zuul功能
 * <p>
 * 是如果選用的註冊中心是eureka,那麼就推薦@EnableEurekaClient,
 * 如果是其他的註冊中心,那麼推薦使用@EnableDiscoveryClient。
 */
@EnableEurekaClient
@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class ServiceZuulApplication {

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

1.4、配置檔案application.properties內容如下

eureka.client.service-url.defaultZone=http://localhost:8001/eureka/
server.port=8006
#請求路徑和服務名
zuul.routes.xiaobu.path=/xiaobu/**
zuul.routes.xiaobu.service-id=service-ribbon
#請求路徑和服務名
zuul.routes.admin.path=/admin/**
zuul.routes.admin.service-id=service-feign

spring.application.name=service-zuul

#熱部署生效
spring.devtools.restart.enabled=true

這樣路由就起到效果了。

二、zuul還可以起到攔截的作用。

2.1、定義個MyFilter實現ZuulFilter.

package com.example.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.micrometer.core.instrument.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2018/11/7 14:28
 * @description V1.0 zuul過濾器
 */
@Component
public class MyFilter extends ZuulFilter {
    private static Logger logger = LoggerFactory.getLogger(MyFilter.class);
    /**
     * @author xiaobu
     * @date 2018/11/7 14:57
     * @return java.lang.String
     * @descprition pre:路由之前  routing:路由之時 post: 路由之後 error:傳送錯誤呼叫
     * @version 1.0
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * @author xiaobu
     * @date 2018/11/7 15:01
     * @return int
     * @descprition   過濾的順序
     * @version 1.0
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * @author xiaobu
     * @date 2018/11/7 14:59
     * @return boolean
     * @descprition  true表示為需要過濾
     * @version 1.0
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }


    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request=requestContext.getRequest();
        String token=request.getParameter("token");
        //字串替換替換
        logger.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        if(StringUtils.isBlank(token)){
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
            try {
                requestContext.getResponse().setContentType("text/html;charset=UTF-8");
                requestContext.getResponse().getWriter().print("令牌是空的。");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        logger.info("ok");
        return null;
    }
}