欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

程序员文章站 2022-06-05 18:45:49
部署consul-docker镜像 先搜索consul的docker镜像 然后选择了第一个,也就是官方镜像 下载镜像 然后运行镜像 docker run -d --name consul -v /home/root/config:/config --restart=always\ -p 8300:8 ......

部署consul-docker镜像

先搜索consul的docker镜像

docker search consul

然后选择了第一个,也就是官方镜像

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

 

 下载镜像

docker pull consul

然后运行镜像

docker run -d --name consul -v /home/root/config:/config --restart=always\
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302 \
-p 8302:8302/udp \
-p 8400:8400 \
-p 8500:8500 \
consul agent -server \
-bootstrap-expect 1 \
-ui \
-client 0.0.0.0

 

consul中每个启动参数的含义,参考了以下链接:

https://www.cnblogs.com/pearlran/p/11225953.html

 

注册服务

参考链接:

 

新建一个common项目

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

 

  

新建consulbuilderextensions.cs 、consulservice.cs、healthservice.cs

 1 using consul;
 2 using microsoft.aspnetcore.builder;
 3 using microsoft.aspnetcore.hosting;
 4 using system;
 5 using system.collections.generic;
 6 using system.linq;
 7 using system.threading.tasks;
 8 
 9 namespace test.webapi.common
10 {
11     public static class consulbuilderextensions
12 
13     {
14 
15         // 服务注册
16 
17         public static iapplicationbuilder registerconsul(this iapplicationbuilder app, iapplicationlifetime lifetime, healthservice healthservice, consulservice consulservice)
18 
19         {
20 
21             var consulclient = new consulclient(x => x.address = new uri($"http://{consulservice.ip}:{consulservice.port}"));//请求注册的 consul 地址
22 
23             var httpcheck = new agentservicecheck()
24 
25             {
26 
27                 deregistercriticalserviceafter = timespan.fromseconds(5),//服务启动多久后注册
28 
29                 interval = timespan.fromseconds(10),//健康检查时间间隔,或者称为心跳间隔
30 
31                 http = $"http://{healthservice.ip}:{healthservice.port}/api/health",//健康检查地址
32 
33                 timeout = timespan.fromseconds(5)
34 
35             };
36 
37             // register service with consul
38 
39             var registration = new agentserviceregistration()
40 
41             {
42 
43                 checks = new[] { httpcheck },
44 
45                 id = healthservice.name + "_" + healthservice.port,
46 
47                 name = healthservice.name,
48 
49                 address = healthservice.ip,
50 
51                 port = healthservice.port,
52 
53                 tags = new[] { $"urlprefix-/{healthservice.name}" }//添加 urlprefix-/servicename 格式的 tag 标签,以便 fabio 识别
54 
55             };
56 
57             consulclient.agent.serviceregister(registration).wait();//服务启动时注册,内部实现其实就是使用 consul api 进行注册(httpclient发起)
58 
59             lifetime.applicationstopping.register(() =>
60 
61             {
62 
63                 consulclient.agent.servicederegister(registration.id).wait();//服务停止时取消注册
64 
65             });
66 
67             return app;
68 
69         }
70 
71     }
72 }

 

 1 namespace test.webapi.common
 2 {
 3     public class consulservice
 4 
 5     {
 6 
 7         public string ip { get; set; }
 8 
 9         public int port { get; set; }
10 
11     }
12 }

 

 1 namespace test.webapi.common
 2 {
 3     public class healthservice
 4     {
 5         public string name { get; set; }
 6 
 7         public string ip { get; set; }
 8 
 9         public int port { get; set; }
10     }
11 }

 

两个webapi项目引用这个common项目

并修改各自的 startup.cs

 1 public void configure(iapplicationbuilder app, ihostingenvironment env, iapplicationlifetime lifetime)
 2         {
 3             if (env.isdevelopment())
 4             {
 5                 app.usedeveloperexceptionpage();
 6             }
 7             else
 8             {
 9                 // the default hsts value is 30 days. you may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
10                 app.usehsts();
11             }
12             consulservice consulservice = new consulservice()
13             {
14                 ip = configuration["consul:ip"],
15                 port = convert.toint32(configuration["consul:port"])
16             };
17             healthservice healthservice = new healthservice()
18             {
19                 ip = configuration["service:ip"],
20                 port = convert.toint32(configuration["service:port"]),
21                 name = configuration["service:name"],
22             };
23 
24             app.registerconsul(lifetime, healthservice, consulservice);
25 
26             //app.useconsul();
27             app.usehttpsredirection();
28             app.usemvc();
29         }

修改 appsettings.json。 192.168.2.16是本机地址。192.168.2.29是docker中consul的地址。 两个项目的配置类似,区别是本地项目的端口9001、9002。

 1 {
 2   "logging": {
 3     "loglevel": {
 4       "default": "warning"
 5     }
 6   },
 7   "allowedhosts": "*",
 8 
 9   "service": {
10     "name": "apiservice",
11     "ip": "192.168.2.16",
12     "port": "9001"
13   },
14   "consul": {
15     "ip": "192.168.2.29",
16     "port": "8500"
17   }
18 }

 

iis部署 .net core 2.2

参考链接:

 

部署期间遇到过以下问题

http error 500.35 - ancm multiple in-process applications in same process asp.net core 3

解决方法:两个webapi项目 用不一样的应用池。

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

 

 

部署完成后,测试下 各自项目的 /api/health接口是否正常。

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

 

 

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

测试网关项目

修改网关项目的配置configuration.json

 1 {
 2   "reroutes": [
 3     {
 4       "useservicediscovery": true,
 5       "downstreampathtemplate": "/{url}",
 6       "downstreamscheme": "http",
 7       "servicename": "apiservice",
 8       "loadbalanceroptions": {
 9         "type": "roundrobin"
10       },
11       "upstreampathtemplate": "/{url}",
12       "upstreamhttpmethod": [ "get" ],
13       "reroutescasesensitive": false
14     }
15   ],
16   "globalconfiguration": {
17     "servicediscoveryprovider": {
18       "host": "192.168.2.29",
19       "port": 8500,
20       "type": "pollconsul",
21       "pollinginterval": 100
22     }
23   }
24 }

修改 startup.cs

 1 public void configureservices(iservicecollection services)
 2         {
 3             services.addmvc().setcompatibilityversion(compatibilityversion.version_2_2);
 4             services.addocelot(new configurationbuilder()
 5                     .addjsonfile("configuration.json")
 6                     .build())
 7                     .addconsul();
 8         }
 9 
10         // this method gets called by the runtime. use this method to configure the http request pipeline.
11         public async void configure(iapplicationbuilder app, ihostingenvironment env)
12         {
13             if (env.isdevelopment())
14             {
15                 app.usedeveloperexceptionpage();
16             }
17             else
18             {
19                 // the default hsts value is 30 days. you may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
20                 app.usehsts();
21             }
22             await app.useocelot();
23             app.usehttpsredirection();
24             app.usemvc();
25         }

f5启动项目

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

 

 

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

 

 

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul

 

 

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul