您好,登錄后才能下訂單哦!
這篇文章主要介紹了.netcore如何使用surging框架發布到docker中的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇.netcore如何使用surging框架發布到docker中文章都會有所收獲,下面我們一起來看看吧。
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網關,即可看見效果
關于“.netcore如何使用surging框架發布到docker中”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“.netcore如何使用surging框架發布到docker中”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。