mirror of https://github.com/kone-net/go-chat.git
14 changed files with 308 additions and 47 deletions
@ -0,0 +1,6 @@ |
|||||||
|
FROM hub.c.163.com/public/centos:7.0 |
||||||
|
RUN [ "mkdir", "/usr/local/gochat" ] |
||||||
|
WORKDIR /usr/local/gochat |
||||||
|
COPY ../../bin/chat /usr/local/gochat |
||||||
|
COPY ../../config.toml /usr/local/gochat |
||||||
|
CMD [ "/usr/local/gochat/chat" ] |
@ -0,0 +1,56 @@ |
|||||||
|
version: '3' |
||||||
|
|
||||||
|
services: |
||||||
|
mysql8: |
||||||
|
image: mysql:8.0 |
||||||
|
container_name: mysql8 |
||||||
|
restart: always |
||||||
|
environment: |
||||||
|
TZ: Asia/Shanghai |
||||||
|
MYSQL_ROOT_PASSWORD: thepswdforroot |
||||||
|
MYSQL_DATABASE: go-chat-message |
||||||
|
MYSQL_USER: gochat |
||||||
|
MYSQL_PASSWORD: thepswdforgochat |
||||||
|
ports: |
||||||
|
- 3306:3306 |
||||||
|
command: |
||||||
|
# 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配) |
||||||
|
--default-authentication-plugin=mysql_native_password |
||||||
|
--character-set-server=utf8mb4 |
||||||
|
--collation-server=utf8mb4_general_ci |
||||||
|
--explicit_defaults_for_timestamp=true |
||||||
|
--lower_case_table_names=1 |
||||||
|
|
||||||
|
nginx: |
||||||
|
image: nginx:latest |
||||||
|
container_name: nginx |
||||||
|
volumes: |
||||||
|
- ./nginx.conf:/etc/nginx/nginx.conf |
||||||
|
ports: |
||||||
|
- 80:80 |
||||||
|
links: |
||||||
|
- gochat |
||||||
|
- gochat1 |
||||||
|
depends_on: |
||||||
|
- gochat |
||||||
|
- gochat1 |
||||||
|
|
||||||
|
gochat: |
||||||
|
image: konenet/gochat:1.0 |
||||||
|
container_name: gochat |
||||||
|
restart: always |
||||||
|
ports: |
||||||
|
- 8888:8888 |
||||||
|
links: |
||||||
|
- mysql8 |
||||||
|
depends_on: |
||||||
|
- mysql8 |
||||||
|
|
||||||
|
gochat1: |
||||||
|
image: konenet/gochat:1.0 |
||||||
|
restart: always |
||||||
|
container_name: gochat1 |
||||||
|
links: |
||||||
|
- mysql8 |
||||||
|
depends_on: |
||||||
|
- mysql8 |
@ -0,0 +1,48 @@ |
|||||||
|
user nginx; |
||||||
|
worker_processes auto; |
||||||
|
|
||||||
|
error_log /var/log/nginx/error.log notice; |
||||||
|
pid /var/run/nginx.pid; |
||||||
|
|
||||||
|
|
||||||
|
events { |
||||||
|
worker_connections 1024; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
http { |
||||||
|
include /etc/nginx/mime.types; |
||||||
|
default_type application/octet-stream; |
||||||
|
|
||||||
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' |
||||||
|
'$status $body_bytes_sent "$http_referer" ' |
||||||
|
'"$http_user_agent" "$http_x_forwarded_for"'; |
||||||
|
|
||||||
|
access_log /var/log/nginx/access.log main; |
||||||
|
|
||||||
|
sendfile on; |
||||||
|
#tcp_nopush on; |
||||||
|
|
||||||
|
keepalive_timeout 65; |
||||||
|
|
||||||
|
#gzip on; |
||||||
|
|
||||||
|
#include /etc/nginx/conf.d/*.conf; |
||||||
|
|
||||||
|
upstream chat { |
||||||
|
ip_hash; |
||||||
|
server gochat:8888; |
||||||
|
server gochat1:8888; |
||||||
|
} |
||||||
|
|
||||||
|
server { |
||||||
|
listen 80; |
||||||
|
server_name chat; |
||||||
|
location / { |
||||||
|
proxy_pass http://chat; |
||||||
|
proxy_set_header Host $host; |
||||||
|
proxy_set_header X-Real-IP $remote_addr; |
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
package kafka |
||||||
|
|
||||||
|
import ( |
||||||
|
"chat-room/pkg/global/log" |
||||||
|
"github.com/Shopify/sarama" |
||||||
|
"strings" |
||||||
|
) |
||||||
|
|
||||||
|
var consumer sarama.Consumer |
||||||
|
|
||||||
|
type ConsumerCallback func(data []byte) |
||||||
|
|
||||||
|
// 初始化消费者
|
||||||
|
func InitConsumer(hosts string) { |
||||||
|
config := sarama.NewConfig() |
||||||
|
client, err := sarama.NewClient(strings.Split(hosts, ","), config) |
||||||
|
if nil != err { |
||||||
|
log.Error("init kafka consumer client error", log.Any("init kafka consumer client error", err.Error())) |
||||||
|
} |
||||||
|
|
||||||
|
consumer, err = sarama.NewConsumerFromClient(client) |
||||||
|
if nil != err { |
||||||
|
log.Error("init kafka consumer error", log.Any("init kafka consumer error", err.Error())) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// 消费消息,通过回调函数进行
|
||||||
|
func ConsumerMsg(callBack ConsumerCallback) { |
||||||
|
partitionConsumer, err := consumer.ConsumePartition(topic, 0, sarama.OffsetNewest) |
||||||
|
if nil != err { |
||||||
|
log.Error("iConsumePartition error", log.Any("ConsumePartition error", err.Error())) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
defer partitionConsumer.Close() |
||||||
|
for { |
||||||
|
msg := <-partitionConsumer.Messages() |
||||||
|
if nil != callBack { |
||||||
|
callBack(msg.Value) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func CloseConsumer() { |
||||||
|
if nil != consumer { |
||||||
|
consumer.Close() |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,37 @@ |
|||||||
|
package kafka |
||||||
|
|
||||||
|
import ( |
||||||
|
"strings" |
||||||
|
|
||||||
|
"chat-room/pkg/global/log" |
||||||
|
"github.com/Shopify/sarama" |
||||||
|
) |
||||||
|
|
||||||
|
var producer sarama.AsyncProducer |
||||||
|
var topic string = "default_message" |
||||||
|
|
||||||
|
func InitProducer(topicInput, hosts string) { |
||||||
|
topic = topicInput |
||||||
|
config := sarama.NewConfig() |
||||||
|
config.Producer.Compression = sarama.CompressionGZIP |
||||||
|
client, err := sarama.NewClient(strings.Split(hosts, ","), config) |
||||||
|
if nil != err { |
||||||
|
log.Error("init kafka client error", log.Any("init kafka client error", err.Error())) |
||||||
|
} |
||||||
|
|
||||||
|
producer, err = sarama.NewAsyncProducerFromClient(client) |
||||||
|
if nil != err { |
||||||
|
log.Error("init kafka async client error", log.Any("init kafka async client error", err.Error())) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func Send(data []byte) { |
||||||
|
be := sarama.ByteEncoder(data) |
||||||
|
producer.Input() <- &sarama.ProducerMessage{Topic: topic, Key: nil, Value: be} |
||||||
|
} |
||||||
|
|
||||||
|
func Close() { |
||||||
|
if producer != nil { |
||||||
|
producer.Close() |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue