Asp.Net Core Ocelot Consul 微服务
做一個簡單的微服務架構如下圖:
這個圖表示的是一個網(wǎng)關代理Consul的兩個服務,consul每個服務注冊集群
安裝 Consul的服務,這里安裝單機版的,集群版配置最低要求(3個Consul server)的需要三臺虛擬機,窮
這是下載地址 Consul 我這里部署的是CentOS7 ip是:192.168.31.140 記得關閉防火墻
圖一
圖二
接下來寫ServiceUser的服務,選擇asp.net core Api模板創(chuàng)建項目,安裝Consul包
添加一個健康檢查的API直接返回OK
再添加一個返回用戶數(shù)據(jù)的API
在寫一個服務注冊的方法在Startup.cs里,ip和port及id我是從命令行獲取的其他配置均寫在配置文件里
private static void ServiceRegister(IConfiguration configuration){ConsulClient client = new ConsulClient(new Action<ConsulClientConfiguration>(t => {t.Address = new Uri(configuration["consul:servicesAddr"]);//這是Consul的服務地址192.168.31.140t.Datacenter = configuration["consul:datacenter"];//儲存名}));//注冊一個實例var result = client.Agent.ServiceRegister(new AgentServiceRegistration(){Address = configuration["ip"], //注冊服務的IPID = $"{configuration["consul:serviceName"]}{configuration["id"]}",//服務id唯一的Name = configuration["consul:serviceName"],//服務名Port = Convert.ToInt32(configuration["port"]),//端口Tags = null,Check = new AgentServiceCheck(){HTTP = $"http://{configuration["ip"]}:{configuration["port"]}{configuration["consul:healthCheck"]}",//健康檢查的API地址Interval = new TimeSpan(0, 0, 10),//間隔多少檢查一次DeregisterCriticalServiceAfter = new TimeSpan(0, 1, 0) //多久注銷不健康的服務}}).Result;}獲取命令行參數(shù) 寫在main 方法里如下圖
編寫配置文件
"consul": {"servicesAddr": "http://192.168.31.140:8500","datacenter": "dr1","serviceName": "ServiceCommodity","healthCheck": "/api/Health"}ServiceCommodity和ServiceUser一樣
接下來啟動實例注冊服務,我這里用端口區(qū)分不同的實例
接下來注冊Ocelot網(wǎng)關,微服務是離不開網(wǎng)關的,現(xiàn)在可能看不出網(wǎng)關的效果,Consul搭建成集群就能看出效果了
創(chuàng)建一個空的web項目安裝如下兩個包
在Startup.cs 里注冊
創(chuàng)建ocelot.json配置文件
{"ReRoutes": [{"DownstreamPathTemplate": "/api/{controller}",//你的api路徑"DownstreamScheme": "http","UpstreamPathTemplate": "/dust/{controller}",//你映射的路徑"UpstreamHttpMethod": [ "get", "post" ],//請求方法"ServiceName": "ServiceCommodity",//你的服務名稱"LoadBalancerOptions": {"Type": "LeastConnection"//ocelot提供了幾種均衡方法,這里用的是最小連接}},{"DownstreamPathTemplate": "/api/{controller}","DownstreamScheme": "http","UpstreamPathTemplate": "/dust1/{controller}","UpstreamHttpMethod": [ "get", "post" ],"ServiceName": "ServiceUser","LoadBalancerOptions": {"Type": "LeastConnection"}}],"GlobalConfiguration": {"ServiceDiscoveryProvider": {"Host": "192.168.31.140",//你的Consul的ip地址"Port": 8500,//你的Consul的端口"ConfigurationKey": "Consul"//指定Consul,ocelot提供了幾種,可以去官網(wǎng)看看}} }加載配置文件
.ConfigureAppConfiguration((hostingContext, config) =>{config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath).AddJsonFile("appsettings.json", true, true).AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true).AddJsonFile("ocelot.json").AddEnvironmentVariables();}) 啟動 dotnet GateWayOcelot.dll --urls http://*:7000最后應用請求網(wǎng)關
到此一個簡單的微服務OK了
Demo下載地址:https://github.com/Xiao-Dust/MicroService
往期精彩回顧
.netcore consul實現(xiàn)服務注冊與發(fā)現(xiàn)(一)單機部署
.netcore consul實現(xiàn)服務注冊與發(fā)現(xiàn)(二)集群完整版
基于Docker的Consul服務發(fā)現(xiàn)集群搭建
Ocelot簡易教程(五)之集成IdentityServer認證以及授權
點擊【在看】+轉(zhuǎn)發(fā)【朋友圈】對我最大的支持
總結
以上是生活随笔為你收集整理的Asp.Net Core Ocelot Consul 微服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Xamarin.Forms读取并展示An
- 下一篇: .Neter们,你真的应该了解下EFCo