這篇文章主要介紹了.netcore如何使用surging框架發(fā)布到docker中的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇.netcore如何使用surging框架發(fā)布到docker中文章都會有所收獲,下面我們一起來看看吧。
岳麓ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
demo運行在windows的docker中,系統(tǒng)是win10,所以需要先下載docker for windows,安裝完畢后系統(tǒng)會重啟,然后桌面上可以找到docker for windows的快捷圖標,右下角有個鯨魚小圖標
單擊右鍵,選擇菜單中的kitematic
會提示你下載kitematic,自行下載后解壓即可,將kitematic快捷到桌面;
打開kitematic,在搜索欄中下載好rabbitmq、redis、consul相關(guān)鏡像,因為這些是surging運行的先決條件。
接著去github上下載surging網(wǎng)關(guān)項目,修改其中的gatewaysettings.json中register下的address地址,對應的事consul docker鏡像的ip
具體如何查看其ip,看如下操作:
打開kitematic,點擊左下角,如圖:
進入到命令窗口,輸入docker container ls或者 docker ps -a 查看docker,
可以看到現(xiàn)在運行的docker的相關(guān)信息,
如圖:
然后查看consul的相關(guān)配置,輸入docker inspect 鏡像的 containerid,如consul的id是b0e98b94638c,輸入命令如下:docker inspect b0e98b94638c,
顯示這個docker的配置,內(nèi)容會很多,不過ip的信息在最后,如圖
找到其中的ip是多少,然后修改surging網(wǎng)關(guān)中的consul地址為:"address": "172.17.0.4:8500",其他配置根據(jù)上面的操作進行修改,如redis 鏡像地址的查找和修改等;
修改好surging的網(wǎng)關(guān)配置后在surging.apigateway項目上單擊右鍵,由于我項目中已經(jīng)添加過,所以該處為灰色,如圖:
新建docker-compose后修改其中docker-compose.yml的配置如下:
在后面添加docker的外部端口和內(nèi)部端口的映射和網(wǎng)絡模式,這里我們都使用橋接模式,包括之前的consul、rabbitmq、redis都是同一模式,這樣他們會在同一vlan下,
然后運行網(wǎng)關(guān),如下:
接下來新建一個解決方案,方案名隨意,喜歡就好,由于時間比較短,這里我簡單的處理,不清楚的可以留言
新建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,即其中的
service.c引用service.c.service;
類庫service.a.service、service.b.service、service.c.service中都引用
如圖:
由于代碼很多地方相識,以下我只說service.a,和service.a.service;
service.a 中新增configs文件夾,下面添加log4net.config,log4net.config代碼如下:
然后新增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() .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() .addconsole((c, l) => (int)l >= 3); } #region 私有方法 /// /// 配置日志服務 /// /// private void configurelogging(iservicecollection services) { services.addlogging(); } private static void configureeventbus(iconfigurationbuilder build) { build .addeventbusfile("eventbussettings.json", optional: false); } ////// 配置緩存服務 /// 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 tasksayhello(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 { tasksayhello(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網(wǎng)關(guān),即可看見效果
關(guān)于“.netcore如何使用surging框架發(fā)布到docker中”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“.netcore如何使用surging框架發(fā)布到docker中”知識都有一定的了解,大家如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。