欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringCloud中Zuul如何實現(xiàn)API網(wǎng)關(guān)與請求過濾

小編給大家分享一下Spring Cloud中Zuul如何實現(xiàn)API網(wǎng)關(guān)與請求過濾,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

因為努力和真誠,有更多的客戶和我們聚集在一起,為了共同目標(biāo),創(chuàng)新互聯(lián)公司在工作上密切配合,從創(chuàng)業(yè)型企業(yè)到如今不斷成長,要感謝客戶對我們的高要求,讓我們敢于面對挑戰(zhàn),才有今天的進(jìn)步與發(fā)展。從網(wǎng)站到重慶小程序開發(fā)公司,軟件開發(fā),重慶APP開發(fā),10余年企業(yè)網(wǎng)站建設(shè)服務(wù)經(jīng)驗,為企業(yè)提供網(wǎng)站設(shè)計,網(wǎng)站托管、服務(wù)器托管一條龍服務(wù).為企業(yè)提供營銷型網(wǎng)站建設(shè),按需規(guī)劃網(wǎng)站,原創(chuàng)設(shè)計,10余年品質(zhì),值得您的信賴.

簡介

Zuul是Netflix基于JVM的路由器和服務(wù)器負(fù)載均衡器。最常用的場景是替換Nginx反向代理后臺微服務(wù)供前端UI訪問。

Zuul使用Ribbon來定位一個通過發(fā)現(xiàn)轉(zhuǎn)發(fā)的實例,所有請求都以hystrix命令執(zhí)行,所以故障將顯示在Hystrix指標(biāo)中。

注:Zuul不包括發(fā)現(xiàn)客戶端,因此對于基于服務(wù)ID的路由,需要在類路徑中提供其中一個路由

Zuul是Spring Cloud提供的api網(wǎng)關(guān)和過濾組件,它提供如下功能:

  • 認(rèn)證

  • 過濾

  • 壓力測試

  • Canary測試

  • 動態(tài)路由

  • 服務(wù)遷移

  • 負(fù)載均衡

  • 安全

  • 靜態(tài)請求處理

  • 動態(tài)流量管理

在本教程中,我們將用zuul,把web端的請求/product轉(zhuǎn)發(fā)到對應(yīng)的產(chǎn)品服務(wù)上,并且定義一個pre過濾器來驗證是否經(jīng)過了zuul的轉(zhuǎn)發(fā)。

基礎(chǔ)環(huán)境

  • JDK 1.8

  • Maven 3.3.9

  • IntelliJ 2018.1

  • Git

項目源碼

點擊這里

創(chuàng)建Zuul服務(wù)

在IntelliJ中創(chuàng)建一個maven項目:

  • cn.zxuqian

  • apiGateway

然后在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>

 <groupId>cn.zxuqian</groupId>
 <artifactId>apiGateway</artifactId>
 <version>1.0-SNAPSHOT</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.1.RELEASE</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <!-- name has changed, before: spring-cloud-starter-zuul -->
   <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.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 </dependencies>

 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Finchley.M9</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>

 <properties>
  <java.version>1.8</java.version>
 </properties>


 <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/libs-milestone</url>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
  </repository>
 </repositories>
</project>

需要注意的是,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ā)到對應(yīng)的服務(wù)器上。然后啟用了eureka的服務(wù)發(fā)現(xiàn)。Zuul默認(rèn)也會使用Ribbon做負(fù)載均衡,所以可以通過eureka發(fā)現(xiàn)已注冊的服務(wù)。PreFilter是一個預(yù)過濾器,用來在request請求被處理之前進(jìn)行一些操作,它的代碼如下:

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è)務(wù)邏輯。這里只是簡單的log了一下reqeust的請求方式和請求的路徑。

接下來,在我們的配置中心的git倉庫中創(chuàng)建zuul-server.yml文件,并添加如下配置:

server:
 port: 8083
zuul:
 routes:
 products:
  path: /product/**
  serviceId: product-service

這里配置了zuul的端口為8083,然后映射所有/product/的請求到我們的product-service服務(wù)上。如果不配置serviceId,那么products這個Key就會默認(rèn)作為ServiceId,而我們的例子中,ServiceId包括了-,所以在下邊顯示指定了ServiceId。配置完成后提交到git。

更新productService

productService的uri做了一點改動,使其更符合rest風(fēng)格:

@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服務(wù),然后由它把我們的請求反射代理到product-service服務(wù)。最后在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的命令行窗口中,我們還會看到隨機(jī)出現(xiàn)的

Access to /products endpoint

說明zuulServer也會自動進(jìn)行負(fù)載均衡。

看完了這篇文章,相信你對“Spring Cloud中Zuul如何實現(xiàn)API網(wǎng)關(guān)與請求過濾”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站題目:SpringCloud中Zuul如何實現(xiàn)API網(wǎng)關(guān)與請求過濾
網(wǎng)站URL:http://chinadenli.net/article0/gdoooo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)網(wǎng)站制作、網(wǎng)頁設(shè)計公司、定制開發(fā)、建站公司、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)