美文网首页
为你的golang程序插上docker_compose的翅膀

为你的golang程序插上docker_compose的翅膀

作者: hwholiday | 来源:发表于2020-02-04 11:18 被阅读0次
    • 构建一个简单的http服务,使用redis的get,set方法
    • 使用docker_compose构建一个agent服务,redis服务并使用networks

    服务端代码

    var rd *redis.Client
    func main() {
        InitRedis()
        http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
            _, _ = fmt.Fprintf(writer, "hello world")
        })
        http.HandleFunc("/set", func(writer http.ResponseWriter, request *http.Request) {
            _ = request.ParseForm()
            key := request.Form.Get("key")
            val := request.Form.Get("val")
            fmt.Println("set >>>>>>  ", "key", key, "val", val)
            if key == "" {
                _, _ = writer.Write([]byte("参数错误"))
                return
            }
            if err := rd.Set(key, val, time.Second*60).Err(); err != nil {
                _, _ = writer.Write([]byte(err.Error()))
                return
            }
            _, _ = writer.Write([]byte("操作成功"))
        })
        http.HandleFunc("/get", func(writer http.ResponseWriter, request *http.Request) {
            _ = request.ParseForm()
            key := request.Form.Get("key")
            fmt.Println("get >>>>>>  ", "key", key)
            if key == "" {
                _, _ = writer.Write([]byte("参数错误"))
                return
            }
            info, err := rd.Get(key).Result()
            if err != nil {
                _, _ = writer.Write([]byte(err.Error()))
                return
            }
            _, _ = writer.Write([]byte(info))
        })
        fmt.Println("服务启动成功 监听端口 9999")
        er := http.ListenAndServe("0.0.0.0:9999", nil)
        if er != nil {
            fmt.Println("ListenAndServe: ", er)
        }
    }
    func InitRedis() {
        rd = redis.NewClient(
            &redis.Options{
                Addr:         "redis:6379",
                DialTimeout:  10 * time.Second,
                ReadTimeout:  30 * time.Second,
                WriteTimeout: 30 * time.Second,
                Password:     "123456",
                PoolSize:     100,
            },
        )
        err := rd.Ping().Err()
        if nil != err {
            fmt.Println(err)
            return
        }
    }
    

    工程目录

    ├── agent
    ├── build.sh
    ├── docker-compose.yml
    ├── Dockerfile
    └── docker.go
    
    

    构建agent服务的Dockerfile

    • 编译agent服务
    CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o agent .
    
    • 编写Dockerfile
    FROM scratch
    MAINTAINER hwholiday
    ADD agent /server/http/
    ENTRYPOINT ["/server/http/agent"]
    

    编写docker-compose.yml

    version: "3"
    services:
      agent:
        hostname: "agent"
        container_name: agent
        restart: always
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - "9999:9999"
        #等redis启动了再启动agent服务
        depends_on:
          - redis
        networks:
          - back
      redis:
        image: redis
        hostname: "redis"
        container_name: redis
        restart: always
        volumes:
          - /home/ghost/redis:/data
        command: redis-server --requirepass 123456
        networks:
          - back
    networks:
      back:
    

    运行

    ## 编译服务
    sudo docker-compose -f docker-compose.yml up
    ## 编译某一个服务
    sudo docker-compose -f docker-compose.yml up agent
    ## 卸载服务
    sudo docker-compose -f docker-compose.yml down
    

    结语

    • docker-compose的用法还有很多,这里只展示简单的使用,更加高级的功能欢迎大家一起讨论
    • 欢迎添加QQ一起讨论

    完整代码地址

    联系 QQ: 3355168235

    相关文章

      网友评论

          本文标题:为你的golang程序插上docker_compose的翅膀

          本文链接:https://www.haomeiwen.com/subject/qkqrxhtx.html