GIN实践问题记录及问题解决(一)

2,570 阅读1分钟

安装篇

安装方法:

  • 使用govendor安装
    • 安装govendor
    go get -u github.com/kardianos/govendor
    
    • 使用govendor安装gin
      1. 进入$GOPATH目录下的src目录
      2. 创建项目目录。eg:studytest
      3. 进入项目目录。cd studytest
      4. 使用govendor获取gin
      govendor fetch github.com/gin-gonic/gin@v1.6
      
      1. 编写测试入口文件。
      package main
      import "github.com/gin-gonic/gin"
      func main() {
          r := gin.Default()
          r.GET("/ping", func(c *gin.Context) {
              c.JSON(200, gin.H{
      	    "message": "pong",
              })
          })
          r.Run() // listen and serve on 0.0.0.0:8080
      }
      
      1. 测试。go run main.go

安装过程中遇到的问题

  • cannot find package "github.com/go-playground/validator/v10"
    • 解决方法:
    # cd $GOPATH
    # go get "gopkg.in/go-playground/validator.v10"
    # cp -rf $GOPATH/src/gopkg.in/go-playground/validator.v10/* 
    # $GOPATH/src/vendor/github.com/go-playground/validator/v10
    # govendor add +external
    
  • cannot find package "golang.org/x/sys/unix"
    • 解决方法:
    # mkdir -p $GOPATH/src/golang.org/x
    # cd $GOPATH/src/golang.org/x
    # git clone https://github.com/golang/sys.git
    # govendor add +external
    

部署篇

drone+docker+rancher部署:

  • 编写.drone.yml
workspace:
  base: /www/gopath/src
  path: gateway

pipeline:
  build:
    image: golang:alpine3.11
    commands:
      - export GOPATH=/www/gopath
      - set GOARCH=amd64
      - set GOOS=linux
      - go build .
      - ls -l

  publish-test:
    image: alpine:3.11.5
    mirror: https://docker.mirrors.ustc.edu.cn
    registry: registry.cn-hangzhou.aliyuncs.com
    repo: 
      from_secret: docker_repo
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password
    tags:
      - test
    when:
      branch: test

  publish-prod:
    image: alpine:3.11.5
    mirror: https://docker.mirrors.ustc.edu.cn
    registry: registry.cn-hangzhou.aliyuncs.com
    repo: 
      from_secret: docker_repo
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password
    tags: ${DRONE_TAG=latest}
    when:
      event: tag
      
  rancher-test:
    image: peloton/drone-rancher
    url: 
      from_secret: rancher_url
    access_key:
      from_secret: rancher_access_key
    secret_key:
      from_secret: rancher_secret_key
    service: testing/gateway
    docker_image: {docker_image} // 远程docker镜像地址
    start_first: true
    confirm: true
    when:
      branch: test
  • 编写Dockerfile
FROM alpine:3.11.5
ENV WEB_ROOT=/www/wwwroot
# 更新APK源123
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# apk更新
RUN apk update
# 创建根目录
RUN mkdir -p ${WEB_ROOT}
COPY ./gateway ${WEB_ROOT}
WORKDIR ${WEB_ROOT}
RUN chmod +x gateway
CMD ["/www/wwwroot/gateway", "-g", "daemon off;"]
STOPSIGNAL SIGQUIT

部署结果

访问结果

完结撒花