Skip to content

Commit

Permalink
support docker
Browse files Browse the repository at this point in the history
  • Loading branch information
woodylan committed Sep 10, 2020
1 parent 6033efa commit a6cc04d
Show file tree
Hide file tree
Showing 28 changed files with 336 additions and 316 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ before_deploy:
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o go-websocket-linux-amd64
- CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o go-websocket-windows-amd64.exe
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o go-websocket-darwin-amd64
- zip -r go-websocket-linux-amd64.zip go-websocket-linux-amd64 configs/config.ini
- zip -r go-websocket-windows-amd64.zip go-websocket-windows-amd64.exe configs/config.ini
- zip -r go-websocket-darwin-amd64.zip go-websocket-darwin-amd64 configs/config.ini
- zip -r go-websocket-linux-amd64.zip go-websocket-linux-amd64 conf/*
- zip -r go-websocket-windows-amd64.zip go-websocket-windows-amd64.exe conf/*
- zip -r go-websocket-darwin-amd64.zip go-websocket-darwin-amd64 conf/*

deploy:
provider: releases
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.13 AS build-dist
ENV GOPROXY='https://mirrors.aliyun.com/goproxy'
WORKDIR /data/release
COPY . .
RUN go build

FROM centos:latest as prod
WORKDIR /data/go-websocket
COPY --from=build-dist /data/release/go-websocket ./
COPY --from=build-dist /data/release/conf /data/go-websocket/conf

EXPOSE 6000

CMD ["/data/go-websocket/go-websocket","-c","./conf/app.ini"]
42 changes: 28 additions & 14 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

## 简介

本系统基于Golang、Redis、RPC实现分布式WebSocket微服务,也可以单机部署,单机部署不需要Redis、RPC。分布式部署可以支持nginx负责均衡、水平扩容部署,程序之间使用RPC通信。
本系统基于Golang、ETCD、RPC实现分布式WebSocket微服务,也可以单机部署,单机部署不需要ETCD、RPC。分布式部署可以支持nginx负责均衡、水平扩容部署,程序之间使用RPC通信。

基本流程为:用ws协议连接本服务,得到一个clientId,由客户端上报这个clinetId给服务端,服务端拿到这个clientId之后,可以给这个客户端发送信息,绑定这个客户端都分组,给分组发送消息。

Expand Down Expand Up @@ -61,43 +61,56 @@ CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build

**执行:**

编译成功之后会得到一个二进制文件`go-websocket`,执行该二进制文件,文件名后面跟着的是端口号,下面的命令`666`则表示端口号,你可以可以改成其他的
编译成功之后会得到一个二进制文件`go-websocket`,执行该二进制文件。

```shell
./go-websocket 666
./go-websocket -c ./conf/app.ini
```

**连接测试:**

打开支持Websocket的客户端,输入 `ws://127.0.0.1:666/ws` 进行连接,连接成功会返回`clientId`
打开支持Websocket的客户端,输入 `ws://127.0.0.1:6000/ws` 进行连接,连接成功会返回`clientId`



## 单机部署
## docker体验

单机部署很简单,不需要配置Redis,只需要编译然后运行该二进制文件就可以了,步骤如上。
### 体验单机

1. 构建镜像

```shell
docker build -t go-websocket .
```

## 分布式部署
2. 基于镜像运行容器

**安装ETCD:** 参考网上教程
```shell
docker run -tid -p 6000:6000 go-websocket
```

### 体验集群,同时运行ETCD集群
在当前目录下,直接运行` docker-compose up` 即可体验。



## 配置

**配置文件:**

配置文件位于项目根目录的`configs/config.ini``cluster`为true表示分布式部署
配置文件位于项目根目录的`conf/app.ini`

```ini
[common]
# 是否分布式部署
cluster = true
# 对称加密key 16位
crypto_key = xxxxxxxxxxxxxxxx
HttpPort = 6000
RPCPort = 7000
# 是否集群,单机则设为false
Cluster = true
# 对称加密key
CryptoKey = Adba723b7fe06819

[etcd]
host = 127.0.0.1:2379
Endpoints = 127.0.0.1:2379, 127.0.0.2:2379, 127.0.0.3:2379
```

**运行项目:**
Expand Down Expand Up @@ -157,6 +170,7 @@ server {
- [x] 错误日志
- [x] 参数校验
- [x] 关闭某个连接
- [x] 支持docker
- [ ] 查询某个客户端是否在线


Expand Down
7 changes: 2 additions & 5 deletions api/bind2group/bind2group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bind2group
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"go-websocket/configs"
"go-websocket/pkg/setting"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -23,10 +23,7 @@ type retMessage struct {

func newServer(t *testing.T) *testServer {
var s testServer

if err := configs.InitConfig(); err != nil {
panic(err)
}
setting.Default()

controller := &Controller{}
s.Server = httptest.NewServer(http.HandlerFunc(controller.Run))
Expand Down
7 changes: 2 additions & 5 deletions api/closeclient/closeclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package closeclient
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"go-websocket/configs"
"go-websocket/pkg/setting"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -23,10 +23,7 @@ type retMessage struct {

func newServer(t *testing.T) *testServer {
var s testServer

if err := configs.InitConfig(); err != nil {
panic(err)
}
setting.Default()

controller := &Controller{}
s.Server = httptest.NewServer(http.HandlerFunc(controller.Run))
Expand Down
6 changes: 2 additions & 4 deletions api/getonlinelist/getonelinelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package getonlinelist
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"go-websocket/configs"
"go-websocket/pkg/setting"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -23,9 +23,7 @@ type retMessage struct {

func newServer(t *testing.T) *testServer {
var s testServer
if err := configs.InitConfig(); err != nil {
panic(err)
}
setting.Default()

controller := &Controller{}
s.Server = httptest.NewServer(http.HandlerFunc(controller.Run))
Expand Down
7 changes: 2 additions & 5 deletions api/send2client/send2client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package send2client
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"go-websocket/configs"
"go-websocket/pkg/setting"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -23,10 +23,7 @@ type retMessage struct {

func newServer(t *testing.T) *testServer {
var s testServer

if err := configs.InitConfig(); err != nil {
panic(err)
}
setting.Default()

controller := &Controller{}
s.Server = httptest.NewServer(http.HandlerFunc(controller.Run))
Expand Down
7 changes: 2 additions & 5 deletions api/send2clients/send2clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package send2clients
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"go-websocket/configs"
"go-websocket/pkg/setting"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -23,10 +23,7 @@ type retMessage struct {

func newServer(t *testing.T) *testServer {
var s testServer

if err := configs.InitConfig(); err != nil {
panic(err)
}
setting.Default()

controller := &Controller{}
s.Server = httptest.NewServer(http.HandlerFunc(controller.Run))
Expand Down
6 changes: 2 additions & 4 deletions api/send2group/send2group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package send2group
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"go-websocket/configs"
"go-websocket/pkg/setting"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -23,9 +23,7 @@ type retMessage struct {

func newServer(t *testing.T) *testServer {
var s testServer
if err := configs.InitConfig(); err != nil {
panic(err)
}
setting.Default()

controller := &Controller{}
s.Server = httptest.NewServer(http.HandlerFunc(controller.Run))
Expand Down
10 changes: 10 additions & 0 deletions conf/app.cluster.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[common]
HttpPort = 6000
RPCPort = 7000
# 是否集群,单机则设为false
Cluster = true
# 对称加密key
CryptoKey = Adba723b7fe06819

[etcd]
Endpoints = ws_etcd1:2379, ws_etcd2:2379, ws_etcd3:2379
8 changes: 5 additions & 3 deletions configs/config.ini → conf/app.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[common]
HttpPort = 6000
RPCPort = 7000
# 是否集群,单机则设为false
cluster = false
Cluster = false
# 对称加密key
crypto_key =
CryptoKey =

[etcd]
host = 127.0.0.1:2379
Endpoints =
130 changes: 0 additions & 130 deletions configs/configs.go

This file was deleted.

Loading

0 comments on commit a6cc04d

Please sign in to comment.