小編給大家分享一下Spring Cloud中Zuul如何實現(xiàn)API網(wǎng)關(guān)與請求過濾,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
因為努力和真誠,有更多的客戶和我們聚集在一起,為了共同目標,創(chuàng)新互聯(lián)公司在工作上密切配合,從創(chuàng)業(yè)型企業(yè)到如今不斷成長,要感謝客戶對我們的高要求,讓我們敢于面對挑戰(zhàn),才有今天的進步與發(fā)展。從網(wǎng)站到重慶小程序開發(fā)公司,軟件開發(fā),重慶APP開發(fā),10余年企業(yè)網(wǎng)站建設(shè)服務經(jīng)驗,為企業(yè)提供網(wǎng)站設(shè)計,網(wǎng)站托管、服務器托管一條龍服務.為企業(yè)提供營銷型網(wǎng)站建設(shè),按需規(guī)劃網(wǎng)站,原創(chuàng)設(shè)計,10余年品質(zhì),值得您的信賴.
簡介
Zuul是Netflix基于JVM的路由器和服務器端負載均衡器。最常用的場景是替換Nginx反向代理后臺微服務供前端UI訪問。
Zuul使用Ribbon來定位一個通過發(fā)現(xiàn)轉(zhuǎn)發(fā)的實例,所有請求都以hystrix命令執(zhí)行,所以故障將顯示在Hystrix指標中。
注:Zuul不包括發(fā)現(xiàn)客戶端,因此對于基于服務ID的路由,需要在類路徑中提供其中一個路由
Zuul是Spring Cloud提供的api網(wǎng)關(guān)和過濾組件,它提供如下功能:
認證
過濾
壓力測試
Canary測試
動態(tài)路由
服務遷移
負載均衡
安全
靜態(tài)請求處理
動態(tài)流量管理
在本教程中,我們將用zuul,把web端的請求/product轉(zhuǎn)發(fā)到對應的產(chǎn)品服務上,并且定義一個pre過濾器來驗證是否經(jīng)過了zuul的轉(zhuǎn)發(fā)。
基礎(chǔ)環(huán)境
JDK 1.8
Maven 3.3.9
IntelliJ 2018.1
Git
項目源碼
點擊這里
創(chuàng)建Zuul服務
在IntelliJ中創(chuàng)建一個maven項目:
cn.zxuqian
apiGateway
然后在pom.xml中添加如下代碼:
4.0.0 cn.zxuqian apiGateway 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE org.springframework.cloud spring-cloud-starter-netflix-zuul org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-dependencies Finchley.M9 pom import 1.8 org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false
需要注意的是,Spring官網(wǎng)的教程給的zuul的artifactId為spring-cloud-starter-zuul,這個是舊版zuul的名字,在我們的Finchley.M9版本中已經(jīng)更名為spring-cloud-starter-netflix-zuul。
添加src/main/resources/bootstrap.yml文件,指定spring.application.name:
spring: application: name: zuul-server
創(chuàng)建cn.zxuqian.Application類:
package cn.zxuqian; import cn.zxuqian.filters.PreFilter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; @EnableZuulProxy @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public PreFilter preFilter() { return new PreFilter(); } }
這里使用了@EnableZuulProxy來指定使用zuul的反向代理,把我們的請求轉(zhuǎn)發(fā)到對應的服務器上。然后啟用了eureka的服務發(fā)現(xiàn)。Zuul默認也會使用Ribbon做負載均衡,所以可以通過eureka發(fā)現(xiàn)已注冊的服務。PreFilter是一個預過濾器,用來在request請求被處理之前進行一些操作,它的代碼如下:
package cn.zxuqian.filters; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; public class PreFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(PreFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 1; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s 方式請求 %s", request.getMethod(), request.getRequestURL().toString())); return null; } }
filterType - Zuul內(nèi)置的filter類型有四種,pre, route,post,error,分別代表請求處理前,處理時,處理后和出錯后。
filterOrder - 指定了該過濾器執(zhí)行的順序。
shouldFilter - 是否開啟此過濾器。
run - 過濾器的業(yè)務邏輯。這里只是簡單的log了一下reqeust的請求方式和請求的路徑。
接下來,在我們的配置中心的git倉庫中創(chuàng)建zuul-server.yml文件,并添加如下配置:
server: port: 8083 zuul: routes: products: path: /product/** serviceId: product-service
這里配置了zuul的端口為8083,然后映射所有/product/的請求到我們的product-service服務上。如果不配置serviceId,那么products這個Key就會默認作為ServiceId,而我們的例子中,ServiceId包括了-,所以在下邊顯示指定了ServiceId。配置完成后提交到git。
更新productService
productService的uri做了一點改動,使其更符合rest風格:
@RequestMapping("/list") public String productList() { log.info("Access to /products endpoint"); return "外套,夾克,毛衣,T恤"; }
這里@RequestMapping匹配的路徑改為了/list,之前是/products。
更新web客戶端
在我們的web客戶端的ProductService中添加一個新的方法:
public String productListZuul() { return this.restTemplate.getForObject("http://zuul-server/product/list", String.class); }
這次我們直接請求zuul-server服務,然后由它把我們的請求反射代理到product-service服務。最后在ProductController中添加一個請求處理方法:
@RequestMapping("/product/list") public String productListZuul() { return productService.productListZuul(); }
用來處理/product/list請求,然后調(diào)用ProductService類中的方法。
測試
使用mvn spring-boot:run啟動configServer,registry, zuulServer, productService,web這幾個工程,然后啟動第二個productService,使用SERVER_PORT=8082 spring-boot:run。
訪問幾次http://localhost:8080/product/list,然后除了會在瀏覽器看到返回的結(jié)果,我們還會在zuulServer的命令行窗口中看到如下字樣:
GET 方式請求 http://xuqians-imac:8083/product/list
然后在兩個productService的命令行窗口中,我們還會看到隨機出現(xiàn)的
Access to /products endpoint
說明zuulServer也會自動進行負載均衡。
看完了這篇文章,相信你對“Spring Cloud中Zuul如何實現(xiàn)API網(wǎng)關(guān)與請求過濾”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!