部署consul-docker镜像

先搜索consul的docker镜像

docker search 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.bitdoom.com/2017/09/07/p125/

https://yq.aliyun.com/articles/536508

https://blog.csdn.net/yinwaner/article/details/80762757

https://blog.csdn.net/qq_36228442/article/details/89085373

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

https://www.cnblogs.com/magic-chenyang/p/7975677.html

注册服务

参考链接:

https://blog.csdn.net/hailang2ll/article/details/82079192

新建一个common项目

新建ConsulBuilderExtensions.cs 、ConsulService.cs、HealthService.cs

 using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace Test.WebApi.Common
{
public static class ConsulBuilderExtensions { // 服务注册 public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IApplicationLifetime lifetime, HealthService healthService, ConsulService consulService) { var consulClient = new ConsulClient(x => x.Address = new Uri($"http://{consulService.IP}:{consulService.Port}"));//请求注册的 Consul 地址 var httpCheck = new AgentServiceCheck() { DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(),//服务启动多久后注册 Interval = TimeSpan.FromSeconds(),//健康检查时间间隔,或者称为心跳间隔 HTTP = $"http://{healthService.IP}:{healthService.Port}/api/health",//健康检查地址 Timeout = TimeSpan.FromSeconds() }; // Register service with consul var registration = new AgentServiceRegistration() { Checks = new[] { httpCheck }, ID = healthService.Name + "_" + healthService.Port, Name = healthService.Name, Address = healthService.IP, Port = healthService.Port, Tags = new[] { $"urlprefix-/{healthService.Name}" }//添加 urlprefix-/servicename 格式的 tag 标签,以便 Fabio 识别 }; consulClient.Agent.ServiceRegister(registration).Wait();//服务启动时注册,内部实现其实就是使用 Consul API 进行注册(HttpClient发起) lifetime.ApplicationStopping.Register(() => { consulClient.Agent.ServiceDeregister(registration.ID).Wait();//服务停止时取消注册 }); return app; } }
}
 namespace Test.WebApi.Common
{
public class ConsulService { public string IP { get; set; } public int Port { get; set; } }
}
 namespace Test.WebApi.Common
{
public class HealthService
{
public string Name { get; set; } public string IP { get; set; } public int Port { get; set; }
}
}

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

并修改各自的 startup.cs

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
ConsulService consulService = new ConsulService()
{
IP = Configuration["Consul:IP"],
Port = Convert.ToInt32(Configuration["Consul:Port"])
};
HealthService healthService = new HealthService()
{
IP = Configuration["Service:IP"],
Port = Convert.ToInt32(Configuration["Service:Port"]),
Name = Configuration["Service:Name"],
}; app.RegisterConsul(lifetime, healthService, consulService); //app.UseConsul();
app.UseHttpsRedirection();
app.UseMvc();
}

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

 {
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*", "Service": {
"Name": "ApiService",
"IP": "192.168.2.16",
"Port": ""
},
"Consul": {
"IP": "192.168.2.29",
"Port": ""
}
}

IIS部署 .NET CORE 2.2

参考链接:

https://www.cnblogs.com/wxlv/p/netcore-to-iis.html

部署期间遇到过以下问题

HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process ASP.NET Core 3

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

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

测试网关项目

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

 {
"ReRoutes": [
{
"UseServiceDiscovery": true,
"DownstreamPathTemplate": "/{url}",
"DownstreamScheme": "http",
"ServiceName": "ApiService",
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
"UpstreamPathTemplate": "/{url}",
"UpstreamHttpMethod": [ "Get" ],
"ReRoutesCaseSensitive": false
}
],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Host": "192.168.2.29",
"Port": ,
"Type": "PollConsul",
"PollingInterval":
}
}
}

修改 startup.cs

 public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddOcelot(new ConfigurationBuilder()
.AddJsonFile("configuration.json")
.Build())
.AddConsul();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
await app.UseOcelot();
app.UseHttpsRedirection();
app.UseMvc();
}

F5启动项目

03 .NET CORE 2.2 使用OCELOT -- Docker中的Consul的更多相关文章

  1. .NET Core Web 应用部署到 Docker 中运行

    环境介绍 : 虚拟机:VirtualBox 5.1.6 系 统:Ubuntu 16.04.1 LTS 系统准备完成后可以使用 sudo apt-get udpate 和 sudo apt-get up ...

  2. Asp.Net Core WebAPI+PostgreSQL部署在Docker中

     PostgreSQL是一个功能强大的开源数据库系统.它支持了大多数的SQL:2008标准的数据类型,包括整型.数值值.布尔型.字节型.字符型.日期型.时间间隔型和时间型,它也支持存储二进制的大对像, ...

  3. [Linux之旅一] .NET Core 2.2部署到Docker中

    第一步,使用VS2017或者VS2019创建.NET Core 2.2或3.1的项目,如下图: 在创建项目的时候记得勾选Docker支持,这样会自动创建Dockerfile文件,这个文件用于构建Doc ...

  4. .net core Ocelot实现API网关并部署在docker中

    基于Ocelot(http://ocelot.readthedocs.io)搭建的API网关demo 软件以及系统版本:  Asp.Net Core 2.2 Ocelot 13.5.0 CentOS ...

  5. 在docker中运行ASP.NET Core Web API应用程序

    本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...

  6. 将 ASP.NET Core 1.0 应用作为 docker 镜像发布 (Linux版)

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  7. docker中运行ASP.NET Core Web API

    在docker中运行ASP.NET Core Web API应用程序 本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过 ...

  8. ASP.NET Core 网站在Docker中运行

    Docker作为新一代的虚拟化方式,未来肯定会得到广泛的应用,传统虚拟机的部署方式要保证开发环境.测试环境.UAT环境.生产环境的依赖一致性,需要大量的运维人力,使用Docker我们可以实现一次部署, ...

  9. .net core微服务之基于Docker+Consul+Registrator服务注册服务发现

    一.Docker部分: 先拉最新的asp.net core的镜像: docker pull microsoft/aspnetcore 将下载下来的镜像重命名,为什么要重命名?等会讲Registrato ...

随机推荐

  1. ElasticSearch最全分词器比较及使用方法

    介绍:ElasticSearch 是一个基于 Lucene 的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口.Elasticsearch 是用 Java 开 ...

  2. EM算法直观认识

    Expectation Maximization, 字面翻译为, "最大期望". 我个人其实一直都不太理解EM算法, 从我个人的渊源来看, 之前数理统计里面的参数估计, 也是没有太 ...

  3. go-torch安装和使用

    go-torch安装 1.安装FlameGraph脚本 git clone https://github.com/brendangregg/FlameGraph.git cp FlameGraph/f ...

  4. 如何解决phpMyAdmin缺少mcrypt 扩展

    出现问题:在安装配置phpMyAdmin管理mysql数据库的时候,打开phpMyAdmin登录页面,出现下面的错误提示: 缺少 mcrypt 扩展.请检查 PHP 配置 以CentOS 6.0系统为 ...

  5. qos-server can not bind localhost:22222, dubbo version: 2.6.0, current host: 127.0.0.1【问题解决】

    好吧,这个问题比较low,但是记录一下,以免后期遗忘. 说白了,这个问题就是端口被占用了. 问题: qos-server can not bind localhost:22222, dubbo ver ...

  6. 解决chrome插件安装时出现的“程序包无效”问题信息:程序包无效。

    https://blog.csdn.net/bluexuemei/article/details/35213117 2014-06-27 09:00:51 bluexuemei 阅读数 14374更多 ...

  7. Bayesian Optimization使用Hyperopt进行参数调优

    超参数优化 Bayesian Optimization使用Hyperopt进行参数调优 1. 前言 本文将介绍一种快速有效的方法用于实现机器学习模型的调参.有两种常用的调参方法:网格搜索和随机搜索.每 ...

  8. c# 第8节 变量、变量名命令规则、作用域、@的作用

    本节内容: 1:变量 2:变量名命令规则 3:常量 4:变量和常量的作用域 5:@的作用 1:变量是什么? 计算即中存储变量的也是三步骤: 实例: 2:变量命令规则 练习: 3.常量 实现: 4:变量 ...

  9. 201871010102-常龙龙《面向对象程序设计(java)》第十七周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  10. PAC在异常检测中的应用

    注:资料均来源于网络,本文只做知识分享,如侵立删,谢谢. PAC算法背景简述: 在许多领域的研究与应用中,通常需要对含有多个变量的数据进行观测,收集大量数据后进行分析寻找规律.多变量大数据集无疑会为研 ...