如何使用.NETCore+Cloud實(shí)現(xiàn)API網(wǎng)關(guān)

這篇文章給大家介紹如何使用.NET Core + Cloud實(shí)現(xiàn)API網(wǎng)關(guān),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

創(chuàng)新互聯(lián)建站主營(yíng)小店網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,小店h5微信平臺(tái)小程序開發(fā)搭建,小店網(wǎng)站營(yíng)銷推廣歡迎小店等地區(qū)企業(yè)咨詢

API 網(wǎng)關(guān)是系統(tǒng)的唯一入口,調(diào)用任何服務(wù)的請(qǐng)求都需要經(jīng)過網(wǎng)關(guān)層,最終才可能到達(dá)目標(biāo)服務(wù),既然是必經(jīng)之路,那我們可以在網(wǎng)關(guān)層進(jìn)行一些通用的操作,如:認(rèn)證、鑒權(quán)、限流、智能路由、緩存、日志、監(jiān)控、超時(shí)、熔斷、重試等等,這樣既使整個(gè)框架條理清晰,也讓開發(fā)者更多注重功能的邏輯實(shí)現(xiàn)。

常見的 API 網(wǎng)關(guān)項(xiàng)目有:Kong、TykNetflix zuul、Ocelot 等。在 Spring Cloud 中,Zuul 是其核心組件,下面將介紹 .NET Core 中通過 Zuul 來進(jìn)行統(tǒng)一 API 調(diào)用。

如何使用.NET Core + Cloud實(shí)現(xiàn)API網(wǎng)關(guān)  
 

搭建 Zuul Server

  1. 在 IntelliJ IDEA 中新建項(xiàng)目,選 Spring Initializr 完成項(xiàng)目創(chuàng)建

  2. 在 pom.xml 添加 zuul 和 eureka-client  的依賴,我們將會(huì)把 Zuul Server 注冊(cè)到 Eureka Server

    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  3. 在啟動(dòng)類上添加 EnableZuulProxy 注解

    @EnableZuulProxy
    @SpringBootApplication
    public class EurekaServiceApplication {
     public static void main(String[] args) {
       SpringApplication.run(EurekaServiceApplication.class, args);
     }
    }
  4. 修改 application.yml 配置文件(集群模式通過一個(gè)類似 gateway.xxx.com 的域名指向多個(gè) Zuul  Server 實(shí)例即可

    spring:
     application:
       name: zuul-service

    server:
     port: 5555

    eureka:
     instance:
       hostname: server1
       # 超過這個(gè)時(shí)間沒收到心跳就剔除這個(gè)服務(wù),這個(gè)配置一般為服務(wù)刷新時(shí)間配置的三倍,默認(rèn)90s
       lease-expiration-duration-in-seconds: 15
       # 服務(wù)刷新時(shí)間,默認(rèn)30s
       lease-renewal-interval-in-seconds: 5
     client:
       service-url:
         defaultZone: http://server1:8001/eureka/,http://server2:8002/eureka/,http://server3:8003/eureka/
  5. 啟動(dòng)服務(wù),訪問:http://server1:8001/(請(qǐng)確保 Eureka Server 已啟動(dòng)),可以發(fā)現(xiàn) Zuul Server 已在 5555 端口啟動(dòng)

 

創(chuàng)建 .NET Core 服務(wù)

這里直接基于文章 .NET Core + Spring Cloud:服務(wù)注冊(cè)與發(fā)現(xiàn) 中的測(cè)試項(xiàng)目,修改 .NET Core 客戶端服務(wù)的配置文件 services:base-service:url

"services": {
 "base-service": {
   "url": "http://server1:5555/base-service/" // 原來 http://base-service/
 }
}
 

因?yàn)槲覀兊?Zuul Server 是啟動(dòng)在 server1 的 5555 端口,通過 Zuul 網(wǎng)關(guān)調(diào)用服務(wù)的路由規(guī)則是 http://server1:5555/ + {Application 小寫},而基礎(chǔ)服務(wù)的 Application名稱為 BASE-SERVICE,所以 url 設(shè)置為 http://server1:5555/base-service/

如何使用.NET Core + Cloud實(shí)現(xiàn)API網(wǎng)關(guān)  
 

測(cè)試

 
訪問 base-service

通過網(wǎng)關(guān)的路由地址 http://server1:5555/base-service/ 訪問 api/values 接口,多次請(qǐng)求可測(cè)試負(fù)載均衡效果

 
如何使用.NET Core + Cloud實(shí)現(xiàn)API網(wǎng)關(guān)  
 
訪問 client-service

通過網(wǎng)關(guān)的路由地址 http://server1:5555/client-service/ 訪問 api/values 接口,而配置文件中的 base-service 設(shè)置的也是網(wǎng)關(guān)地址,所以實(shí)際在調(diào)用 base-service 也是通過網(wǎng)關(guān)

 
如何使用.NET Core + Cloud實(shí)現(xiàn)API網(wǎng)關(guān)  

關(guān)于如何使用.NET Core + Cloud實(shí)現(xiàn)API網(wǎng)關(guān)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

文章題目:如何使用.NETCore+Cloud實(shí)現(xiàn)API網(wǎng)關(guān)
分享路徑:http://muchs.cn/article30/isjdso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站維護(hù)、網(wǎng)站制作、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)公司移動(dòng)網(wǎng)站建設(shè)

廣告

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

成都定制網(wǎng)站建設(shè)