Zuul:Zuul 是在雲平臺上提供動態路由,監控,彈性,安全等邊緣服務的框架。Zuul 至關因而設備和 Netflix 流應用的 Web 網站後端全部請求的前門。java
在微服務盛行的時代,客戶端與系統之間的交互基本上以下圖:git
客戶端想要請求到服務,首先請求到zuul網關,而後zuul網管將請求分發到對應的服務去。web
新建一個springcloud_zuul(8766),pom文件加入zuul依賴,pom文件完整代碼以下:spring
<?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>
<groupId>com.dalaoyang</groupId>
<artifactId>springcloud_zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_zuul</name>
<description>springcloud_zuul</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
而後在啓動類上添加@EnableZuulProxy啓動zuul,完整代碼以下:apache
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class SpringcloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudZuulApplication.class, args);
}
}
複製代碼
在配置文件加入以下配置:json
##端口號
server.port=8766
##服務名稱
spring.application.name=service-zuul
##註冊中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/
##zuul路由轉發 service-a轉發到service_ribbon
zuul.routes.service-a.path=/service-a/**
zuul.routes.service-a.service-id=service_ribbon
##zuul路由轉發 service-b轉發到service
zuul.routes.service-b.path=/service-b/**
zuul.routes.service-b.service-id=service
複製代碼
而後分別啓動以前service(8762),service_ribbon(8764)和springcloud_zuul(8766)。後端
先看一下eureka.dalaoyang.cn,以下:瀏覽器
而後在瀏覽器上分別訪問http://localhost:8766/service-a,以下安全
http://localhost:8766/service-b,以下bash
zuul網關的簡單配置到這裏基本上完成了。
而後介紹一個zuul的過濾器,新建一個過濾器的類RequestLogFilter,這個類須要繼承ZuulFilter,須要實現幾個方法。
filterType:這個方法返回過濾器的類型,pre(路由前執行),route(路由中執行),post(路由後執行),error(發生錯誤後執行)
filterOrder:返回一個int來指定過濾器的執行順序
shouldFilter:返回boolean值來決定過濾器是否執行,true執行,false不執行。
run:過濾器要執行的操做。
本項目中過濾器代碼以下:
package com.dalaoyang.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
/**
* @author dalaoyang
* @Description
* @project springcloud_learn
* @package com.dalaoyang.filter
* @email yangyang@dalaoyang.cn
* @date 2018/4/22
*/
public class RequestLogFilter extends ZuulFilter {
Logger logger = LoggerFactory.getLogger(RequestLogFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public Object run() {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest httpServletRequest = requestContext.getRequest();
logger.info("請求路徑:"+httpServletRequest.getRequestURL().toString());
return null;
}
}
複製代碼
修改一下啓動類:
package com.dalaoyang;
import com.dalaoyang.filter.RequestLogFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableZuulProxy
public class SpringcloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudZuulApplication.class, args);
}
@Bean
public RequestLogFilter requestLogFilter(){
return new RequestLogFilter();
}
}
複製代碼
在重啓項目,訪問http://localhost:8766/service-a,而後看控制檯能夠看到以下圖:
有時會出現這樣的狀況,好比本文的service實例down了,以下圖這種狀況
那麼在訪問,就會沒法轉發,其實zuul也提供了失敗回調的方法。 新建一個ServiceFallback類實現ZuulFallbackProvider接口,須要重寫2個方法。
其中getRoute方法中能夠指定爲哪一個微服務回退,*爲全部服務。
代碼以下:
package com.dalaoyang.fallback;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
/**
* @author dalaoyang
* @Description
* @project springcloud_learn
* @package com.dalaoyang.fallback
* @email yangyang@dalaoyang.cn
* @date 2018/4/22
*/
@Component
public class ServiceFallback implements ZuulFallbackProvider {
@Override
public String getRoute() {
return "*";
}
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return this.getStatusCode().value();
}
@Override
public String getStatusText() throws IOException {
return this.getStatusCode().getReasonPhrase();
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("當前訪問服務不可用!".getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
MediaType mediaType = new MediaType("application", "json",
Charset.forName("UTF-8"));
httpHeaders.setContentType(mediaType);
return httpHeaders;
}
};
}
}
複製代碼
重啓項目,訪問http://localhost:8766/service-a以下圖所示:
源碼下載 :大老楊碼雲
我的網站:www.dalaoyang.cn