1. 程式人生 > >linux下 .netcore 微服務註冊到SpringClound

linux下 .netcore 微服務註冊到SpringClound

disco 分享圖片 dmv ron ble core cor 執行權限 其中

1.創建一個SpringCloud

Idea工具:New Project -> Spring Initializr -> Next

技術分享圖片

然後填寫對應名稱 -> Next

技術分享圖片

然後選擇:Cloud Discovery -> 勾選 Eureka Server -> Next

技術分享圖片

然後填寫對應的工程名,還有工程路徑 -> Finish

技術分享圖片

然後在springboot工程的啟動application類上加 @EnableEurekaServer 註解

技術分享圖片

配置yml文件,習慣使用yml進行配置。修改application.properties 為 -> application.yml,然後加入如下配置:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
其中 port 服務註冊中心的端口號,可根據實際情況修改
然後 啟動服務後,使用 http://ip:8761 訪問,ip為服務所在ip。 界面如下:

技術分享圖片

2. 創建.netcore服務
使用VS2017 創建一個 dotnet core web api程序。
新建項目 -> Visual C# -> .NET Core -> ASP.NET Core Web 應用程序 -> 修改名稱和位置等 -> 確定

技術分享圖片

然後選擇 API -> 去掉https -> 確定

技術分享圖片

然後,使用NuGet工具安裝Pivotal.Discovery.ClientCore

VS ->工具 -> NuGet 包管理器 -> 程序包管理控制臺 -> 然後再控制臺中輸入如下命令:

Install-Package Pivotal.Discovery.ClientCore

然後再配置文件appsettings.json中添加如下配置:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "spring": {
    "application": {
      "name": "WebApp1"
    }
  },
  "eureka": {
    "client": {
      "serviceUrl": "http://127.0.0.1:8761/eureka/",
      "shouldFetchRegistry": false,
      "shouldRegisterWithEureka": true
    },
    "instance": {
      "port": 5000,
      "preferIpAddress": true,
      "instanceId": "localhost"
    }
  }
}

其中 serviceUrl 為 服務註冊的地址,根據實際的註冊中心地址填寫。這裏默認都在同一個服務器上。

然後修改Startup.cs文件如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Pivotal.Discovery.Client;

namespace WebApplication1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDiscoveryClient(Configuration);
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
            app.UseDiscoveryClient();
        }
    }
}

加粗的為新添加的代碼。

啟動服務後,使用 http:127.0.0.1:5000/api/values方位,如果 返回 的值 為 ["value1", "value2"] ,則成功。也可以在註冊中心的web頁面查看服務是否註冊上來。

3.增加 配置文件,進行微服務的端口讀取

微服務中默認端口是5000。以後為了方便,使用讀取配置文件的方式進行獲取端口。

在項目中新建json文件,名稱為host.json

然後添加內容如下:

{
  "urls": "http://*:9098"
}

這是端口為9098。

然後修改代碼 Program.cs 中的代碼如下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
     var config = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("host.json", optional: true, reloadOnChange: true)
                   .Build();

     return WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(config)
                .UseStartup<Startup>();
}

註意:當修改host.json中的端口的時候,同步修改appsettings.json中 port 信息。

啟動項目,監聽端口就會變為9098

4.項目發布

右擊工程 -> 發布 -> 文件夾 -> 更改發布到的目錄 -> 高級 -> 修改如下:

技術分享圖片

點擊保存 -> 點擊發布。

把發布後的包直接拷貝到linux下,添加執行權限:

chmod u+x 程序名

然後進行到程序所在目錄

./程序名

微服務即可起來。

5. 修改端口

在發布的文件中執行

ll *.json

顯示:

技術分享圖片

然後修改host.json和appsettings.json中的端口,然後重啟服務。

linux下 .netcore 微服務註冊到SpringClound