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

如何通過(guò).NETCore+SpringCloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)

本篇文章為大家展示了如何通過(guò).NET Core + Spring Cloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

南康網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),南康網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為南康上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的南康做網(wǎng)站的公司定做!

毫無(wú)疑問(wèn),微服務(wù)架構(gòu)是目前的主流,在微服務(wù)架構(gòu)下,服務(wù)治理、負(fù)載均衡、服務(wù)熔斷、配置中心、API網(wǎng)關(guān) 等都是需要關(guān)注的問(wèn)題,當(dāng)然不是非要全部完善后才能進(jìn)行微服務(wù)開(kāi)發(fā),在很多項(xiàng)目團(tuán)隊(duì)中,初期可能會(huì)將某個(gè)服務(wù)部署成集群,然后通過(guò) Nginx 代理做到負(fù)載均衡提供服務(wù),但隨著微服務(wù)體量逐漸龐大,以上提到需要關(guān)注的問(wèn)題就越來(lái)越明顯。在 .NET Core 環(huán)境下,目前比較流行的微服務(wù)架構(gòu):Consul(服務(wù)治理、配置中心)+ Polly(服務(wù)熔斷)+ Ocelot(API網(wǎng)關(guān)),當(dāng)然這只是一種組合方式。

今天主要介紹一下如何通過(guò) Spring Cloud 下的 Eureka  來(lái)實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn),Spring Cloud 是 Java 平臺(tái)提供的一套解決方案,提供了微服務(wù)的基礎(chǔ)功能,包括 Eureka(服務(wù)治理)、Config(配置中心)、Hystrix(服務(wù)熔斷)、Zuul(API網(wǎng)關(guān))等。

至于為什么要將 .NET Core 服務(wù)融合 Spring Cloud 來(lái)部署,毫無(wú)疑問(wèn),這只是一種結(jié)合方案,如果團(tuán)隊(duì)是 Java + .NET, 如果恰好需要,嘗試一下也為何不可。

 

Steeltoe

Steeltoe[2] 是 .NET 與 Spring Cloud 結(jié)合的橋梁,Steeltoe 客戶(hù)端庫(kù)使 .NET Core 和 .NET Framework 應(yīng)用程序能夠輕松利用 Spring Cloud 的 Eureka、Hystrix、Config Server、云平臺(tái)服務(wù) 等核心組件。更多資料請(qǐng)參考官方文檔:http://steeltoe.io/docs/

 

搭建 Eureka Server

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

  2. 在 pom.xml 添加 eureka-server 的依賴(lài)

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

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServiceApplication {
     public static void main(String[] args) {
       SpringApplication.run(EurekaServiceApplication.class, args);
     }
    }
  4. 修改配置文件,模擬搭建 Eureka Server 集群

    如何通過(guò).NET Core + Spring Cloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)

    application.yml

    spring:
     application:
       # 服務(wù)名
       name: eureka-service
     profiles:
       # 默認(rèn)使用 server1 配置
       active: server1

    eureka:
     instance:
       # 超過(guò)這個(gè)時(shí)間沒(méi)收到心跳就剔除這個(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:
       # eureka client 刷新本地緩存時(shí)間,默認(rèn)30s
       registry-fetch-interval-seconds: 5
     server:
       # eureka server 刷新 readCacheMap 的時(shí)間,client 讀取的是 readCacheMap,默認(rèn)30s
       response-cache-update-interval-ms: 3000
       # 服務(wù)下線任務(wù)定時(shí),默認(rèn)60s
       eviction-interval-timer-in-ms: 3000

    application-server1.yml

    server:
     port: 8001

    eureka:
     instance:
       hostname: server1
    client:
     service-url:
       defaultZone: http://server2:8002/eureka/,http://server3:8003/eureka/

    application-server2.yml 和 application-server3.yml 與 server1 類(lèi)似,只需 port、hostname、defaultZone 作調(diào)整。hostname 對(duì)應(yīng)的名稱(chēng)需要修改電腦的 C:\Windows\System32\drivers\etc\HOSTS 文件添加映射關(guān)系

    127.0.0.1 server1
    127.0.0.1 server2
    127.0.0.1 server3
  5. 修改啟動(dòng)配置

    如何通過(guò).NET Core + Spring Cloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
 

創(chuàng)建 .NET Core 基礎(chǔ)服務(wù)

基礎(chǔ)服務(wù)只提供服務(wù),不引用其他服務(wù)

  1. 創(chuàng)建 .NET Core WebApi 項(xiàng)目

  2. NuGet 添加 Steeltoe.Discovery.ClientCore 引用(目前版本 2.1.1)

  3. 修改 Startup.cs 的 ConfigureServices 方法,添加 AddDiscoveryClient

    public void ConfigureServices(IServiceCollection services)
    {
     services.AddDiscoveryClient(Configuration);
     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
  4. 修改 Startup.cs 的 Configure 方法,添加 UseDiscoveryClient

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
     if (env.IsDevelopment())
     {
       app.UseDeveloperExceptionPage();
     }
     app.UseMvc();
     app.UseDiscoveryClient();
    }
  5. 修改配置文件 appsettings.json,更多配置請(qǐng)參考 eureka-client-settings[3]

    {
     "spring": {
       "application": {
         "name": "base-service"
       }
     },
     "eureka": {
       "client": {
         "serviceUrl": "http://server1:8001/eureka/",
         "shouldRegisterWithEureka": true,
         "shouldFetchRegistry": false // 不需要獲取注冊(cè)信息,只提供服務(wù)
       },
       "instance": {
         "port": 5001
       }
     }
    }
  6. 修改 program.cs,添加 UseUrls,端口與 appsettings.json port 一致

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
     WebHost.CreateDefaultBuilder(args)
     .UseUrls("http://*:5001")
     .UseStartup<Startup>();
  7. 再新建一個(gè)項(xiàng)目,其他都一致,端口改成 5002

  8. 啟動(dòng) 2 個(gè) base-service 成功后在 Eureka 中查看如下:

    如何通過(guò).NET Core + Spring Cloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
    如何通過(guò).NET Core + Spring Cloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
  9. 通過(guò)訪問(wèn) http://server1:6001/api/values 查看調(diào)用 base-service 返回結(jié)果,因?yàn)?base-service 有 2 個(gè)服務(wù),所以會(huì)自動(dòng)做到負(fù)載均衡

    如何通過(guò).NET Core + Spring Cloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)

上述內(nèi)容就是如何通過(guò).NET Core + Spring Cloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前名稱(chēng):如何通過(guò).NETCore+SpringCloud實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
網(wǎng)頁(yè)地址:http://www.chinadenli.net/article4/jdjpie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)微信公眾號(hào)移動(dòng)網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)