<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Dubbo Gateway基于 Java 的 Dubbo 網(wǎng)關(guān)實現(xiàn)

          聯(lián)合創(chuàng)作 · 2023-09-29 06:52

          Dubbo Gateway是一個基于Java語言的Dubbo網(wǎng)關(guān)實現(xiàn)。基于Dubbo注冊中心的元數(shù)據(jù),通過將HTTP請求轉(zhuǎn)換為Dubbo協(xié)議,泛化調(diào)用的方式返回請求結(jié)果。它具有如下的特性:

          1. 使用簡單,開箱即用,非常適用于一些接口測試場景;
          2. 靈活可拓展。通過Interceptor的實現(xiàn),可非常方便實現(xiàn)熔斷、限流、路由、定制響應(yīng)、用戶授權(quán)等功能。系統(tǒng)也內(nèi)置了部分Interceptor實現(xiàn);
          3. 支持僅調(diào)用聲明為對外開放的Dubbo服務(wù),安全保證。(基于Feature:apache/dubbo#7660 );
          4. 文檔自動化生成?;贘avadoc標(biāo)準(zhǔn)的dubbo對外接口生成系統(tǒng),API寫好即可生成文檔,同時集成了阿里云OSS,全程僅需幾步參數(shù)配置,即可可視化預(yù)覽文檔。

          Requirements

          服務(wù)提供者Dubbo 版本:Dubbo 2.7.14+ 或 Dubbo 3.x

          Java 版本:1.8+

          Guides

          以Springboot項目方式,啟動Dubbo Gateway網(wǎng)關(guān):

          1、添加依賴

          新建Springboot工程,添加Dubbo Gateway的核心依賴:

          <dependency>
          	<groupId>com.kalman03</groupId>
          	<artifactId>gateway-core</artifactId>
          	<version>1.2.0</version>
          </dependency>

          當(dāng)然,還需要添加Dubbo的注冊中心依賴,以Zookeeper為例:

          <dependency>
          	<groupId>org.apache.zookeeper</groupId>
          	<artifactId>zookeeper</artifactId>
          	<version>${zookeeper_version}</version>
          	<exclusions>
          		<exclusion>
          			<groupId>log4j</groupId>
          			<artifactId>log4j</artifactId>
          		</exclusion>
          		<exclusion>
          			<groupId>org.slf4j</groupId>
          			<artifactId>slf4j-log4j12</artifactId>
          		</exclusion>
          	</exclusions>
          </dependency>
          <dependency>
          	<groupId>org.apache.curator</groupId>
          	<artifactId>curator-framework</artifactId>
          	<version>${curator_version}</version>
          </dependency>
          <dependency>
          	<groupId>org.apache.curator</groupId>
          	<artifactId>curator-recipes</artifactId>
          	<version>${curator_version}</version>
          </dependency>

          2、參數(shù)配置

          gateway.netty.server.port=80
          gateway.netty.server.host=127.0.0.1
          gateway.netty.server.connect-timeout=3000
          gateway.netty.business.thread-count=50
          gateway.dubbo.registry.address=zookeeper://127.0.0.1:2181
          gateway.dubbo.openservice=true

          3、啟動服務(wù)

          @SpringBootApplication
          @EnableAutoConfiguration
          public class DubboGatewayTest {
          
          	public static void main(String[] args) {
          		try {
          			SpringApplication.run(DubboGatewayTest.class, args);
          		} catch (Throwable e) {
          			e.printStackTrace();
          		}
          	}
          
              //可選自定義攔截器
          	@Component
          	@Order(100)
          	@InterceptorRule(routeRuleType = RouteRuleType.PATH, excludePatterns = { "/api/**" })
          	class CustomInterceptor implements HandlerInterceptor {
          		@Override
          		public boolean preHandle(GatewayHttpRequest request, GatewayHttpResponse response) throws Exception {
          			System.out.println("preHandle");
          			return true;
          		}
          		@Override
          		public void afterCompletion(GatewayHttpRequest request, GatewayHttpResponse response, Exception ex)
          				throws Exception {
          			System.out.println("afterCompletion");
          		}
          	}
          }

          4、訪問目標(biāo)服務(wù)

          系統(tǒng)內(nèi)置了PATH路由MIX路由,也同時支持自定義路由實現(xiàn)。下面演示如何通過不同的路由規(guī)則訪問對應(yīng)的服務(wù)。

          • PATH路由(推薦)

            PATH路由是系統(tǒng)內(nèi)置默認(rèn)的路由,也是系統(tǒng)推薦的路由規(guī)則,其擁有較多適用性(既可正常調(diào)用,也可以滿足一些第三方系統(tǒng)的調(diào)用要求,比如支付消息回調(diào)等),路由規(guī)則如下

            HTTP URL:

            http(s)://{domain}:{port}/{appName}/{interfaceName}/{method}/{group}/{version}
            

            HTTP Body (payload or form-data):

            {
              "id": 23,
              "username": "testUser"
            }
            
          • MIX路由

            考慮到有些參數(shù)相對于固定不變,且暴露在URL中不盡友善,系統(tǒng)內(nèi)置了一種MIX的路由規(guī)則,將部分路由參數(shù)以Header的參數(shù)形式進行傳遞。

            HTTP URL:

            http(s)://{domain}:{port}/{interfaceName}/{method}
            

            HTTP Header:

            x-app-name={appName}
            x-group={group}
            x-version={version}
            x-route-rule=mix
            

            HTTP Body (payload or form-data):

            {
              "id": 23,
              "username": "testUser"
            }
            
          • CUSTOM自定義路由

            CUSTOM路由專為一些對PATH路由MIX路由都不滿意的開發(fā)者準(zhǔn)備,只需要繼承AbstractRouteHandlerInterceptor 類,即可輕松實現(xiàn)自定義的路由規(guī)則。

          Param Transmission

          用戶端通用參數(shù)(諸如請求UA/Referer/IP等)以及Token用戶信息等,需要傳遞到服務(wù)提供方。Dubbo Gateway與服務(wù)提供者之間內(nèi)置的參數(shù)傳遞走Dubbo的RpcContext。

          Dubbo網(wǎng)關(guān)內(nèi)置了gatewayConsumerFiltergatewayProviderFilter,可作為Dubbo服務(wù)提供者的默認(rèn)Filter實現(xiàn),通過該Filter可獲取用戶端請求的一些參數(shù)以及自定義攔截器封裝的參數(shù)。當(dāng)然,開發(fā)者也可自行在服務(wù)提供方讀取RpcContext中的傳遞參數(shù)。

          更多使用方式,參考:gateway-samples  gateway-samples-provider

          Attention

          Dubbo Gateway對Dubbo服務(wù)提供者提供的對外服務(wù)有一條要求:只能有一個服務(wù)入?yún)?,且為對象類型(非普通Java數(shù)據(jù)類型)。

          瀏覽 12
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲日韩欧美一区 | 午夜精品久久99热蜜桃剧情介绍 | 婷婷色色五月天图片 | 国产无码精品黄色电影 | 国产乱╳╳aⅴ老师 |