學習微服務的路由網關zuul——路由轉發和過濾器

zuul具備路由轉發和過濾的功能,相似於nginx的功能。首先新建一個項目service-zuul來實現路由轉發的功能。java

1.路由轉發功能nginx

build.gradle文件web

buildscript {
	ext {
		springBootVersion = '2.0.4.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
	mavenCentral()
}


ext {
	springCloudVersion = 'Finchley.SR1'
}

dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
	compile('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

application.yml文件spring

server:
  port: 8798

spring:
  application:
    name: service-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8791/eureka/

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

主方法api

package com.example.servicezuul;

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 ServiceZuulApplication {

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

啓動該項目,以及service-ribbon,service-feign項目,以及eureka-server項目,以及eureka-client-say-hi項目,此時的eureka註冊中心是這樣:app

此時分別訪問http://localhost:8798/api-a/hi;eclipse

以及http://localhost:8798/api-b/hi;maven

其響應頁面都是:ide

以上結果代表,zuul實現了路由轉發的功能,只需加上註解@EnableZuulProxy以及路由轉發相關策略便可。spring-boot

 

2.過濾功能

在上面項目的基礎上添加一個自定義的過濾組件

MyFilter.java

package com.example.servicezuul;

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;
import java.io.IOException;

@Component
public class MyFilter extends ZuulFilter {
    @Override
    public String filterType() {  //過濾器類型,一共有4種不一樣週期的類型,pre:路由以前;routing:路由之時;post:表明路由以後;error:錯誤時調用
        return "pre";
    }

    @Override
    public int filterOrder() { //過濾器使用順序
        return 0;
    }

    @Override
    public boolean shouldFilter() { //是否容許過濾,爲true,即始終過濾
        return true;
    }

    @Override
    public Object run() throws ZuulException {  //過濾器的具體邏輯,如下的內容大體是,有test參數容許訪問,沒有test參數不容許訪問

        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        Object o = request.getParameter("test");
        if (o == null) {
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
            try {
                requestContext.getResponse().getWriter().write("test is empty");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}

運行結果比較:

 

以上結果代表,過濾器已經產生做用了。

相關文章
相關標籤/搜索