应无所住,而生其心
排名
1
文章
862
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

自己实现一个consul的服务管理,功能持续完善

7548人阅读 2020/6/8 15:16 总访问:5261265 评论:2 收藏:0 手机
分类: 微服务

因为自带的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>


有接口的话其实就会比较方便了,后台写个接口调用前台展示即可

后台接口可以这样简单调用一下

  1. public IActionResult GetConsulService()
  2. {
  3.     HttpClient httpClient = new HttpClient();
  4.     string reuslt = httpClient.GetAsync("http://consul地址/v1/catalog/services").Result.Content.ReadAsStringAsync().Result;
  5.     return Content(reuslt);
  6. }
  7. public IActionResult GetConsulInstancesByService(string serviceName)
  8. {
  9.     HttpClient httpClient = new HttpClient();
  10.     string reuslt = httpClient.GetAsync("http://consul地址/v1/catalog/service/" + serviceName).Result.Content.ReadAsStringAsync().Result;
  11.     return Content(reuslt);
  12. }
  13. public IActionResult DeleteConsulServiceByName(string serviceId)
  14. {
  15.     HttpClient httpClient = new HttpClient();
  16.     string reuslt = httpClient.PutAsync("http://consul地址/v1/agent/service/deregister/" + serviceId, null).Result.Content.ReadAsStringAsync().Result;
  17.     return Content(reuslt);
  18. }

前台使用的是vue+layui

html:

  1. <div class="layui-tab" id="consulservice" lay-filter="consulservice" style="margin-top:5px">
  2.     <ul class="layui-tab-title">
  3.         <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>
  4.     </ul>
  5.     <div class="layui-tab-content">
  6.         <table class="layui-table layui-form">
  7.             <colgroup>
  8.                 <col>
  9.                 <col>
  10.                 <col>
  11.                 <col>
  12.                 <col>
  13.                 <col>
  14.                 <col>
  15.                 <col>
  16.             </colgroup>
  17.             <thead>
  18.                 <tr>
  19.                     <th>ID</th>
  20.                     <th>Node</th>
  21.                     <th>Address</th>
  22.                     <th>Port</th>
  23.                     <th>Node Checks</th>
  24.                     <th>显示</th>
  25.                     <th>操作</th>
  26.                 </tr>
  27.             </thead>
  28.             <tbody>
  29.                 <tr v-for="(item,index) in instances">
  30.                     <td>{{item.ServiceID}}</td>
  31.                     <td>{{item.Node}}</td>
  32.                     <td>
  33.                         {{item.ServiceAddress}}
  34.                     </td>
  35.                     <td>{{item.ServicePort}}</td>
  36.                     <td></td>
  37.                     <td>
  38.                         <input type="checkbox" checked lay-skin="switch">
  39.                     </td>
  40.                     <td><a href="/admin/category/update/id/9.html">修改</a> | <a class="del" data-id=".id9" @@click="deleteService(item.ServiceID,index)">删除</a></td>
  41.                 </tr>
  42.             </tbody>
  43.         </table>
  44.     </div>
  45. </div>

js:

  1. <script>
  2.     var consulserviceVue = new Vue({
  3.         el"#consulservice",
  4.         data: {
  5.             consulservice: {},
  6.             instances: []
  7.         },
  8.         createdfunction ({
  9.         },
  10.         updatedfunction ({
  11.             //数据是vue动态加载的,需要重新渲染某些layui组件
  12.             layui.use(['form'], function ({
  13.                 var form = layui.form;
  14.                 form.render();
  15.             });
  16.         },
  17.         methods: {
  18.             deleteServicefunction (serviceId, index{
  19.                 if (confirm("确定删除嘛?删除服务很危险的哦")) {
  20.                         $.post('/Consul/DeleteConsulServiceByName', { serviceId: serviceId }, function (result{
  21.                             if (result == "") {
  22.                                 alert("删除成功!");
  23.                                 //删除数据源中对应的数据,vue会自动删除对应的dom节点
  24.                                 consulserviceVue.$data.instances.splice(index, 1);
  25.                             }
  26.                         });
  27.                 }
  28.             }
  29.         }
  30.     });
  31.     layui.use(['element''form'], function ({
  32.         var element = layui.element;
  33.         var form = layui.form;
  34.         //监听Tab菜单事件
  35.         element.on('tab(consulservice)'function ({
  36.             var serviceName = this.getAttribute('lay-id');
  37.             getConsulInstancesByService(serviceName);
  38.         });
  39.     });
  40.     var getConsulInstancesByService = function (serviceName{
  41.         $.get('/Consul/GetConsulInstancesByService', { serviceName: serviceName }, function (result{
  42.             var instances = JSON.parse(result);
  43.             consulserviceVue.$data.instances = instances
  44.         });
  45.     }
  46.     $(function ({
  47.         $.get('/Consul/GetConsulService'function (result{
  48.             var jsonobj = JSON.parse(result);
  49.             consulserviceVue.$data.consulservice = jsonobj;
  50.             //先获取第一个分类下的服务
  51.             var poi = 0;
  52.             for (var key in jsonobj) {
  53.                 if (poi == 0) {
  54.                     getConsulInstancesByService(key);
  55.                 }
  56.                 poi++;
  57.             }
  58.         });
  59.     });
  60. </script>


agent 相关接口:

  1. /v1/agent/checks : 返回本地agent注册的所有检查(包括配置文件和HTTP接口)
  2. /v1/agent/services : 返回本地agent注册的所有 服务
  3. /v1/agent/members : 返回agent在集群的gossip pool中看到的成员
  4. /v1/agent/self : 返回本地agent的配置和成员信息
  5. /v1/agent/join/<address> : 触发本地agent加入node
  6. /v1/agent/force-leave/<node>>: 强制删除node
  7. /v1/agent/check/register : 在本地agent增加一个检查项,使用PUT方法传输一个json格式的数据
  8. /v1/agent/check/deregister/<checkID> : 注销一个本地agent的检查项
  9. /v1/agent/check/pass/<checkID> : 设置一个本地检查项的状态为passing
  10. /v1/agent/check/warn/<checkID> : 设置一个本地检查项的状态为warning
  11. /v1/agent/check/fail/<checkID> : 设置一个本地检查项的状态为critical
  12. /v1/agent/service/register : 在本地agent增加一个新的服务项,使用PUT方法传输一个json格式的数据
  13. /v1/agent/service/deregister/<serviceID> : 注销一个本地agent的服务项

catalog endpoints相关接口:catalog endpoints用来注册/注销nodes、services、checks

  1. /v1/catalog/register : Registers a new node, service, or check
  2. /v1/catalog/deregister : Deregisters a node, service, or check
  3. /v1/catalog/datacenters : Lists known datacenters
  4. /v1/catalog/nodes : Lists nodes in a given DC
  5. /v1/catalog/services : Lists services in a given DC
  6. /v1/catalog/service/<service> : Lists the nodes in a given service
  7. /v1/catalog/node/<node> : Lists the services provided by a node

health endpoints相关接口:health endpoints用来查询健康状况相关信息,该功能从catalog中单独分离出来

  1. /v1/healt/node/<node>: 返回node所定义的检查,可用参数?dc=
  2. /v1/health/checks/<service>: 返回和服务相关联的检查,可用参数?dc=
  3. /v1/health/service/<service>: 返回给定datacenter中给定node中service
  4. /v1/health/state/<state>: 返回给定datacenter中指定状态的服务,
  5. state可以是"any""unknown""passing""warning"or "critical",可用参数?dc=



未完待续......下期准备实现一个添加服务与权限管理相关的内容



欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

饰心

2020/6/15 9:17:07

牛皮,我们SaaS平台好像也用到了这个

剑轩:@饰心我觉得这个consul还挺好用的

2020/6/15 9:25:06 回复

.netCore3.1 consul服务集群

前言Consul是一种服务网络解决方案,可跨任何运行时平台以及公共或私有云连接和保护服务简而言之:集群下载地址环境版本:v...

.netCore3.1 Ocelot 与 consul 熔断限流与缓存

目录与前言目录链接:.net core Ocelot 简单网关集群熔断架构整合目录基于上一篇文章展开 Ocelot 与 Consul 进行合并创建一...

consul

Consulagent-dev1、-dev发开模式,数据都在内存2、-server生产模式,数据会持久化

Windows下安装consul与简单介绍

Consul 是一个用来实现分布式系统服务发现与配置的开源工具。它内置了服务注册与发现框架、分布一致性协议实现、健康检查、...

net core服务注入到consul简单调用

下雨又停电,听说下雨天奶茶和代码最配了,找个奶茶店写代码吧 在奶茶店看到有几个小妹妹耍王者好想和她们一起打两把,我...

consul实现简单的服务集群负载均衡调用

接上一篇,consul要实现简单的服务集群,其实也很简单,只需要把多个服务使用统一个名字注入即可,然后调用的时候通过服务...

consul 无法通过ip访问

consul agent -dev 只能本地可以访问要想通过ip可以访问,使用下面的使用即可consul agent -dev -client 0.0.0.0 -ui 指定...

consul 本机可以访问客户端无法访问

是因为默认的启动方式consul agent -dev 只能本机才可以访问如果要外网也可以访问加一个-client 0.0.0.0即可consul agent ...

consulAPI接口

/v1/agent/checks : 返回本地agent注册的所有检查(包括配置文件和HTTP接口) /v1/agent/services : 返回本地agent注册的所...

consul常用API接口地址

agent本地注册 curl http://127.0.0.1:8500/v1/agent/checks 本地代理注册服务的健康状态check curl http://1...

NetCore加consul实现简单服务实例负载

今天,总结一下之前学习的Consul基础(后续如果有时间的话,再加上Ocelot部分):新建一个WebApi项目:模拟服务创建一个Web...

consul删除不需要的服务

调用consul的api接口就行了put请求:">http://consul服务地址/v1/agent/service/deregister/&lt;serviceid&gt;serviceid可...

consul http api与consul-Template

consul的主要接口是RESTful HTTP API,该API可以用来增删查改nodes、services、checks、configguration。所有的endpoints主...

.net core3.1使用API网关Ocelot五:配合consul实现动态路由

真正的项目中我们的服务会很多,如果一一地配置到配置文件,将会是一个巨大的工程,这个时候我们就可以让Ocelot与我们的服...

consul外网访问

consul agent -dev 只能本地可以访问要想通过ip可以访问,使用下面的使用即可consul agent -dev -client 0.0.0.0 -ui 指定...