排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
微服务
因为自带的consul服务管理功能还是比较有限,比如想要移除一个consul中不要的服务,服务集群管理,consul作为配置中心管理等,所以就想自己来写一个consul服务的管理,一点一点的做慢慢的完善。
目前大概的一个效果:
上面的服务名是使用Tab菜单展示出来的,然后点击Tab菜单拿到服务名对应的服务实例,然后实现了一个不要服务的删除
这里暂时用到了consul三个接口:
获取consul服务名:http://consul地址/v1/catalog/services
获取consul实例: http://consul服务地址/v1/catalog/service/<consul服务名>
删除consul接口: http://consul服务地址/v1/agent/service/deregister/<consul服务id>
有接口的话其实就会比较方便了,后台写个接口调用前台展示即可
后台接口可以这样简单调用一下
public IActionResult GetConsulService() { HttpClient httpClient = new HttpClient(); string reuslt = httpClient.GetAsync("http://consul地址/v1/catalog/services").Result.Content.ReadAsStringAsync().Result; return Content(reuslt); } public IActionResult GetConsulInstancesByService(string serviceName) { HttpClient httpClient = new HttpClient(); string reuslt = httpClient.GetAsync("http://consul地址/v1/catalog/service/" + serviceName).Result.Content.ReadAsStringAsync().Result; return Content(reuslt); } public IActionResult DeleteConsulServiceByName(string serviceId) { HttpClient httpClient = new HttpClient(); string reuslt = httpClient.PutAsync("http://consul地址/v1/agent/service/deregister/" + serviceId, null).Result.Content.ReadAsStringAsync().Result; return Content(reuslt); }
前台使用的是vue+layui
html:
<div class="layui-tab" id="consulservice" lay-filter="consulservice" style="margin-top:5px"> <ul class="layui-tab-title"> <li v-for="(item,key,index) in consulservice" v-bind:class="index==0?'layui-this':''" v-bind:lay-id="key" :key="item.key">{{key}}</li> </ul> <div class="layui-tab-content"> <table class="layui-table layui-form"> <colgroup> <col> <col> <col> <col> <col> <col> <col> <col> </colgroup> <thead> <tr> <th>ID</th> <th>Node</th> <th>Address</th> <th>Port</th> <th>Node Checks</th> <th>显示</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item,index) in instances"> <td>{{item.ServiceID}}</td> <td>{{item.Node}}</td> <td> {{item.ServiceAddress}} </td> <td>{{item.ServicePort}}</td> <td>是</td> <td> <input type="checkbox" checked lay-skin="switch"> </td> <td><a href="/admin/category/update/id/9.html">修改</a> | <a class="del" data-id=".id9" @@click="deleteService(item.ServiceID,index)">删除</a></td> </tr> </tbody> </table> </div> </div>
js:
<script> var consulserviceVue = new Vue({ el: "#consulservice", data: { consulservice: {}, instances: [] }, created: function () { }, updated: function () { //数据是vue动态加载的,需要重新渲染某些layui组件 layui.use(['form'], function () { var form = layui.form; form.render(); }); }, methods: { deleteService: function (serviceId, index) { if (confirm("确定删除嘛?删除服务很危险的哦")) { $.post('/Consul/DeleteConsulServiceByName', { serviceId: serviceId }, function (result) { if (result == "") { alert("删除成功!"); //删除数据源中对应的数据,vue会自动删除对应的dom节点 consulserviceVue.$data.instances.splice(index, 1); } }); } } } }); layui.use(['element', 'form'], function () { var element = layui.element; var form = layui.form; //监听Tab菜单事件 element.on('tab(consulservice)', function () { var serviceName = this.getAttribute('lay-id'); getConsulInstancesByService(serviceName); }); }); var getConsulInstancesByService = function (serviceName) { $.get('/Consul/GetConsulInstancesByService', { serviceName: serviceName }, function (result) { var instances = JSON.parse(result); consulserviceVue.$data.instances = instances }); } $(function () { $.get('/Consul/GetConsulService', function (result) { var jsonobj = JSON.parse(result); consulserviceVue.$data.consulservice = jsonobj; //先获取第一个分类下的服务 var poi = 0; for (var key in jsonobj) { if (poi == 0) { getConsulInstancesByService(key); } poi++; } }); }); </script>
agent 相关接口:
/v1/agent/checks : 返回本地agent注册的所有检查(包括配置文件和HTTP接口) /v1/agent/services : 返回本地agent注册的所有 服务 /v1/agent/members : 返回agent在集群的gossip pool中看到的成员 /v1/agent/self : 返回本地agent的配置和成员信息 /v1/agent/join/<address> : 触发本地agent加入node /v1/agent/force-leave/<node>>: 强制删除node /v1/agent/check/register : 在本地agent增加一个检查项,使用PUT方法传输一个json格式的数据 /v1/agent/check/deregister/<checkID> : 注销一个本地agent的检查项 /v1/agent/check/pass/<checkID> : 设置一个本地检查项的状态为passing /v1/agent/check/warn/<checkID> : 设置一个本地检查项的状态为warning /v1/agent/check/fail/<checkID> : 设置一个本地检查项的状态为critical /v1/agent/service/register : 在本地agent增加一个新的服务项,使用PUT方法传输一个json格式的数据 /v1/agent/service/deregister/<serviceID> : 注销一个本地agent的服务项
catalog endpoints相关接口:catalog endpoints用来注册/注销nodes、services、checks
/v1/catalog/register : Registers a new node, service, or check /v1/catalog/deregister : Deregisters a node, service, or check /v1/catalog/datacenters : Lists known datacenters /v1/catalog/nodes : Lists nodes in a given DC /v1/catalog/services : Lists services in a given DC /v1/catalog/service/<service> : Lists the nodes in a given service /v1/catalog/node/<node> : Lists the services provided by a node
health endpoints相关接口:health endpoints用来查询健康状况相关信息,该功能从catalog中单独分离出来
/v1/healt/node/<node>: 返回node所定义的检查,可用参数?dc= /v1/health/checks/<service>: 返回和服务相关联的检查,可用参数?dc= /v1/health/service/<service>: 返回给定datacenter中给定node中service /v1/health/state/<state>: 返回给定datacenter中指定状态的服务, state可以是"any", "unknown", "passing", "warning", or "critical",可用参数?dc=
未完待续......下期准备实现一个添加服务与权限管理相关的内容
欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739
评价