基础准备
1.创建asp.net core web 应用程序选择api
2.appsettings.json 配置consul服务器地址,以及本机ip和端口号信息
{ "logging": { "loglevel": { "default": "warning" } }, "consul": { "ip": "127.0.0.1", "port": "8500" }, "service": { "name": "zyz" }, "ip": "localhost", "port": "6655", "allowedhosts": "*" }
3.程序入口(program.cs)配置useurls,ip和port从配置文件(或者命令行中)读取(命令行启动方式:dotnet consulserver.dll –ip localhost –port 8644)
using system; using system.collections.generic; using system.io; using system.linq; using system.threading.tasks; using microsoft.aspnetcore; using microsoft.aspnetcore.hosting; using microsoft.extensions.configuration; using microsoft.extensions.logging; namespace consulserver { public class program { public static void main(string[] args) { createwebhostbuilder(args).build().run(); } public static iwebhostbuilder createwebhostbuilder(string[] args) { var config = new configurationbuilder().addcommandline(args) .build();//获取配置信息 return webhost.createdefaultbuilder(args) .useurls($"http://{ config["ip"]}:{config["port"]}")//配置ip地址和端口地址 .usestartup<startup>(); } } }
4.配置基础数据,并且调用注册consul接口
using system; using system.collections.generic; using system.linq; using system.threading.tasks; using consul; using consulserver.consulapi; using consulserver.entity; 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; namespace consulserver { 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.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, iapplicationlifetime lifetime) { if (env.isdevelopment()) { app.usedeveloperexceptionpage(); } ////配置信息单例 //configsingleton.setconfigsingleton(configuration); app.usemvc(); consulentity consulentity = new consulentity { ip = configuration["ip"], port = int.parse(configuration["port"] ), servicename = "zyz", consulip = configuration["consul:ip"], consulport = convert.toint32(configuration["consul:port"]) }; app.registerconsul(lifetime, consulentity); } } }
5.配置注册信息
using consul; using consulserver.entity; using microsoft.aspnetcore.builder; using microsoft.aspnetcore.hosting; using microsoft.extensions.configuration; using system; using system.collections.generic; using system.linq; using system.threading.tasks; namespace consulserver.consulapi { /// <summary> /// consul /// </summary> public static class appbuilderextensions { /// <summary> /// 注册consul /// </summary> /// <param name="app"></param> /// <param name="lifetime"></param> /// <param name="serviceentity"></param> /// <returns></returns> public static iapplicationbuilder registerconsul(this iapplicationbuilder app, iapplicationlifetime lifetime, consulentity serviceentity) { //consul地址 action<consulclientconfiguration> configclient = (consulconfig) => { consulconfig.address = new uri($"http://{serviceentity.consulip}:{ serviceentity.consulport}"); consulconfig.datacenter = "dc1"; }; //建立连接 var consulclient = new consulclient(configclient); var httpcheck = new agentservicecheck() { deregistercriticalserviceafter = timespan.fromseconds(5),//服务启动多久后注册 interval = timespan.fromseconds(10),//健康监测 http = string.format($"http://{serviceentity.ip}:{serviceentity.port}/api/health"),//心跳检测地址 timeout = timespan.fromseconds(5) }; //注册 var registrtion = new agentserviceregistration() { checks = new[] { httpcheck }, id = "zyzservice" + guid.newguid().tostring(),//服务编号不可重复 name = serviceentity.servicename,//服务名称 address = serviceentity.ip,//ip地址 port = serviceentity.port//端口 }; //注册服务 consulclient.agent.serviceregister(registrtion); return app; } } }
6.以命令行启动程序查看consul(dotnet consulserver.dll –ip localhost –port 8644)
黄山市民网:https://www.huangshanshimin.com/