Docker 安装
通过 Homebrew 来安装 Dockerbrew install docker
这个安装可能比较慢,可以通过阿里云镜像服务来下载 Docker.dmg
http://mirrors.aliyun.com/doc…
安装 Docker 的图形管理界面 Kitematic
https://github.com/docker/kit…
golang 语言环境配置
brew install go
go 环境变量配置参考:https://www.jianshu.com/p/358…
protobuf 安装
brew install protobuf
如果安装不成功的话,可手动安装
protobuf 下载:https://github.com/protocolbu…
下载完成后解压安装
tar -zxf protobuf-all-3.7.1.tar.gz
cd protobuf-3.7.1
./autogen.sh
./configure
make
make check
sudo make install
如果在执行 ./autogen.sh 出现如下错误:
+ mkdir -p third_party/googletest/m4
+ autoreconf -f -i -Wall,no-obsolete
./autogen.sh: line 37: autoreconf: command not found
解决方法:
brew install autoconf
brew install automake
brew install libtool
然后再执行 ./ autogen.sh 就不报错了
万里长城第一步,终于把 docker 和两个语言环境搭好了,接下来继续。
执行 go get 命令
go get -u -v github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u -v github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u -v github.com/golang/protobuf/protoc-gen-go
如果你在自己的电脑执行上述命令可能会出现问题,go get 被墙,就算有 vpn,网速不好会超时:
此时怎么办呢?先安装 gopm 命令
go get -u -v github.com/gpmgo/gopm
gopm get -u -v github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
gopm get -u -v github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
gopm get -u -v github.com/golang/protobuf/protoc-gen-go
Docker 中安装 Envoy 镜像
docker pull envoyproxy/envoy:latest
1. 在你的机器上创建 docker 数据卷目录,例如,我的目录是:/Users/shifeifei/Software/docker-mount
可将 envoy 的数据卷目录设置成如下形式:/Users/shifeifei/Software/docker-mount/envoyproxy
(1) 创建 envoy.yml 文件存目录, 存放在 config 目录下
(2) 创建 protobuf 代理文件目录,存放在 proto 目录
(3) 创建 docker-compose.yml 文件
目录结构如下:
2. 配置 docker-compose.yml 文件。docker-compose.yml 文件学习
envoy-proxy:
image: envoyproxy/envoy:latest
ports:
- "51051:51051"
volumes:
- /Users/shifeifei/Software/docker-mount/envoyproxy/config:/envoy
- /Users/shifeifei/Software/docker-mount/envoyproxy/proto:/protos
command: sh -c "/usr/local/bin/envoy --config-path /envoy/envoy.yaml"
3. 配置 envoy.yml 文件
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: {address: 0.0.0.0, port_value: 9901}
static_resources:
listeners:
- name: listener1
address:
socket_address: {address: 0.0.0.0, port_value: 51051}
filter_chains:
- filters:
- name: envoy.http_connection_manager
config:
stat_prefix: grpc_json
codec_type: AUTO
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: {prefix: "/"}
route: {cluster: grpc, timeout: { seconds: 60} }
cors:
allow_origin:
- static.stage-5.yinxiang.com
allow_methods: GET, POST, PUT, HEAD, OPTIONS
allow_headers: Authorization,Content-Type
http_filters:
- name: envoy.grpc_json_transcoder
config:
proto_descriptor: "/protos/payment-service.pb"
services: ["payment.PaymentService"]
print_options:
add_whitespace: true
always_print_primitive_fields: true
always_print_enums_as_ints: false
preserve_proto_field_names: false
- name: envoy.router
- name: envoy.cors
clusters:
- name: grpc
type: STRICT_DNS
connect_timeout: 0.25s
lb_policy: LEAST_REQUEST
http2_protocol_options: {}
hosts:
- socket_address:
protocol: TCP
address: host.docker.internal
port_value: 6789
这个文件中需要改动的地方:proto_descriptor、port_value
4. 执行下面的命令时,必须切换到你的项目的根目录;可参考(七、其他备注截图)执行。或者按照下图的路径执行
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--include_imports --include_source_info \
--descriptor_set_out=payment-service.pb \
proto/payment.proto
把生成的 payment-service.pb 文件拷贝到目录:/Users/shifeifei/Software/docker-mount/envoyproxy/proto 中
\ --descriptor_set_out=payment-service.pb \ proto/payment.proto
前面是生成的代理文件名称,后面是你的源文件名称
5. 启动 docker,在 docker-compose.yml 同级目录下执行该命令
docker-compose -f docker-compose.yaml up -d envoy-proxy
6.envoy 系列文章:https://www.lijiaocn.com/%E9%…