排名
1
文章
862
粉丝
112
评论
163
.net core自定义项目模板,创建自己的模板项目,使用命令行创建模板项目
尘叶心繁 : 可以可以讲真的我都想弄个模板
net core webapi post传递参数
庸人 :
确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 : 已精
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 :
疯狂反射
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

接上一篇,consul要实现简单的服务集群,其实也很简单,只需要把多个服务使用统一个名字注入即可,然后调用的时候通过服务名获取服务的时候就可以获取到多个,然后通过一种负载均衡算法或者是按一定比例算法调用即可。
在另外一个项目在注入一下consul服务
- "Consul": {
- "ServiceName": "axj",
- "ServiceIP": "localhost",
- "ServicePort": 19341,
- "ServiceHealthCheck": "http://localhost:19341/Health/HealthCheck",
- "ConsulAddress": "http://127.0.0.1:8500"
- }
注意名字保持一致才会注入到同一个服务中,然后ip,端口这些写成当前项目的即可
写一个测试接口
- [Route("api/[controller]")]
- [ApiController]
- public class TestController : ControllerBase
- {
- // GET: api/Test
- [HttpGet]
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
- }
-
- // GET: api/Test/5
- [HttpGet("{id}")]
- public string Get(int id)
- {
- return "9341:value";
- }
- }
其他的什么健康检查和服务注入扩展方法这些和上篇的一样就不说了,做完这些我们去consul看看我们的服务
点击进去可以看到这个下面有两个服务了,注意看地址是两个不同的地址
然后调用即可,代码和前面测试调用的一样
我们用随机数简单的模拟一下负载均衡
- public IActionResult Index()
- {
-
- var url = _configuration.GetSection("Consul")["ConsulAddress"].ToString();
-
- using (var consulClient = new ConsulClient(a => a.Address = new Uri(url)))
- {
- var services = consulClient.Catalog.Service("axj").Result.Response;
- if (services != null && services.Any())
- {
- // 模拟随机一台进行请求,这里只是测试,可以选择合适的负载均衡框架
- Random r = new Random();
- int index = r.Next(services.Count());
- var service = services.ElementAt(index);
-
- using (HttpClient client = new HttpClient())
- {
- var response = client.GetAsync($"http://{service.ServiceAddress}:{service.ServicePort}/api/Test/520").Result;
- string result = response.Content.ReadAsStringAsync().Result;
- ViewBag.msg = result;
- }
- }
- else
- {
- ViewBag.msg = "未找到服务";
- }
- }
- return View();
- }
效果如下:
两个接口都会被随机的调用
当然服务可以注入很多个:
项目下载地址:https://download.tnblog.net/resource/index/a049e06f93e140afbf5ab9a026b0ad21
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价