Commit a34641aa authored by AlexStocks's avatar AlexStocks

Update: use yaml config

parent 02003dc0
...@@ -46,7 +46,7 @@ func initConf() { ...@@ -46,7 +46,7 @@ func initConf() {
return // I know it is of no usage. Just Err Protection. return // I know it is of no usage. Just Err Protection.
} }
if path.Ext(confFile) != ".yml" { if path.Ext(confFile) != ".yml" {
panic(fmt.Sprintf("application configure file name{%v} suffix must be .toml", confFile)) panic(fmt.Sprintf("application configure file name{%v} suffix must be .yml", confFile))
return return
} }
......
...@@ -33,7 +33,7 @@ core: ...@@ -33,7 +33,7 @@ core:
tcp_write_timeout : "5s" tcp_write_timeout : "5s"
wait_timeout : "1s" wait_timeout : "1s"
max_msg_len : 1024 max_msg_len : 1024
session_name : "getty-micro-server" session_name : "getty-micro-client"
registry: registry:
basic: basic:
......
...@@ -33,7 +33,7 @@ core: ...@@ -33,7 +33,7 @@ core:
tcp_write_timeout : "5s" tcp_write_timeout : "5s"
wait_timeout : "1s" wait_timeout : "1s"
max_msg_len : 1024 max_msg_len : 1024
session_name : "getty-micro-server" session_name : "getty-micro-client"
registry: registry:
basic: basic:
......
...@@ -33,7 +33,7 @@ core: ...@@ -33,7 +33,7 @@ core:
tcp_write_timeout : "5s" tcp_write_timeout : "5s"
wait_timeout : "1s" wait_timeout : "1s"
max_msg_len : 1024 max_msg_len : 1024
session_name : "getty-micro-server" session_name : "getty-micro-client"
registry: registry:
basic: basic:
......
...@@ -13,13 +13,14 @@ import ( ...@@ -13,13 +13,14 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"io/ioutil"
) )
import ( import (
"github.com/AlexStocks/getty/rpc" "github.com/AlexStocks/getty/rpc"
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
jerrors "github.com/juju/errors" jerrors "github.com/juju/errors"
config "github.com/koding/multiconfig" "gopkg.in/yaml.v2"
) )
const ( const (
...@@ -27,8 +28,14 @@ const ( ...@@ -27,8 +28,14 @@ const (
APP_LOG_CONF_FILE = "APP_LOG_CONF_FILE" APP_LOG_CONF_FILE = "APP_LOG_CONF_FILE"
) )
type Config struct {
ServerHost string `default:"127.0.0.1" yaml:"server_host" json:"server_host,omitempty"`
ServerPort int `default:"1234" yaml:"server_port" json:"server_port,omitempty"`
rpc.ClientConfig `yaml:"core" json:"core, omitempty"`
}
var ( var (
conf *rpc.ClientConfig conf *Config
) )
func initConf() { func initConf() {
...@@ -38,17 +45,32 @@ func initConf() { ...@@ -38,17 +45,32 @@ func initConf() {
panic(fmt.Sprintf("application configure file name is nil")) panic(fmt.Sprintf("application configure file name is nil"))
return // I know it is of no usage. Just Err Protection. return // I know it is of no usage. Just Err Protection.
} }
if path.Ext(confFile) != ".toml" { if path.Ext(confFile) != ".yml" {
panic(fmt.Sprintf("application configure file name{%v} suffix must be .toml", confFile)) panic(fmt.Sprintf("application configure file name{%v} suffix must be .yml", confFile))
return return
} }
conf = new(rpc.ClientConfig) conf = &Config{}
config.MustLoadWithPath(confFile, conf)
if err := conf.CheckValidity(); err != nil { confFileStream, err := ioutil.ReadFile(confFile)
if err != nil {
panic(fmt.Sprintf("ioutil.ReadFile(file:%s) = error:%s", confFile, jerrors.ErrorStack(err)))
return
}
err = yaml.Unmarshal(confFileStream, conf)
if err != nil {
panic(fmt.Sprintf("yaml.Unmarshal() = error:%s", jerrors.ErrorStack(err)))
return
}
fmt.Printf("conf:%#v\n", conf)
if err = conf.ClientConfig.CheckValidity(); err != nil {
panic(jerrors.ErrorStack(err)) panic(jerrors.ErrorStack(err))
return return
} }
if conf.ServerPort < 0 {
panic("Config.ServerPort < 0")
return
}
// log // log
confFile = os.Getenv(APP_LOG_CONF_FILE) confFile = os.Getenv(APP_LOG_CONF_FILE)
......
...@@ -10,10 +10,12 @@ ...@@ -10,10 +10,12 @@
package main package main
import ( import (
"net"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal" "os/signal"
"strconv"
"syscall" "syscall"
"time" "time"
) )
...@@ -66,7 +68,7 @@ func initProfiling() { ...@@ -66,7 +68,7 @@ func initProfiling() {
func initClient() { func initClient() {
var err error var err error
client, err = rpc.NewClient(conf) client, err = rpc.NewClient(&conf.ClientConfig)
if err != nil { if err != nil {
panic(jerrors.ErrorStack(err)) panic(jerrors.ErrorStack(err))
} }
...@@ -111,7 +113,10 @@ func testJSON() { ...@@ -111,7 +113,10 @@ func testJSON() {
ts := rpc_examples.TestService{} ts := rpc_examples.TestService{}
testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"} testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"}
testRsp := rpc_examples.TestRsp{} testRsp := rpc_examples.TestRsp{}
err := client.Call(rpc.CodecJson, "127.0.0.1:20000", ts.Service(), "Test", &testReq, &testRsp) addr := net.JoinHostPort(conf.ServerHost, strconv.Itoa(conf.ServerPort))
err := client.Call(rpc.CodecJson, addr, ts.Service(), "Test", &testReq,
&testRsp)
if err != nil { if err != nil {
log.Error("client.Call(Json, TestService::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(Json, TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
...@@ -120,7 +125,7 @@ func testJSON() { ...@@ -120,7 +125,7 @@ func testJSON() {
addReq := rpc_examples.AddReq{1, 10} addReq := rpc_examples.AddReq{1, 10}
addRsp := rpc_examples.AddRsp{} addRsp := rpc_examples.AddRsp{}
err = client.Call(rpc.CodecJson, "127.0.0.1:10000", ts.Service(), "Add", &addReq, &addRsp) err = client.Call(rpc.CodecJson, addr, ts.Service(), "Add", &addReq, &addRsp)
if err != nil { if err != nil {
log.Error("client.Call(Json, TestService::Add) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(Json, TestService::Add) = error:%s", jerrors.ErrorStack(err))
return return
...@@ -129,7 +134,7 @@ func testJSON() { ...@@ -129,7 +134,7 @@ func testJSON() {
errReq := rpc_examples.ErrReq{1} errReq := rpc_examples.ErrReq{1}
errRsp := rpc_examples.ErrRsp{} errRsp := rpc_examples.ErrRsp{}
err = client.Call(rpc.CodecJson, "127.0.0.1:20000", ts.Service(), "Err", &errReq, &errRsp) err = client.Call(rpc.CodecJson, addr, ts.Service(), "Err", &errReq, &errRsp)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(Json, TestService::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(Json, TestService::Err) = error:%s", jerrors.ErrorStack(err))
...@@ -142,7 +147,10 @@ func testProtobuf() { ...@@ -142,7 +147,10 @@ func testProtobuf() {
ts := rpc_examples.TestService{} ts := rpc_examples.TestService{}
testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"} testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"}
testRsp := rpc_examples.TestRsp{} testRsp := rpc_examples.TestRsp{}
err := client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", ts.Service(), "Test", &testReq, &testRsp) addr := net.JoinHostPort(conf.ServerHost, strconv.Itoa(conf.ServerPort))
err := client.Call(rpc.CodecProtobuf, addr, ts.Service(), "Test", &testReq,
&testRsp)
if err != nil { if err != nil {
log.Error("client.Call(protobuf, TestService::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(protobuf, TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
...@@ -151,7 +159,8 @@ func testProtobuf() { ...@@ -151,7 +159,8 @@ func testProtobuf() {
addReq := rpc_examples.AddReq{1, 10} addReq := rpc_examples.AddReq{1, 10}
addRsp := rpc_examples.AddRsp{} addRsp := rpc_examples.AddRsp{}
err = client.Call(rpc.CodecProtobuf, "127.0.0.1:10000", ts.Service(), "Add", &addReq, &addRsp) err = client.Call(rpc.CodecProtobuf, addr, ts.Service(), "Add", &addReq,
&addRsp)
if err != nil { if err != nil {
log.Error("client.Call(protobuf, TestService::Add) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(protobuf, TestService::Add) = error:%s", jerrors.ErrorStack(err))
return return
...@@ -160,7 +169,8 @@ func testProtobuf() { ...@@ -160,7 +169,8 @@ func testProtobuf() {
errReq := rpc_examples.ErrReq{1} errReq := rpc_examples.ErrReq{1}
errRsp := rpc_examples.ErrRsp{} errRsp := rpc_examples.ErrRsp{}
err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", ts.Service(), "Err", &errReq, &errRsp) err = client.Call(rpc.CodecProtobuf, addr, ts.Service(), "Err", &errReq,
&errRsp)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(protobuf, TestService::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(protobuf, TestService::Err) = error:%s", jerrors.ErrorStack(err))
...@@ -170,7 +180,7 @@ func testProtobuf() { ...@@ -170,7 +180,7 @@ func testProtobuf() {
} }
func test() { func test() {
for i := 0; i < 5; i++ { for i := 0; i < 1; i++ {
testJSON() testJSON()
testProtobuf() testProtobuf()
} }
......
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
export TARGET_EXEC_NAME="rpc_client" export TARGET_EXEC_NAME="rpc_client"
export BUILD_PACKAGE="app" export BUILD_PACKAGE="app"
export TARGET_CONF_FILE="conf/config.toml" export TARGET_CONF_FILE="conf/config.yml"
export TARGET_LOG_CONF_FILE="conf/log.xml" export TARGET_LOG_CONF_FILE="conf/log.xml"
# toml configure file
# toml中key的首字母可以小写,但是对应的golang中的struct成员首字母必须大写
AppName = "RPC-CLIENT"
# host
LocalHost = "127.0.0.1"
# server
ServerHost = "127.0.0.1"
ServerPort = 10000
ProfilePort = 10080
# connection pool
# 连接池连接数目
ConnectionNum = 2
# session
# client与server之间连接的心跳周期
HeartbeatPeriod = "10s"
# client与server之间连接的超时时间
SessionTimeout = "20s"
# app fail fast
FailFastTimeout = "3s"
# tcp
[GettySessionParam]
CompressEncoding = true
TcpNoDelay = true
TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144
TcpWBufSize = 65536
PkgRQSize = 512
PkgWQSize = 256
TcpReadTimeout = "1s"
TcpWriteTimeout = "5s"
WaitTimeout = "1s"
MaxMsgLen = 1024
SessionName = "getty-rpc-client"
# server
server_host : "127.0.0.1"
server_port : 10000
core:
app_name : "RPC-CLIENT"
host : "127.0.0.1"
profile_port : 20086
# client 与 server单个peer与peer之间连接数目 
connection_number : 2
# session
# client与server之间连接的心跳周期
heartbeat_period : "10s"
# client与server之间连接的超时时间
session_timeout : "20s"
# app fail fast
fail_fast_timeout : "3s"
# connection pool
pool_size : 64
pool_ttl : 600
# tcp
getty_session_param:
compress_encoding : true
tcp_no_delay : true
tcp_keep_alive : true
keep_alive_period : "120s"
tcp_r_buf_size : 262144
tcp_w_buf_size : 65536
pkg_rq_size : 1024
pkg_wq_size : 512
tcp_read_timeout : "1s"
tcp_write_timeout : "5s"
wait_timeout : "1s"
max_msg_len : 1024
session_name : "getty-micro-server"
# toml configure file
# toml中key的首字母可以小写,但是对应的golang中的struct成员首字母必须大写
AppName = "RPC-CLIENT"
# host
LocalHost = "127.0.0.1"
# server
ServerHost = "127.0.0.1"
ServerPort = 10000
ProfilePort = 10080
# connection pool
# 连接池连接数目
ConnectionNum = 2
# session
# client与server之间连接的心跳周期
HeartbeatPeriod = "10s"
# client与server之间连接的超时时间
SessionTimeout = "20s"
# app fail fast
FailFastTimeout = "3s"
# tcp
[GettySessionParam]
CompressEncoding = true
TcpNoDelay = true
TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144
TcpWBufSize = 65536
PkgRQSize = 512
PkgWQSize = 256
TcpReadTimeout = "1s"
TcpWriteTimeout = "5s"
WaitTimeout = "1s"
MaxMsgLen = 1024
SessionName = "getty-rpc-client"
# server
server_host : "127.0.0.1"
server_port : 10000
core:
app_name : "RPC-CLIENT"
host : "127.0.0.1"
profile_port : 20086
# client 与 server单个peer与peer之间连接数目 
connection_number : 2
# session
# client与server之间连接的心跳周期
heartbeat_period : "10s"
# client与server之间连接的超时时间
session_timeout : "20s"
# app fail fast
fail_fast_timeout : "3s"
# connection pool
pool_size : 64
pool_ttl : 600
# tcp
getty_session_param:
compress_encoding : true
tcp_no_delay : true
tcp_keep_alive : true
keep_alive_period : "120s"
tcp_r_buf_size : 262144
tcp_w_buf_size : 65536
pkg_rq_size : 1024
pkg_wq_size : 512
tcp_read_timeout : "1s"
tcp_write_timeout : "5s"
wait_timeout : "1s"
max_msg_len : 1024
session_name : "getty-micro-server"
# toml configure file
# toml中key的首字母可以小写,但是对应的golang中的struct成员首字母必须大写
AppName = "RPC-CLIENT"
# host
LocalHost = "127.0.0.1"
# server
ServerHost = "127.0.0.1"
ServerPort = 10000
ProfilePort = 10080
# connection pool
# 连接池连接数目
ConnectionNum = 2
# session
# client与server之间连接的心跳周期
HeartbeatPeriod = "10s"
# client与server之间连接的超时时间
SessionTimeout = "20s"
# app fail fast
FailFastTimeout = "3s"
# tcp
[GettySessionParam]
CompressEncoding = true
TcpNoDelay = true
TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144
TcpWBufSize = 65536
PkgRQSize = 512
PkgWQSize = 256
TcpReadTimeout = "1s"
TcpWriteTimeout = "5s"
WaitTimeout = "1s"
MaxMsgLen = 1024
SessionName = "getty-rpc-client"
# server
server_host : "127.0.0.1"
server_port : 10000
core:
app_name : "RPC-CLIENT"
host : "127.0.0.1"
profile_port : 20086
# client 与 server单个peer与peer之间连接数目 
connection_number : 2
# session
# client与server之间连接的心跳周期
heartbeat_period : "10s"
# client与server之间连接的超时时间
session_timeout : "20s"
# app fail fast
fail_fast_timeout : "3s"
# connection pool
pool_size : 64
pool_ttl : 600
# tcp
getty_session_param:
compress_encoding : true
tcp_no_delay : true
tcp_keep_alive : true
keep_alive_period : "120s"
tcp_r_buf_size : 262144
tcp_w_buf_size : 65536
pkg_rq_size : 1024
pkg_wq_size : 512
tcp_read_timeout : "1s"
tcp_write_timeout : "5s"
wait_timeout : "1s"
max_msg_len : 1024
session_name : "getty-micro-server"
...@@ -11,6 +11,7 @@ package main ...@@ -11,6 +11,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
) )
...@@ -19,7 +20,7 @@ import ( ...@@ -19,7 +20,7 @@ import (
"github.com/AlexStocks/getty/rpc" "github.com/AlexStocks/getty/rpc"
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
jerrors "github.com/juju/errors" jerrors "github.com/juju/errors"
config "github.com/koding/multiconfig" "gopkg.in/yaml.v2"
) )
const ( const (
...@@ -38,13 +39,23 @@ func initConf() { ...@@ -38,13 +39,23 @@ func initConf() {
panic(fmt.Sprintf("application configure file name is nil")) panic(fmt.Sprintf("application configure file name is nil"))
return // I know it is of no usage. Just Err Protection. return // I know it is of no usage. Just Err Protection.
} }
if path.Ext(confFile) != ".toml" { if path.Ext(confFile) != ".yml" {
panic(fmt.Sprintf("application configure file name{%v} suffix must be .toml", confFile)) panic(fmt.Sprintf("application configure file name{%v} suffix must be .yml", confFile))
return return
} }
conf = &rpc.ServerConfig{} conf = &rpc.ServerConfig{}
config.MustLoadWithPath(confFile, conf) confFileStream, err := ioutil.ReadFile(confFile)
if err := conf.CheckValidity(); err != nil { if err != nil {
panic(fmt.Sprintf("ioutil.ReadFile(file:%s) = error:%s", confFile, jerrors.ErrorStack(err)))
return
}
err = yaml.Unmarshal(confFileStream, conf)
if err != nil {
panic(fmt.Sprintf("yaml.Unmarshal() = error:%s", jerrors.ErrorStack(err)))
return
}
if err = conf.CheckValidity(); err != nil {
panic(jerrors.ErrorStack(err)) panic(jerrors.ErrorStack(err))
return return
} }
......
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
export TARGET_EXEC_NAME="rpc_server" export TARGET_EXEC_NAME="rpc_server"
export BUILD_PACKAGE="app" export BUILD_PACKAGE="app"
export TARGET_CONF_FILE="conf/config.toml" export TARGET_CONF_FILE="conf/config.yml"
export TARGET_LOG_CONF_FILE="conf/log.xml" export TARGET_LOG_CONF_FILE="conf/log.xml"
# toml configure file
# toml中key的首字母可以小写,但是对应的golang中的struct成员首字母必须大写
AppName = "RPC-SERVER"
Host = "127.0.0.1"
Ports = ["10000", "20000"]
ProfilePort = 10086
# client与server之间连接的超时时间
SessionTimeout = "20s"
SessionNumber = 700
# app
FailFastTimeout = "3s"
# tcp
[GettySessionParam]
CompressEncoding = true
TcpNoDelay = true
TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144
TcpWBufSize = 524288
PkgRQSize = 1024
PkgWQSize = 512
TcpReadTimeout = "1s"
TcpWriteTimeout = "5s"
WaitTimeout = "1s"
MaxMsgLen = 1024
SessionName = "getty-rpc-server"
app_name : "RPC-SERVER"
host : "127.0.0.1"
ports : ["10000", "20000"]
profile_port : 10086
# client与server之间连接的超时时间
session_timeout : "20s"
session_number : 700
fail_fast_timeout : "3s"
# tcp
getty_session_param:
compress_encoding : true
tcp_no_delay : true
tcp_keep_alive : true
keep_alive_period : "120s"
tcp_r_buf_size : 262144
tcp_w_buf_size : 524288
pkg_rq_size : 1024
pkg_wq_size : 512
tcp_read_timeout : "1s"
tcp_write_timeout : "5s"
wait_timeout : "1s"
max_msg_len : 1024
session_name : "getty-rpc-server"
# toml configure file
# toml中key的首字母可以小写,但是对应的golang中的struct成员首字母必须大写
AppName = "RPC-SERVER"
Host = "127.0.0.1"
Ports = ["10000", "20000"]
ProfilePort = 10086
# client与server之间连接的超时时间
SessionTimeout = "20s"
SessionNumber = 700
# app
FailFastTimeout = "3s"
# tcp
[GettySessionParam]
CompressEncoding = true
TcpNoDelay = true
TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144
TcpWBufSize = 524288
PkgRQSize = 1024
PkgWQSize = 512
TcpReadTimeout = "1s"
TcpWriteTimeout = "5s"
WaitTimeout = "1s"
MaxMsgLen = 1024
SessionName = "getty-rpc-server"
app_name : "RPC-SERVER"
host : "127.0.0.1"
ports : ["10000", "20000"]
profile_port : 10086
# client与server之间连接的超时时间
session_timeout : "20s"
session_number : 700
fail_fast_timeout : "3s"
# tcp
getty_session_param:
compress_encoding : true
tcp_no_delay : true
tcp_keep_alive : true
keep_alive_period : "120s"
tcp_r_buf_size : 262144
tcp_w_buf_size : 524288
pkg_rq_size : 1024
pkg_wq_size : 512
tcp_read_timeout : "1s"
tcp_write_timeout : "5s"
wait_timeout : "1s"
max_msg_len : 1024
session_name : "getty-rpc-server"
# toml configure file
# toml中key的首字母可以小写,但是对应的golang中的struct成员首字母必须大写
AppName = "RPC-SERVER"
Host = "127.0.0.1"
Ports = ["10000", "20000"]
ProfilePort = 10086
# client与server之间连接的超时时间
SessionTimeout = "20s"
SessionNumber = 700
# app
FailFastTimeout = "3s"
# tcp
[GettySessionParam]
CompressEncoding = true
TcpNoDelay = true
TcpKeepAlive = true
KeepAlivePeriod = "120s"
TcpRBufSize = 262144
TcpWBufSize = 524288
PkgRQSize = 1024
PkgWQSize = 512
TcpReadTimeout = "1s"
TcpWriteTimeout = "5s"
WaitTimeout = "1s"
MaxMsgLen = 1024
SessionName = "getty-rpc-server"
app_name : "RPC-SERVER"
host : "127.0.0.1"
ports : ["10000", "20000"]
profile_port : 10086
# client与server之间连接的超时时间
session_timeout : "20s"
session_number : 700
fail_fast_timeout : "3s"
# tcp
getty_session_param:
compress_encoding : true
tcp_no_delay : true
tcp_keep_alive : true
keep_alive_period : "120s"
tcp_r_buf_size : 262144
tcp_w_buf_size : 524288
pkg_rq_size : 1024
pkg_wq_size : 512
tcp_read_timeout : "1s"
tcp_write_timeout : "5s"
wait_timeout : "1s"
max_msg_len : 1024
session_name : "getty-rpc-server"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment