.netcore 使用surging框架发布到docker
作者:landonzeng 发布时间:2023-12-21 17:28:00
demo运行在windows的docker中,系统是win10,所以需要先下载Docker for Windows,安装完毕后系统会重启,然后桌面上可以找到Docker for Windows的快捷图标,右下角有个鲸鱼小图标
单击右键,选择菜单中的Kitematic
会提示你下载Kitematic,自行下载后解压即可,将Kitematic快捷到桌面;
打开Kitematic,在搜索栏中下载好RabbitMQ、redis、consul相关镜像,因为这些是surging运行的先决条件。
接着去GitHub上下载surging网关项目,修改其中的gatewaySettings.json中Register下的Address地址,对应的事consul docker镜像的ip
具体如何查看其ip,看如下操作:
打开Kitematic,点击左下角,如图:
进入到命令窗口,输入docker container ls或者 docker ps -a 查看docker,
可以看到现在运行的docker的相关信息,
如图:
然后查看consul的相关配置,输入docker inspect 镜像的 containerID,如consul的id是b0e98b94638c,输入命令如下:docker inspect b0e98b94638c,
显示这个docker的配置,内容会很多,不过ip的信息在最后,如图
找到其中的ip是多少,然后修改surging网关中的consul地址为:"Address": "172.17.0.4:8500",其他配置根据上面的操作进行修改,如redis 镜像地址的查找和修改等;
修改好surging的网关配置后在Surging.ApiGateway项目上单击右键,由于我项目中已经添加过,所以该处为灰色,如图:
新建docker-Compose后修改其中docker-compose.yml的配置如下:
在后面添加docker的外部端口和内部端口的映射和网络模式,这里我们都使用桥接模式,包括之前的consul、RabbitMQ、redis都是同一模式,这样他们会在同一VLAN下,
然后运行网关,如下:
接下来新建一个解决方案,方案名随意,喜欢就好,由于时间比较短,这里我简单的处理,不清楚的可以留言
新建Service.A,然后在其下新建控制台应用Service.A、Service.B、Service.C,新建类库Service.A.Service、Service.B.Service、Service.C.Service;
编辑Service.A.csporj、Service.B.csporj、Service.C.csporj,如下
将其中的引用包都复制过去,分别修改一下对应的Service,即其中的<ProjectReference Include="..\Service.A.Service\Service.A.Service.csproj" />,Service.A引用Service.A.Service,Service.B引用Service.B.Service
Service.C引用Service.C.Service;
类库Service.A.Service、Service.B.Service、Service.C.Service中都引用
<PackageReference Include="surging" Version="0.5.4" />
如图:
由于代码很多地方相识,以下我只说Service.A,和Service.A.Service;
Service.A 中新增Configs文件夹,下面添加log4net.config,log4net.config代码如下:
<log4net>
<root>
<level value="Error" />
<!-- <appender-ref ref="RollingLogFileAppender" /> -->
<appender-ref ref="ErrorRollingLogFileAppender" />
</root>
<appender name="ErrorRollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net" LEVEL="ERROR">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="c:\surging\Error\" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="_yyyyMMddHH.TXT" />
<param name="StaticLogFileName" value="false" />
<param name="MaxSizeRollBackups" value="-1" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
</log4net>
然后新增cacheSettings.json其中Map:Properties下的value的值是redis地址
{
"CachingSettings": [
{
"Id": "ddlCache",
"Class": "Surging.Core.Caching.RedisCache.RedisContext,Surging.Core.Caching",
"Properties": [
{
"Name": "appRuleFile",
"Ref": "rule"
},
{
"Name": "dataContextPool",
"Ref": "ddls_sample",
"Maps": [
{
"Name": "Redis",
"Properties": [
{
"value": "172.17.0.2:6379::1"
}
]
},
{
"Name": "MemoryCache"
}
]
},
{
"Name": "defaultExpireTime",
"value": "120"
},
{
"Name": "connectTimeout",
"Value": "120"
},
{
"Name": "minSize",
"Value": "1"
},
{
"Name": "maxSize",
"Value": "10"
}
]
}
]
}
新增eventBusSettings.json,其中的EventBusConnection对应的是RabbitMQ docker的地址
{
"EventBusConnection": "172.17.0.3",
"EventBusUserName": "guest",
"EventBusPassword": "guest"
}
Program.cs的代码如下
using Autofac;
using Surging.Core.Codec.MessagePack;
using Surging.Core.Consul;
using Surging.Core.Consul.Configurations;
using Surging.Core.CPlatform;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.DotNetty;
using Surging.Core.EventBusRabbitMQ;
using Surging.Core.Log4net;
using Surging.Core.ProxyGenerator;
using Surging.Core.ServiceHosting;
using Surging.Core.ServiceHosting.Internal.Implementation;
using System;
using System.Text;
namespace Service.A
{
class Program
{
static void Main(string[] args)
{
NewMethod();
}
private static void NewMethod()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var host = new ServiceHostBuilder()
.RegisterServices(builder =>
{
builder.AddMicroService(option =>
{
option.AddServiceRuntime();
option.AddRelateService();
//option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181"));
option.UseConsulManager(new ConfigInfo("172.17.0.4:8500"));
option.UseDotNettyTransport();
option.UseRabbitMQTransport();
option.AddRabbitMQAdapt();
//option.UseProtoBufferCodec();
option.UseMessagePackCodec();
builder.Register(p => new CPlatformContainer(ServiceLocator.Current));
});
})
.SubscribeAt()
.UseLog4net("Configs/log4net.config")
//.UseServer("127.0.0.1", 98)
//.UseServer("127.0.0.1", 98,“true”) //自动生成Token
//.UseServer("127.0.0.1", 98,“123456789”) //固定密码Token
.UseServer(options =>
{
options.Ip = "172.17.0.6";
options.Port = 9990;
options.Token = "True";
options.ExecutionTimeoutInMilliseconds = 30000;
options.MaxConcurrentRequests = 200;
options.NotRelatedAssemblyFiles = "Centa.Agency.Application.DTO\\w*|StackExchange.Redis\\w*";
})
.UseProxy()
.UseStartup<Startup>()
.Build();
using (host.Run())
{
Console.WriteLine($"服务端启动成功,{DateTime.Now}。");
}
}
}
}
新增Startup.cs
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Surging.Core.Caching.Configurations;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.EventBusRabbitMQ.Configurations;
using System;
namespace Service.A
{
public class Startup
{
public Startup()
{
var config = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory);
ConfigureEventBus(config);
//ConfigureCache(config);
}
public IContainer ConfigureServices(ContainerBuilder builder)
{
var services = new ServiceCollection();
ConfigureLogging(services);
builder.Populate(services);
ServiceLocator.Current = builder.Build();
return ServiceLocator.Current;
}
public void Configure(IContainer app)
{
app.Resolve<ILoggerFactory>()
.AddConsole((c, l) => (int)l >= 3);
}
#region 私有方法
/// <summary>
/// 配置日志服务
/// </summary>
/// <param name="services"></param>
private void ConfigureLogging(IServiceCollection services)
{
services.AddLogging();
}
private static void ConfigureEventBus(IConfigurationBuilder build)
{
build
.AddEventBusFile("eventBusSettings.json", optional: false);
}
/// <summary>
/// 配置缓存服务
/// </summary>
private void ConfigureCache(IConfigurationBuilder build)
{
build
.AddCacheFile("cacheSettings.json", optional: false);
}
#endregion
}
}
Service.A.Service 类库下新增AService.cs
using Surging.Core.ProxyGenerator;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Service.A.Service
{
public class AService:ProxyServiceBase,IAService
{
public Task<string> SayHello(string name)
{
return Task.FromResult($"{name} say : hello");
}
}
}
新增IAService.cs
using Surging.Core.CPlatform.Ioc;
using Surging.Core.CPlatform.Runtime.Server.Implementation.ServiceDiscovery.Attributes;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Service.A.Service
{
[ServiceBundle("api/{Service}")]
public interface IAService : IServiceKey
{
Task<string> SayHello(string name);
}
}
其他类库和服务与以上代码基本无二,这里不在赘述。不清楚的可以留言
所有代码都处理好后,在Service.A、Service.B、Service.C项目上右键新增docker支持文件,然后会生成一下文件
修改其中的docker-compose.yml
version: '3'
services:
service.a:
image: servicea
ports:
- "127.0.0.1:9990:9990"
network_mode: "bridge"
build:
context: .
dockerfile: Service.A/Dockerfile
service.b:
image: serviceb
ports:
- "127.0.0.1:9991:9991"
network_mode: "bridge"
build:
context: .
dockerfile: Service.B/Dockerfile
service.c:
image: servicec
ports:
- "127.0.0.1:9992:9992"
network_mode: "bridge"
build:
context: .
dockerfile: Service.C/Dockerfile
webapplication1:
image: webapplication1
build:
context: .
dockerfile: ../WebApplication1/Dockerfile
然后选择docker运行即可
最后访问surging网关,即可看见效果
来源:https://www.cnblogs.com/landonzeng/p/8612026.html
猜你喜欢
- 网络罪犯最近翻新分散式阻断服务(DDOS)攻击的手法,开始用形同互联网黄页的域名系统服务器来发动攻击,扰乱在线商务。VeriSign上周
- 随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多。但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当
- 为了庆祝今天ID解封,特发一篇小教程,针对新手的,讲错的地方欢迎指正,下面开始教程. 在网上流传很广,坛子里也有几个人都转过,这里
- 如何制作高质量的链接?也就是链接的权重问题。链接建设分外部链接和内部链接两个部分,本文将围绕这两个方面对SEO链接进行简单分析,让大家明白,
- 怎么删除docker里建立的容器1、首先使用docker -s -a命令列出所有容器# docker ps -aCONTAINER ID &
- 最近总有朋友问我同一个问题:如何才能让discuz 7.2 发帖和回复的时候直接采用高级模式,而不是默认是那个弹窗模式,如下图所示:如果站长
- 鉴于支付宝等服务无视我们Linux用户的存在,没办法,那只好在Linux上用VirtualBox 虚拟一个Windows系统了。系
- 网站在搜索引擎中被K掉(处理),如网站首页搜索没有收录、网站收录被拔掉、排名下降、都是网站被K的现象。网站被K是广大站长最为头疼的事之一,总
- IBM eServer iSeries 520 Express Edition 与中型企业不断增长的需求相适应,它不仅可以提供运行核心业务应
- 足球可以功夫,熊猫可以功夫,奥运网站为什么不能功夫?在华中师大,一群自称“13侠”的大学生办起了一个以奥运为主题,却颇有武侠味的“奥林客栈”
- VMware Workstation 不可恢复错误: (vcpu-1) Exception 0xc0000005 (access violation) has occurred终极解决方案(最新推荐)问题VMware Workstation 不可恢复错误: (vcpu-0) Exception 0xc0000005 (access vio
- 我们一直致力于为发布商提供充足的数据信息,从而让您更好地了解自己的网站运营和广告效果。现在我们很高兴地告诉大家,AdSense 帐户将会融入
- Ask.com在搜索引擎市场增长迅猛,刚刚传出消息其将耗资1亿美元于两年内进入中国市场建立中文版,就个人感觉,Ask在用户体验方面整体上已经
- 最近有一个用户提出想以文章的标题前缀筛选出文章,想定制一个标签,殊不知这功能在我们系统的标签已经能轻易实现,那最常用的标签“通用信息列表”为
- 1 VMware网卡VMware默认情况下会安装三个网卡,如下图示(点击最上方编辑-->虚拟网络编辑器)。VMnet0:“桥接网络”
- 开始写之前,我先提几个问题,希望大这思考一下:1. 什么是软文?2. 软文有什么用?3. 软文的重要性?4. 怎样写软文?5. 用软文来推广
- 需要注意的是,编译apache时候必须加入了DSO支持,如果没有,请加入 --enable-module=so 选项重新编译apache安装
- 本文主要给大家介绍了关于在centos 7安装Jenkins的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:安装添加yu
- 10月13日凌晨消息,360安全卫士内测手机版安全软件悄然对外推出:手机访问m.360.cn可免费下载名为“360手机卫
- 我们经常用FTP共享文件,往往有时候会发生FTP空间不足的情况,这时候就不能共享资源了。如何对FTP空间的剩余数量进行监控呢?一旦出现空间不