AspNetCore 多环境配置 以及注册 消费Consul

本文主要记录 CoreApi 的多环境配置以及如何消费在consul中注册的Api 服务

一、建立三个CoreApi

        咱们在项目中建立三个站点分别为 UserServices “用户服务OrderServices “订单服务” 以及 StorehouseServices “库房服务”html

  1. 、打开VS2017 建立Core Web 应用程序

     

   2、选择项目模板

       注意:在选择项目模板的时候有一个支持Https 的选项不使用能够去掉json

   

 

  忘记去掉了也没问题在项目属性调试中也能够去掉 以下图:api

   

 

   三、在建立CoreApi的时候尽可能不使用项目模板进行建立由于默认会使用MVC的引擎可使用空模板进行建立,自行搭建。

二、CoreApi进行环境的配置

  1、CoreApi的几种环境:开发(Development)占存(Staging)生产(Production)

  项目建立后咱们在项目启动文件中有两种环境启动设置以下图服务器

   

  咱们能够看到不管是使用IIS启动仍是 命令启动时都默认使用开发环境变量“”Development“”。app

  在项目建立完成后默认有一个Json的配置文件,咱们打开后能够看到一个默认的名为 appsettings.Development.json 的配置文件 如图所示负载均衡

   

 

  也就是说项目建立完成后不论使用哪一种环境运行都是同一个环境变量“Development” 一样也表明这不论哪一种环境运行默认使用的同一份配置文件异步

  二、新增Production(生产)环境配置文件

         2.一、如图所示:ide

  

  2.2、更改launchSettings.json 中的环境变量的配置信息 以下性能

   

  三、运行结果 三种运行方式

    3.一、首先使用IIS运行 默认走开发者配置学习

    

    3.二、在当前目录 下使用dotnet run  运行

    

    咱们能够看到配置已经生效了

    (3):在项目的生成bin 目录下使用 dotnet  WebApplication1.dll 运行 结果

    

    能够看到这个时候怎么的配置失效了由于命令行是托管在Kestrel上运行的默认使用的端口就是 5000/5001 这个时候咱们须要在增长一个配置来更改默认

    

    配置以下步骤:

    在项目新增的配置文件appsettings.Production.json中 添加 以下

    

    配置 127.0.0.1:表明着localhost 在下面使用的时候须要进行一下转换因此用127.0.0.1 具体配置以下

     在项目Program 中这样去使用

    

    这里不会影响咱们的其余运行方式由于运行的环境不一样:运行结果以下     

     

  四、根据不一样的运行环境读取使用不一样的配置文件

  上面的有的步骤只是更改了项目启动时的IP端口信息,下面咱们配置在项目运行时根据不一样的运行环境去读取不一样的配置文件

  Startup 文件写入以下代码

 private IHostingEnvironment env;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        public Startup(IHostingEnvironment environment)
        {
            env = environment;
            if (env.IsDevelopment())
            {
                var config = new ConfigurationBuilder()
                 .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.Development.json", true, true) //.AddJsonFile("appsettings.json", true, true) //
                 .AddEnvironmentVariables()
                 .Build();
                Configuration = config;
            }
            //生产环境Production运行 命令运行读取的配置信息
            if (env.IsProduction())
            {
                var config = new ConfigurationBuilder()
                 .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                 .AddJsonFile("appsettings.Production.json", true, true)
                 .AddEnvironmentVariables()
                 .Build();
                Configuration = config;
            };
        }
View Code

  五、项目启动后把服务注册到Consul中

  代码以下

  public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
       
            //ServiceID复制
            ConsulServicesID = Configuration["ServicesName"]+ Guid.NewGuid();
            using (var client = new ConsulClient(ConsulConfig))
            {
                //注册服务到 Consul
                client.Agent.ServiceRegister(new AgentServiceRegistration()
                {
                    ID = ConsulServicesID,//服务编号,不能重复,用 Guid 最简单
                    Name = Configuration["ServicesName"],//服务的名字
                    Address = Configuration["SerivceIP"],//个人 ip 地址(能够被其余应用访问的地址,本地测试能够用127.0.0.1,机房环境中必定要写本身的内网 ip 地址)
                    Port = Convert.ToInt32(Configuration["SerivcePort"]),//个人端口
                    Check = new AgentServiceCheck
                    {
                        DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务中止多久后反注册
                        Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔,或者称为心跳间隔
                        HTTP = $"{Configuration["HealthUrl"]}/api/Health",//健康检查地址
                        Timeout = TimeSpan.FromSeconds(5)
                    }
                }).Wait();//Consult 客户端的全部方法几乎都是异步方法,可是都没按照规范加上Async 后缀,因此容易误导。记得调用后要 Wait()或者 await

            }
            //程序正常退出的时候从 Consul 注销服务
            //要经过方法参数注入 IApplicationLifetime
            applicationLifetime.ApplicationStopped.Register(() =>
            {
                using (var client = new ConsulClient(ConsulConfig))
                {
                    //ServiceDeregister异步方法增长 Wait 等待完成
                    client.Agent.ServiceDeregister(ConsulServicesID).Wait();
                }
            });
        }
View Code

  6、项目中我使用了Swagger 具体配置

            Swagger 配置博客 http://www.javashuo.com/article/p-exanjcem-kz.html

三、消费consul中的CoreApi的服务

  一、建立项目

  使用什么样的客户端都行,控制台,MVC,WebFrom 都行

  二、具体代码

  这个代码只是用来引路,能够根据本身的喜爱去进行封装

  代码以下:

 #region //查看全部consul中被注册的服务
            //查看全部consul中被注册的服务
            using (var consulClient = new ConsulClient(c => { c.Address = new Uri("http://127.0.0.1:8500"); c.Datacenter = "dc1"; }))
            {

                var services = consulClient.Agent.Services().Result.Response;
                //var ss = services.Values.Where(s => s.Service.Equals("UserServices", StringComparison.OrdinalIgnoreCase));//忽略大小写
                foreach (var service in services.Values)
                {
                    Console.WriteLine($"id={service.ID},name={service.Service},ip={service.Address},port={service.Port}");
                }
            }
            #endregion
            #region 客户端负载均衡
            //客户端负载均衡
            using (var consulClient = new ConsulClient(c => c.Address = new Uri("http://127.0.0.1:8500")))
            {
                var services = consulClient.Agent.Services().Result.Response.Values
                .Where(s => s.Service.Equals("UserServices", StringComparison.OrdinalIgnoreCase));
                if (!services.Any())
                {
                    Console.WriteLine("找不到服务的实例");
                }
                else
                {
                    // services.ElementAt(1);//若是环境中有多台服务器注册服务时咱们可使用随机数的方式,使用下标进行随机抽取一台服务进行使用
                    //集群中也能够轮训,当服务器性能差很少的时候能够轮着来
                    var service = services.ElementAt(Environment.TickCount % services.Count());
                    Console.WriteLine($"{service.Address}:{service.Port}");
                }

            }
            #endregion

            #region 调用服务方法     UserServices/api/Values
            using (var consulClient = new ConsulClient(c => { c.Address = new Uri("http://127.0.0.1:8500"); c.Datacenter = "dc1"; }))
            {
                var AllServicesInfor = consulClient.Agent.Services().Result.Response;
                //获取第一个实例 把UserServices转换为在Consul中注册的路径 而后进行访问
                var UserServices = AllServicesInfor.Values.Where(s => s.Service.Equals("UserServices", StringComparison.OrdinalIgnoreCase)).First();//忽略大小写
                using (System.Net.Http.HttpClient http = new HttpClient())
                {
                    using (var HttpContent = new StringContent("", System.Text.Encoding.UTF8, "application/json"))
                    {
                        var ss = http.PostAsync($"http://{UserServices.Address}:{UserServices.Port}/api/Values", HttpContent);
                        string sss = ss.Result.RequestMessage.ToString();
                    }
                }
            }
            #endregion
View Code

 项目连接

 连接:https://pan.baidu.com/s/1V0YcX1kFJg752icNICTuQQ 密码:1s47

有不足之处 但愿你们指出相互学习,

                                     本文原创:转载请注明出处 谢谢!

相关文章
相关标签/搜索