.Net Core微服务入门——Consul集群搭建(三)

26 篇文章 1 订阅
13 篇文章 1 订阅

.Net Core微服务入门——Consul集群搭建(三)

本章将介绍.Consul 集群 Client

还记得第一篇中,我们再Consul中启动了2个MyAPI服务么,我们重启下,看下效果:

浏览:http://192.168.8.25:8500

在这里插入图片描述

再浏览 http://192.168.8.25:8501,http://192.168.8.25:8502,结果一样。

那我们将Consul1停掉,看会发生什么情况

http://192.168.8.25:8500:无法访问

http://192.168.8.25:8501,http://192.168.8.25:8502,结果如下:

在这里插入图片描述
MyAPI没有连上Consul!MyAPI没有连上Consul!MyAPI没有连上Consul!

纳尼,这不是我想要的结果!

Consul1停止,Consul2、Consul3就不起作用了

这是什么集群,这不是我想要的结果

想办法!想办法!

新建Consul Clinet,并加入集群

1、新建client:

docker run --name consul-client1 -d -p 8505:8500 consul agent -client -bind=0.0.0.0 -node=consul-client1 0.0.0.0 -retry-join=172.17.0.3

指定端口 8505,设置成client,并指定节点名称 consul-client1

2、那MyAPI就不能直接访问Serve了,而是访问Client

修改项目中 ConsulAddress (http://192.168.8.25:8500/ 改成 http://192.168.8.25:8505/)

 "ConsulSetting": {
    //"ConsulAddress": "http://localhost:8500/", //注意,docker容器内部无法使用localhost访问宿主机器,如果是控制台启动的话就用localhost
    //"ConsulAddress": "http://host.docker.internal:8500/", //注意,docker容器内部无法使用localhost访问宿主机器
    "ConsulAddress": "http://192.168.8.25:8505/",
    "ServiceName": "MyApi", //服务名
    "ServiceHealthApi": "/api/health", //心跳检查api
  }

发布,重启拉取镜像,运行容器

3、查看Consul 控制台

在这里插入图片描述

在这里插入图片描述
4、停止consul1

重新查看控制台

在这里插入图片描述
在这里插入图片描述
MyAPI 连接正常。

今天有人问,如果Client 挂了怎么办!

没关系,解决办法来了

1、再新建一个client:

docker run --name consul-client1 -d -p 8506:8500 consul agent -client -bind=0.0.0.0 -node=consul-client2 0.0.0.0 -retry-join=172.17.0.3

指定端口 8506,设置成client,并指定节点名称 consul-client2.

2、修改MyApi代码,将consulAddress 改成启动配置

ConsulHelper 代码修改:

var consulhost = configuration["consulhost"];//configuration["ConsulSetting:ConsulAddress"];
var consulport = configuration["consulport"];
ConsulClient client = new ConsulClient(c =>

    c.Address = new Uri($"http://{consulhost}:{consulport}/");
});

启动配置

--ip=127.0.0.1 --port=49167 --consulhost=192.168.8.25 --consulport:8505

3、发布

启动两个MyApi,各连接一个consul client

docker run -d -p 5001:80 --name myservice1 192.168.8.25:5000/myapi  --ip=192.168.8.61 --port=5001 --consulhost=192.168.8.25 --consulport:8505
docker run -d -p 5002:80 --name myservice2 192.168.8.25:5000/myapi  --ip=192.168.8.61 --port=5002 --consulhost=192.168.8.25 --consulport:8506

这样,一个client 挂了,不影响另一个。

多台服务器申请下来了,准备多机搭建consul集群,配合nginx 耍耍

添加链接描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值