Docker most complete tutorial - from the theoretical to the practical (XVI)

Foreword

Compared with other languages, Go highly recommended and learning, delicious! why? Mainly it can be compiled directly into machine code (superior performance, volume is very small, up to 10 M, hands-on tutorials, see pictures) and well-designed, low threshold to get started. Data in this chapter focuses on explaining the advantages of the Go language, and provides a push message nails Demo. Finally, since feeling really ambitious, also wrote a .NET Core of Demo, a simple control (see below one).

 

table of Contents

 

  • Go

  • Go advantages

  • Official Mirror

  • Go push messages using nails

  • Learn nails robot

  • Definition Message Type

  • Parameters are taken from the environment variable

  • And sends a request message format set

  • Set Dockerfile

  • Run and set the environment variable push messages

 

Go

Go (also known as Golang) is a static developed by Google strongly typed, compiled, and hair, and having programming language garbage collection function. In 2016, Go was evaluated software company TIOBE selected "TIOBE2016 Best language."

And provisions compared to other languages, there are several different mandatory provisions in Go, when a mismatch compiler will generate an error following provisions:

  • Each line after the end of the program do not need to write a semicolon (;).

  • Braces ({) can not wrap placement.

  • and if the formula for determining cycles need not be covered up parentheses.

     

Go advantages

Compared to other languages, Go language mainly has the following advantages:

  • It can be directly translated into machine code, independent of other libraries;

  • Rich built-in data types (error is the basic data types) ;

  • Language level support concurrent;

  • Good design (though not outstanding, but just right, especially practical) ;

  • Support for garbage collection;

  • Specifications (does not regulate directly compile error, this is too vigorous a) , simple, easy to learn;

  • Rich standard library;

  • Cross-platform compilation;

  • Performance is relatively strong;

  • Easy to deploy;

  • Eco-rich

Go strong performance, but the development of high efficiency off than C / C ++ (many Go language developers are converting over from C / C ++, and get started almost no threshold), lower maintenance costs, and development efficiency not weaker than Python and other dynamic languages, but also supports compile, may reduce the number of low-level errors. In addition, Go there is a great advantage, Go and .NET Core, like, talking a little nice, that is born, pure blood, not to speak of it nicely, are wealthy people, there is a good father.

The protagonist of our tutorial series Docker, based on the Go language is written. Because some of the above characteristics, Go is particularly suitable for cloud computing-related services development (On this point, we can focus on the major cloud vendors of open source projects), server programming, distributed systems, network programming, database memory and so on.

 

 

Official Mirror

Official mirror address:

https://hub.docker.com/_/golang

Because of the good father, so the official document in more detail:

Similarly, we can use docker images golang command to view mirror. It is worth noting that, in general, use a mirror with alpine golang label, because smaller.

 

Go push messages using nails

Next, we use the Go write a simple Demo: namely push message to the nail by nail robot WebHooks.

Currently nail has been widely used in office areas, butt nailed by a robot, we can expect some teams to focus on relevant information pushed to the corresponding nail base.

 

Learn nails robot

Before we begin, we need to have a general understanding nailing robot:

https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq

Here we are using a custom robot. The current custom robot supports text (text), connect (link), markdown (markdown), ActionCard, FeedCard message type, we can choose the right type of message according to their usage scenario, to achieve the best display style. For example more it is that we use the markdown type:

 

Definition Message Type

Let us define the text and markdown type:

 

 

 

Parameters are taken from the environment variable

We can pass parameters through environment variables.

  • Define an environment variable parameters:

 

复制代码
//环境变量

varenvList = []string{

    //钉钉机器人地址

    "WEBHOOK",

    //@的手机号码

    "AT_MOBILES",

    //@所有人

    "IS_AT_ALL",

    //消息内容

    "MESSAGE",

    //消息类型(仅支持文本和markdown)

    "MSG_TYPE",

}
 
复制代码

 

 

  • 从环境变量获取参数并校验

 

    

复制代码
//获取环境变量

    envs:= make(map[string]string)

    for_, envName :=rangeenvList {

        envs[envName]= os.Getenv(envName)

        //参数检查

        ifenvs[envName]==""&&envName !="AT_MOBILES"&&envName !="IS_AT_ALL"{

            fmt.Println("envionmentvariable "+envName+" isrequired")

            os.Exit(1)

        }

    }

 

    ifenvs["AT_MOBILES"] ==""&& envs["IS_AT_ALL"] ==""{

        fmt.Println("必须设置参数AT_MOBILES和IS_AT_ALL两者之一!")

        os.Exit(1)

    }
复制代码

 

设置消息格式并发送请求

相关包引用如下:

i

复制代码
mport(

    "bytes"

    "encoding/json"

    "fmt"

    "io/ioutil"

    "net/http"

    "strings"

)
复制代码

 

关键代码如下所示:

 

 

设置Dockerfile

Dockerfile如下所示:

复制代码
FROM golang:1.10-alpineasbuilder

 

WORKDIR /go/src/component-dingding

 

COPY .//go/src/component-dingding

 

RUN set -ex&& \

go build -v -o/go/bin/component-dingding \

-gcflags'-N -l'\

./*.go

 

FROM alpine

RUN apk update&& apk add ca-certificates

 

COPY --from=builder/go/bin/component-dingding /usr/bin/

CMD ["component-dingding"]

 

#注意不要单独使用MAINTAINER指令,MAINTAINER已被Label标签代替

LABEL MAINTAINER ="[email protected]"

# LABEL指令用于将元数据添加到镜像,支持键值对和JSON,我们可以使用docker inspect命令来查看

LABEL DingtalkComponent='{\

  "description": "使用钉钉发送通知消息.",\

  "input": [\

    {"name": "WEBHOOK","desc": "必填,钉钉机器人Webhook地址"},\

    {"name": "AT_MOBILES","desc": "非必填,被@人的手机号"},\

    {"name": "IS_AT_ALL","desc": "非必填,@所有人时:true,否则为:false"},\

    {"name": "MESSAGE","desc": "必填,自定义发送的消息内容"},\

    {"name": "MSG_TYPE","desc": "必填,自定义发送的消息类型,目前仅支持text和markdown"}\

  ]\

}'
复制代码

 

这里我们使用了标签来说明参数,我们可以使用以下命令来查看标签:

docker inspect go-dingtalk

编译出来的镜像非常小,使用了分阶段构建,因为alpine的镜像非常小,而go可以直接编译成机器代码:

看到这个大小,是不是相对惊诧呢!!其实.NET Core也支持,需要用到CoreRT(.NET Core Runtime,C++的性能,.NET的生产力),不过目前虽然可用,但是还不算完全成熟。有兴趣的可以了解下官方的这个开源库:

https://github.com/dotnet/corert

 

注意:

Alpine Linux 是一个社区开发的面向安全应用的轻量级Linux发行版。从上图我们可以看到,它非常非常小,只有5M,这是其最大的优势。因此,其非常适合用来做Docker镜像、路由器、防火墙、VPNs、VoIP 盒子以及服务器的操作系统。

 

运行并设置环境变量推送消息

运行并设置环境变量推送消息

我们使用PowerShell编写简单脚本如下所示:

复制代码
docker build --rm-f"Dockerfile"-t go-dingtalk:latest .

 

docker run --rm -e"WEBHOOK=https://oapi.dingtalk.com/robot/send?access_token={AccessToken}"`

 -e"MESSAGE=*使用go发送钉钉消息。*"`

 -e"IS_AT_ALL=true"`

 -e"MSG_TYPE=markdown"`

 -d go-dingtalk
复制代码

 

注意:

--rm用于自动清理。也就是用之即来,用完即走。

 

推送成功后,效果图如下所示:

写到这里,笔者有些技痒了!!于是准备试试使用.NET Core也编写类似代码,下节内容将持续更新,敬请关注!

作者:雪雁
出处:http://www.cnblogs.com/codelove/

Guess you like

Origin www.cnblogs.com/zdalongren/p/12213063.html