接入服务器和后端业务服务其维持tcp连接,多个前端请求通过接入服务器访问后端业务服务器,接入服务器可以方便增加路由功能,维护多个业务服务器,根据消息ID路由到具体的业务服务器。
项目目录如下
simplelotus
src
lotus
main.go
lotuslib
tcplotus.go
test
tcpclient.go
tcpserver.go
install
install源码如下:
#!/usr/bin/env bash if [ ! -f install ]; then
echo 'install must be run within its container folder' 1>&2
exit 1
fi CURDIR=`pwd`
OLDGOPATH="$GOPATH"
export GOPATH="$CURDIR" gofmt -w src go install lotus export GOPATH="$OLDGOPATH" echo 'finished'
main.go
package main import (
"lotuslib"
) const (
ip = "0.0.0.0"
port = 1987
) func main() {
tcplotus.TcpLotusMain(ip, port)
}
tcplotus.go(和上游维持tcp连接)
package tcplotus import (
"encoding/json"
"log"
"net"
"strconv"
"time"
) const (
proxy_timeout = 5
proxy_server = "127.0.0.1:1988"
msg_length = 1024
) type Request struct {
reqId int
reqContent string
rspChan chan<- string // writeonly chan
} //store request map
var requestMap map[int]*Request type Clienter struct {
client net.Conn
isAlive bool
SendStr chan *Request
RecvStr chan string
} func (c *Clienter) Connect() bool {
if c.isAlive {
return true
} else {
var err error
c.client, err = net.Dial("tcp", proxy_server)
if err != nil {
return false
}
c.isAlive = true
log.Println("connect to " + proxy_server)
}
return true
} //send msg to upstream server
func ProxySendLoop(c *Clienter) { //store reqId and reqContent
senddata := make(map[string]string)
for {
if !c.isAlive {
time.Sleep(1 * time.Second)
c.Connect()
}
if c.isAlive {
req := <-c.SendStr //construct request json string
senddata["reqId"] = strconv.Itoa(req.reqId)
senddata["reqContent"] = req.reqContent
sendjson, err := json.Marshal(senddata)
if err != nil {
continue
} _, err = c.client.Write([]byte(sendjson))
if err != nil {
c.RecvStr <- string("proxy server close...")
c.client.Close()
c.isAlive = false
log.Println("disconnect from " + proxy_server)
continue
}
//log.Println("Write to proxy server: " + string(sendjson))
}
}
} //recv msg from upstream server
func ProxyRecvLoop(c *Clienter) {
buf := make([]byte, msg_length)
recvdata := make(map[string]string, 2)
for {
if !c.isAlive {
time.Sleep(1 * time.Second)
c.Connect()
}
if c.isAlive {
n, err := c.client.Read(buf)
if err != nil {
c.client.Close()
c.isAlive = false
log.Println("disconnect from " + proxy_server)
continue
}
//log.Println("Read from proxy server: " + string(buf[0:n])) if err := json.Unmarshal(buf[0:n], &recvdata); err == nil {
reqidstr := recvdata["reqId"]
if reqid, err := strconv.Atoi(reqidstr); err == nil {
req, ok := requestMap[reqid]
if !ok {
continue
}
req.rspChan <- recvdata["resContent"]
}
continue
}
}
}
} //one handle per request
func handle(conn *net.TCPConn, id int, tc *Clienter) { data := make([]byte, msg_length)
handleProxy := make(chan string)
request := &Request{reqId: id, rspChan: handleProxy} requestMap[id] = request
for {
n, err := conn.Read(data)
if err != nil {
log.Println("disconnect from " + conn.RemoteAddr().String())
conn.Close()
delete(requestMap, id)
return
}
request.reqContent = string(data[0:n])
//send to proxy
select { case tc.SendStr <- request:
case <-time.After(proxy_timeout * time.Second):
//proxyChan <- &Request{cancel: true, reqId: id}
_, err = conn.Write([]byte("proxy server send timeout."))
if err != nil {
conn.Close()
delete(requestMap, id)
return
}
continue
} //read from proxy
select {
case rspContent := <-handleProxy:
_, err := conn.Write([]byte(rspContent))
if err != nil {
conn.Close()
delete(requestMap, id)
return
}
case <-time.After(proxy_timeout * time.Second):
_, err = conn.Write([]byte("proxy server recv timeout."))
if err != nil {
conn.Close()
delete(requestMap, id)
return
}
continue
}
}
} func TcpLotusMain(ip string, port int) {
//start tcp server
listen, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP(ip), port, ""})
if err != nil {
log.Fatalln("listen port error")
return
}
log.Println("start tcp server " + ip + " " + strconv.Itoa(port))
defer listen.Close() //start proxy connect and loop
var tc Clienter
tc.SendStr = make(chan *Request, 1000)
tc.RecvStr = make(chan string)
tc.Connect()
go ProxySendLoop(&tc)
go ProxyRecvLoop(&tc) //listen new request
requestMap = make(map[int]*Request)
var id int = 0
for { conn, err := listen.AcceptTCP()
if err != nil {
log.Println("receive connection failed")
continue
}
id++
log.Println("connected from " + conn.RemoteAddr().String())
go handle(conn, id, &tc) }
}
测试代码如下:
tcpserver.go
package main import (
"encoding/json"
"fmt"
"net"
) const (
msg_length = 1024
) func Echo(c net.Conn) {
data := make([]byte, msg_length)
defer c.Close() var recvdata map[string]string
recvdata = make(map[string]string, 2)
var senddata map[string]string
senddata = make(map[string]string, 2) for {
n, err := c.Read(data)
if err != nil {
fmt.Printf("read message from lotus failed")
return
} if err := json.Unmarshal(data[0:n], &recvdata); err == nil {
senddata["reqId"] = recvdata["reqId"]
senddata["resContent"] = "Hello " + recvdata["reqContent"] sendjson, err := json.Marshal(senddata)
_, err = c.Write([]byte(sendjson))
if err != nil {
fmt.Printf("disconnect from lotus server")
return
}
}
}
} func main() {
fmt.Printf("Server is ready...\n")
l, err := net.Listen("tcp", ":1988")
if err != nil {
fmt.Printf("Failure to listen: %s\n", err.Error())
} for {
if c, err := l.Accept(); err == nil {
go Echo(c) //new thread
}
}
}
tcpclient.go
package main import (
"bufio"
"fmt"
"net"
"os"
"time"
) type Clienter struct {
client net.Conn
isAlive bool
SendStr chan string
RecvStr chan string
} func (c *Clienter) Connect() bool {
if c.isAlive {
return true
} else {
var err error
c.client, err = net.Dial("tcp", "127.0.0.1:1987")
if err != nil {
fmt.Printf("Failure to connet:%s\n", err.Error())
return false
}
c.isAlive = true
}
return true
} func (c *Clienter) Echo() {
line := <-c.SendStr
c.client.Write([]byte(line))
buf := make([]byte, 1024)
n, err := c.client.Read(buf)
if err != nil {
c.RecvStr <- string("Server close...")
c.client.Close()
c.isAlive = false
return
}
time.Sleep(1 * time.Second)
c.RecvStr <- string(buf[0:n])
} func Work(tc *Clienter) {
if !tc.isAlive {
if tc.Connect() {
tc.Echo()
} else {
<-tc.SendStr
tc.RecvStr <- string("Server close...")
}
} else {
tc.Echo()
}
}
func main() {
var tc Clienter
tc.SendStr = make(chan string)
tc.RecvStr = make(chan string)
if !tc.Connect() {
return
}
r := bufio.NewReader(os.Stdin)
for {
switch line, ok := r.ReadString('\n'); true {
case ok != nil:
fmt.Printf("bye bye!\n")
return
default:
go Work(&tc)
tc.SendStr <- line
s := <-tc.RecvStr
fmt.Printf("back:%s\n", s)
}
}
}
golang实现tcp接入服务器的更多相关文章
-
TODO:Golang语言TCP/UDP协议重用地址端口
TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...
-
golang实现udp接入服务器
前端通过udp与接入服务器连接,接入服务器与后端tcp服务器维持tcp连接.目录结构及后端tcp服务器代码同上一篇博客. main.go package main import ( "lot ...
-
golang中tcp socket粘包问题和处理
转自:http://www.01happy.com/golang-tcp-socket-adhere/ 在用golang开发人工客服系统的时候碰到了粘包问题,那么什么是粘包呢?例如我们和客户端约定数据 ...
-
golang 解决 TCP 粘包问题
什么是 TCP 粘包问题以及为什么会产生 TCP 粘包,本文不加讨论.本文使用 golang 的 bufio.Scanner 来实现自定义协议解包. 协议数据包定义 本文模拟一个日志服务器,该服务器接 ...
-
Golang 编写 Tcp 服务器
Golang 作为广泛用于服务端和云计算领域的编程语言,tcp socket 是其中至关重要的功能.无论是 WEB 服务器还是各类中间件都离不开 tcp socket 的支持. Echo 服务器 拆包 ...
-
golang实现tcp编程
实现简单的tcp服务 package main import ( "fmt" "net" ) func main() { fmt.Println("服 ...
-
golang之tcp自动重连
操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 问题描述 现有一个tcp客户端程序,需定期从服务器取数据,但由于种种原因(网络不稳定等)需要自动重连. 测试服务器示例代码: /* ...
-
golang:TCP总结
在TCP/IP协议中,"IP地址+TCP或UDP端口号"唯一标识网络通讯中的一个进程."IP地址+端口号"就对应一个socket.欲建立连接的两个进程各自有一个 ...
-
6行代码解决golang TCP粘包
转自:https://studygolang.com/articles/12483 什么是TCP粘包问题以及为什么会产生TCP粘包,本文不加讨论.本文使用golang的bufio.Scanner来实现 ...
随机推荐
-
转:PCL+VS2010环境配置
1.下载 http://www.pointclouds.org/downloads/windows.html出下载PCL完全安装包1.6.0 all-in-one-installer,我的电脑是32位 ...
-
Linux中启动和停止jar包的运行
脚本一: startTest.sh内容如下: #!/bin/sh java -jar Test.jar & #注意:必须有&让其后台执行,否则没有pid生成 echo $! ...
-
EditPlus保存文件时不生成其备份文件的方法
3.将“保存时去掉备份文件”复选框去掉,点击 应用->确定,即可 .
-
python笔记十四(高阶函数——map/reduce、filter、sorted)
一.map/reduce 1.map() map(f,iterable),将一个iterable对象一次作用于函数f,并返回一个迭代器. >>> def f(x): #定义一个函数 ...
-
sklearn:最近邻搜索sklearn.neighbors
http://blog.csdn.net/pipisorry/article/details/53156836 ball tree k-d tree也有问题[最近邻查找算法kd-tree].矩形并不是 ...
-
edge
https://www.cnblogs.com/st-leslie/p/6784990.html
-
VM workstation 与 VM vSphere的区别 [转载]
在学完vSphere后,想起了VMware Workstation.这两个都是虚拟化的东西,这两者到底有什么本质的不同呢?顺着我的思路我开始将所学过的进行检索期望从中寻到一丝半点的线索.很快大脑中建立 ...
-
SQL Server配置数据库邮件
需求描述 在生产环境中,大部分情况下需要有自己的运维*,包括自己健康状态的检测等.如果发生异常,需要提前预警的,可以以发邮件告知,邮件作为一种非常便利的预警实现方式,在及时性和易用性方面也有着不可替 ...
-
C/C++缓冲区刷新问题
参考链接 参考链接2 Buffers are normally maintained by the operating system, which determines the optimal tim ...
-
php代码编译的实现
1.php是解析型的高级语言,zend内核使用c语言实现,有main函数,php脚本就是输入,内核处理后输出结果,内核将php脚本翻译成c程序可识别的opcode就是php的编译. c语言的编译将c代 ...