LoginSignup
1
1

More than 5 years have passed since last update.

ottoでRedisを使う環境を用意する

Last updated at Posted at 2015-12-14

要するに

ottoはMicroservicesアーキテクチャをサポートしています。
依存するサービスをAppfileに書くと、Vagrant上のDockerで起動します。

MongoDBのサンプルがあります。Redisのサンプルはありません。
真似して書きました。

MongoDBの例

サンプルにMongoDBの例があります。

Appfile
application {
  name = "otto-getting-started"
  type = "ruby"

  dependency {
    source = "github.com/hashicorp/otto/examples/mongodb"
  }
}

github上のMongoDBの設定を参照できます。
中身

Appfile
application {
    name = "mongodb"
    type = "docker-external"
}

customization {
    image = "mongo:3.0"
    run_args = "-p 27017:27017"
}

です。

RedisのAppfile作成

Redisのサンプルがないので、真似して自分で書きます。

Appfile

Appfile
application {
  name = "otto-getting-started"
  type = "node"

  dependency {
    source = "./redis"
  }
}

dependencyにはローカルディレクトリも指定できます

redisディレクトリ

redisディレクトリ内にはAppfile.ottidを用意します。

redis/Appfile
application {
    name = "redis"
    type = "docker-external"
}

customization "docker" {
    image = "redis:3.0"
    run_args = "-p 6379:6379"
}

MongoDBとほとんど一緒です。

  • image名の修正
  • ポートをRedisのデフォルトポートに修正

otto 0.1.2では.ottidは使用していません。
ですが、将来のバージョニング管理のために必須です。
無いとエラーになります。
空ファイルを用意します。

touch .redis/.ottid

動作確認

動作確認します。

Vagrant起動

otto compile
otto dev

接続確認

dependencyに指定したサービスをはname.service.consulというFQDNで参照可能です。
今回の場合はredis.service.consulです。

起動した仮想環境にログインします。

otto dev ssh

npm

redis-cliのインストールは面倒です(後述)。
Node.jsのredisクライアントを使います。

node_redisをインストールして、Node.jsを起動

npm i redis
node

次のJavaScriptを実行します。

const redis = require('redis')
const redisClient = redis.createClient('redis://redis.service.consul')
redisClient.set("string key", "string val", redis.print)

実行結果

vagrant@precise64:/vagrant$ npm i redis
redis@2.4.2 node_modules/redis
├── double-ended-queue@2.1.0-0
└── redis-commands@1.0.1
vagrant@precise64:/vagrant$ node
> var redis = require('redis')
undefined
> var redisClient = redis.createClient('redis://redis.service.consul')
undefined
> redisClient.set("string key", "string val", redis.print);
true
> Reply: OK

rdis-cli

rdis-cliで確認する場合は、Installion: How to install redis-toolsを参考にします。

sources.listにdeb http://us.archive.ubuntu.com/ubuntu trusty main universeを追加します。

sudo vi /etc/apt/sources.list

インストール

sudo apt-get update
sudo apt-get install redis-tools

接続

redis-cli -h redis.service.consul

注意

仮想環境を作りなおした時に、redis.service.consulでRedisに繋げないことがあります。
otto devする前にotto compileをしてください。毎回です。

このコメントを参考にしました。なぜotto compileが必要なのか、わかりません。

ソースコード

githubにあります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1