Docker Compose之Sidecar模式詳解
什么是Docker Compose
在微服務(wù)盛行的今天,我們通常是這么定義Compose的:對容器的統(tǒng)一啟動和關(guān)閉的編排工具。
但是我以前還是有個疑惑,誰會用Compose在一臺服務(wù)器上部署多個服務(wù)呢?干脆直接用單體服務(wù)就行了!直到我遇到了以下的一個需求,讓我明白了在一臺服務(wù)器上不得不用多個服務(wù)的時候,Compose可以通過sidecar的模式,讓服務(wù)很簡單的通過127.0.0.1調(diào)用另一個服務(wù)
需求遇到不合適的語言
一個用golang開發(fā)的某個項目,希望根據(jù)學(xué)生信息打印學(xué)籍,學(xué)籍表其中一部分如下

go中并不是沒有操作word的庫,但是操作這樣一個復(fù)雜的word,并且填好信息還是有很大難度。所以我們想到了一個實現(xiàn)方案。
實現(xiàn)方案
1.通過excel定義一個一樣的模板

2.golang往excel的指定cell里填值,這樣相對往word里填值就簡單很多,其中一部分代碼
xlsx.SetCellValue("Sheet1", "C3", student.Major.Name)
xlsx.SetCellValue("Sheet1", "F3", student.ClassInfo.Name)
xlsx.SetCellValue("Sheet1", "J3", student.SchoolSystem)
xlsx.SetCellValue("Sheet1", "B4", student.Name)
xlsx.SetCellValue("Sheet1", "D4", student.BeforName)
xlsx.SetCellValue("Sheet1", "F4", student.Gender)
xlsx.SetCellValue("Sheet1", "H4", student.Nation)
xlsx.SetCellValue("Sheet1", "B5", student.IdCardNo)
xlsx.SetCellValue("Sheet1", "F5", student.HomePlace)
xlsx.SetCellValue("Sheet1", "B6", student.Birthday.Format("20060102"))
xlsx.SetCellValue("Sheet1", "D6", student.EntranceTime.Format("20060102"))
xlsx.SetCellValue("Sheet1", "F6", student.JoinTeamTime)
xlsx.SetCellValue("Sheet1", "B7", student.FamilyAddress)
xlsx.SetCellValue("Sheet1", "F7", student.HealthStatus)
3.最關(guān)鍵的一步,把excel轉(zhuǎn)成pdf返給前端,供其展示或者打印
我在github了沒找到golang把excel轉(zhuǎn)成pdf的庫(有推薦可以留言),于是想到了.net里的FreeSpire.Xls庫可以很方便實現(xiàn)excel轉(zhuǎn)pdf的功能,所以需要有個.net api把go生產(chǎn)并填好的excel轉(zhuǎn)成pdf,于是我新建了一個.net webapi,項目名定義成pdfprocessor,其中定一個Controller
[Route("[controller]")]
public class PDFController : ControllerBase
{
private readonly ILogger<PDFController> _logger;
public PDFController(ILogger<PDFController> logger)
{
_logger = logger;
}
[HttpPost]
public async Task<IActionResult> HttpPostAsync()
{
try
{
Stream stream = Request.Body;
byte[] buffer = new byte[Request.ContentLength.Value];
stream.Position = 0L;
stream.ReadAsync(buffer, 0, buffer.Length);
Workbook wb = new Workbook();
wb.LoadFromStream(stream);
Worksheet ws = wb.Worksheets[0];
var streamReturn = new MemoryStream();
ws.SaveToPdfStream(streamReturn);
return File(streamReturn, "application/octet-stream");
}
catch (Exception ex)
{
_logger.LogError("", ex);
return BadRequest(ex.Message);
}
}
}
4.部署go項目與.net項目,使go語言調(diào)用.net api實現(xiàn)excel轉(zhuǎn)化pdf
因為這是一個很小的單體項目,那么如何使這個部署與調(diào)用相對簡單就是我需要考慮的問題了,這時候我想到了Docker Compose。
我可以通過docker-compose同時啟動go api和.net api,最重要的還是可以讓go與.net項目使用同一個network的方式,使go api通過127.0.0.1:port來調(diào)用.net api,拓撲如下

5.go api通過127.0.0.1調(diào)用 .net api,這樣.net api就成了go api的一個sidecar,為其服務(wù)
response, err := http.Post("http://127.0.0.1:6081/PDF", "multipart/form-data;boundary="+multipart.NewWriter(bytes.NewBufferString("")).Boundary(), bytes.NewReader(byteA))
if err != nil {
c.Bad(err.Error())
return
}
defer response.Body.Close()
if response.StatusCode != 200 {
data, _ := ioutil.ReadAll(response.Body)
c.Bad(string(data))
return
}
pdfFilePth := fmt.Sprintf("./templates/tmp/%s.pdf", uuid.New())
f, err := os.Create(pdfFilePth)
if err != nil {
c.Bad(err.Error())
return
}
io.Copy(f, response.Body)
c.Ctx.Output.Download(pdfFilePth, "data.xlsx")
6.docker-compose部署
編寫go的dockerfile
FROM library/golang WORKDIR /app RUN go env -w GO111MODULE=on RUN go env -w GOPROXY=https://goproxy.cn,direct ADD api/ /app RUN cd /app RUN go mod tidy RUN go build main.go ENTRYPOINT ["/app/main"] EXPOSE 6080
編寫.net的dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base RUN apt-get update RUN apt-get install -y --no-install-recommends libgdiplus libc6-dev RUN apt-get install -y fontconfig xfonts-utils COPY /pdfprocessor/fonts/ /usr/share/fonts/ RUN mkfontscale RUN mkfontdir RUN fc-cache -fv WORKDIR /app EXPOSE 6081 FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src COPY ["pdfprocessor/pdfprocessor.csproj", "pdfprocessor/"] RUN dotnet restore "pdfprocessor/pdfprocessor.csproj" COPY . . WORKDIR "/src/pdfprocessor" RUN dotnet build "pdfprocessor.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "pdfprocessor.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "pdfprocessor.dll"]
編寫docker-compose.yaml,讓goapi與.net api使用同一個network
version: '3.4'
services:
pdfprocessor:
image: pdfprocessor
build:
context: .
dockerfile: pdfprocessor/Dockerfile
depends_on:
- eduadmin
network_mode: "service:eduadmin"
eduadmin:
image: eduadmin
build:
context: .
dockerfile: api/Dockerfile
ports:
- "6080:6080"
- "6088:6088"
7.通過docker-compose up -d啟動服務(wù),查看pdf展示效果


最后想說docker-compose真香!
到此這篇關(guān)于Docker Compose之Sidecar模式的文章就介紹到這了,更多相關(guān)Docker Compose Sidecar模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Docker基礎(chǔ)教程之Dockerfile語法詳解
這篇文章主要給大家介紹了關(guān)于Docker基礎(chǔ)教程之Dockerfile語法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Docker具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
docker創(chuàng)建mongodb容器存儲數(shù)據(jù)步驟詳解
這篇文章主要為大家介紹了docker創(chuàng)建mongodb容器存儲數(shù)據(jù)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
docker容器環(huán)境安裝及鏡像基礎(chǔ)操作
容器是一種輕量級虛擬化技術(shù),能夠快速構(gòu)建業(yè)務(wù)環(huán)境并便于業(yè)務(wù)遷移,解決兼容性問題,這篇文章主要介紹了docker容器環(huán)境安裝及鏡像基礎(chǔ)操作,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-09-09
Linux如何使用Docker部署DashDot訪問本地服務(wù)器面板
DashDot是一款簡單、實用的開源現(xiàn)代服務(wù)器儀表盤,主要應(yīng)用于小型?VPS?和私人的服務(wù)器(比如說NAS),它是一個界面非常漂亮的監(jiān)控服務(wù)器面板,這篇文章給大家介紹Linux使用Docker部署DashDot訪問本地服務(wù)器面板的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧2024-04-04

