您好,登錄后才能下訂單哦!
這篇文章主要介紹了.netcore如何使用surging框架發(fā)布到docker中的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇.netcore如何使用surging框架發(fā)布到docker中文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
demo運(yùn)行在windows的docker中,系統(tǒng)是win10,所以需要先下載docker for windows,安裝完畢后系統(tǒng)會(huì)重啟,然后桌面上可以找到docker for windows的快捷圖標(biāo),右下角有個(gè)鯨魚小圖標(biāo)
單擊右鍵,選擇菜單中的kitematic
會(huì)提示你下載kitematic,自行下載后解壓即可,將kitematic快捷到桌面;
打開(kāi)kitematic,在搜索欄中下載好rabbitmq、redis、consul相關(guān)鏡像,因?yàn)檫@些是surging運(yùn)行的先決條件。
接著去github上下載surging網(wǎng)關(guān)項(xiàng)目,修改其中的gatewaysettings.json中register下的address地址,對(duì)應(yīng)的事consul docker鏡像的ip
具體如何查看其ip,看如下操作:
打開(kāi)kitematic,點(diǎn)擊左下角,如圖:
進(jìn)入到命令窗口,輸入docker container ls或者 docker ps -a 查看docker,
可以看到現(xiàn)在運(yùn)行的docker的相關(guān)信息,
如圖:
然后查看consul的相關(guān)配置,輸入docker inspect 鏡像的 containerid,如consul的id是b0e98b94638c,輸入命令如下:docker inspect b0e98b94638c,
顯示這個(gè)docker的配置,內(nèi)容會(huì)很多,不過(guò)ip的信息在最后,如圖
找到其中的ip是多少,然后修改surging網(wǎng)關(guān)中的consul地址為:"address": "172.17.0.4:8500",其他配置根據(jù)上面的操作進(jìn)行修改,如redis 鏡像地址的查找和修改等;
修改好surging的網(wǎng)關(guān)配置后在surging.apigateway項(xiàng)目上單擊右鍵,由于我項(xiàng)目中已經(jīng)添加過(guò),所以該處為灰色,如圖:
新建docker-compose后修改其中docker-compose.yml的配置如下:
在后面添加docker的外部端口和內(nèi)部端口的映射和網(wǎng)絡(luò)模式,這里我們都使用橋接模式,包括之前的consul、rabbitmq、redis都是同一模式,這樣他們會(huì)在同一vlan下,
然后運(yùn)行網(wǎng)關(guān),如下:
接下來(lái)新建一個(gè)解決方案,方案名隨意,喜歡就好,由于時(shí)間比較短,這里我簡(jiǎn)單的處理,不清楚的可以留言
新建service.a,然后在其下新建控制臺(tái)應(yīng)用service.a、service.b、service.c,新建類庫(kù)service.a.service、service.b.service、service.c.service;
編輯service.a.csporj、service.b.csporj、service.c.csporj,如下
將其中的引用包都復(fù)制過(guò)去,分別修改一下對(duì)應(yīng)的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;
類庫(kù)service.a.service、service.b.service、service.c.service中都引用
<packagereference include="surging" version="0.5.4" />
如圖:
由于代碼很多地方相識(shí),以下我只說(shuō)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對(duì)應(yīng)的是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”) //自動(dòng)生成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($"服務(wù)端啟動(dòng)成功,{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> /// 配置日志服務(wù) /// </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> /// 配置緩存服務(wù) /// </summary> private void configurecache(iconfigurationbuilder build) { build .addcachefile("cachesettings.json", optional: false); } #endregion } }
service.a.service 類庫(kù)下新增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); } }
其他類庫(kù)和服務(wù)與以上代碼基本無(wú)二,這里不在贅述。不清楚的可以留言
所有代碼都處理好后,在service.a、service.b、service.c項(xiàng)目上右鍵新增docker支持文件,然后會(huì)生成一下文件
修改其中的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運(yùn)行即可
最后訪問(wèn)surging網(wǎng)關(guān),即可看見(jiàn)效果
關(guān)于“.netcore如何使用surging框架發(fā)布到docker中”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“.netcore如何使用surging框架發(fā)布到docker中”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。