Service 示例

对于第一个 LAIN 应用中提到的 hello-world 来说, 假如想统计访问次数,我们可以把次数信息存在 redis 里。LAIN 提供了 service 机制来支持这种场景。

前置条件

部署 redis-service-sm

LAIN 提供了开箱即用的 redis-service-sm,按照以下步骤即可部署到 LAIN 集群:

git clone https://github.com/laincloud/redis-service-sm.git
cd redis-service-sm/
lain build
lain tag local
lain push local
lain deploy local

修改 lain.yaml

现在我们需要在 lain.yaml 里配置 service:

appname: hello-world

build:
  base: golang:1.8
  script:
    - go get -u github.com/go-redis/redis  # 安装依赖
    - go build -o hello-world

use_services:
  redis-service-sm:  # 提供 service 的应用的 appname
    - redis  # 提供 service 的应用里的 proc 的名字,这个 proc 需要定义相应的 portal

proc.web:
  type: web
  cmd: /lain/app/hello-world
  port: 8080

修改 main.go

package main

import (
    "fmt"
    "net/http"
    "strconv"

    "github.com/go-redis/redis"
)

const visitCountKey = "visitCountKey"

func main() {
    redisClient := redis.NewClient(&redis.Options{
        Addr: "redis:6379",
    })

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if err := redisClient.Incr(visitCountKey).Err(); err != nil {
            fmt.Fprintf(w, "redisClient.Incr() failed, error: %s.", err)
            return
        }

        visitCountStr, err := redisClient.Get(visitCountKey).Result()
        if err != nil {
            fmt.Fprintf(w, "redisClient.Get() failed, error: %s.", err)
            return
        }

        visitCount, err := strconv.Atoi(visitCountStr)
        if err != nil {
            fmt.Fprintf(w, "strconv.Atoi() failed, error: %s.", err)
            return
        }

        fmt.Fprintf(w, "Hello, LAIN. You are the %dth visitor.", visitCount)
    })

    http.ListenAndServe(":8080", nil)
}

hello-world 使用了 redis:6379 访问 redis-service-sm 提供的 redis 服务,其中:

laincloud/[email protected] 的完整代码在这里。

验证

lain build && lain tag local && lain push local && lain deploy local

然后访问 hello-world

[vagrant@lain hello-world]$ curl http://hello-world.lain.local
Hello, LAIN. You are the 11th visitor.

如果看到了 Hello, LAIN. You are the ${n}th visitor.,说明我们的应用已经成功的访问了 redis-service-sm 提供的 redis 服务。

results matching ""

    No results matching ""