Skip to content

Commit b4c19eb

Browse files
author
chenjie
committed
update
1 parent 6e9567c commit b4c19eb

24 files changed

+291
-132
lines changed

codegen/tml/config/template_config.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"encoding/json"
1515
"os"
1616
"path/filepath"
17+
"strings"
1718
"sync/atomic"
1819
"time"
1920
"unsafe"
@@ -44,7 +45,10 @@ var AC *AppConfig
4445
var watcher *fsnotify.Watcher
4546
var closech chan struct{}
4647
47-
func init() {
48+
func Init() {
49+
if strings.ToLower(os.Getenv("REMOTE_CONFIG")) == "true" {
50+
//get remote config
51+
}
4852
data, e := os.ReadFile("SourceConfig.json")
4953
if e != nil {
5054
log.Error("[SourceConfig] read config file error:", e)
@@ -86,6 +90,7 @@ func init() {
8690
closech = make(chan struct{})
8791
go watch()
8892
}
93+
8994
func watch() {
9095
defer close(closech)
9196
for {
@@ -146,10 +151,11 @@ type RpcConfig struct {
146151
//WebConfig -
147152
type WebConfig struct {
148153
//server
149-
WebTimeout ctime.Duration $json:"web_timeout"$ //default 500ms
150-
WebStaticFile string $json:"web_staticfile"$
151-
WebCertFile string $json:"web_certfile"$
152-
WebKeyFile string $json:"web_keyfile"$
154+
UsePprof bool $json:"use_pprof"$
155+
WebTimeout ctime.Duration $json:"web_timeout"$ //default 500ms
156+
WebStaticFile string $json:"web_staticfile"$
157+
WebCertFile string $json:"web_certfile"$
158+
WebKeyFile string $json:"web_keyfile"$
153159
//cors
154160
WebCors *WebCorsConfig $json:"web_cors"$
155161
}

codegen/tml/configfile/template_configfile.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const textsource = `{
1414
"rpc_heart_probe":"1.5s"
1515
},
1616
"web":{
17+
"use_pprof":false,
1718
"web_timeout":"200ms",
1819
"web_staticfile":"./src",
1920
"web_certfile":"",

codegen/tml/dao/template_dao.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
func NewApi() error {
2626
//var e error
2727
//rc := config.GetRpcConfig()
28-
//if ExampleRpcApi, e = example.NewExampleRpcClient(time.Duration(rc.RpcTimeout), time.Duration(rc.RpcConnTimeout), time.Duration(rc.RpcHeartTimeout), time.Duration(rc.RpcHeartProbe), api.Group, api.Name, []byte(os.Getenv("RPC_VERIFY_DATA")), nil, nil); e != nil {
28+
//if ExampleRpcApi, e = example.NewExampleRpcClient(time.Duration(rc.RpcTimeout), time.Duration(rc.RpcConnTimeout), time.Duration(rc.RpcHeartTimeout), time.Duration(rc.RpcHeartProbe), api.Group, api.Name, os.Getenv("RPC_VERIFY_DATA"), nil, nil); e != nil {
2929
// return e
3030
//}
3131
//wc := config.GetWebConfig()

codegen/tml/gomod/template_gomod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ go 1.16
1212
1313
require (
1414
github.com/go-sql-driver/mysql v1.5.0
15-
github.com/chenjie199234/Corelib v0.0.10
15+
github.com/chenjie199234/Corelib v0.0.11
1616
github.com/fsnotify/fsnotify v1.4.9
1717
github.com/golang/protobuf v1.4.3
1818
github.com/segmentio/kafka-go v0.4.8

codegen/tml/kubernetes/kubernetes.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ ENV DISCOVERY_SERVER_GROUP=<DISCOVERY_SERVER_GROUP> \
1515
DISCOVERY_SERVER_NAME=<DISCOVERY_SERVER_NAME> \
1616
DISCOVERY_SERVER_PORT=<DISCOVERY_SERVER_PORT>
1717
ENV DISCOVERY_SERVER_VERIFY_DATA=<DISCOVERY_SERVER_VERIFY_DATA> \
18+
OLD_RPC_VERIFY_DATA=<OLD_RPC_VERIFY_DATA> \
1819
RPC_VERIFY_DATA=<RPC_VERIFY_DATA> \
20+
OLD_PPROF_VERIFY_DATA=<OLD_PPROF_VERIFY_DATA> \
21+
PPROF_VERIFY_DATA=<PPROF_VERIFY_DATA> \
22+
REMOTE_CONFIG=<REMOTE_CONFIG> \
1923
RUN_ENV=<RUN_ENV>
2024
COPY main probe.sh AppConfig.json SourceConfig.json ./
2125
ENTRYPOINT ["./main"]`
@@ -56,8 +60,6 @@ spec:
5660
memory: 256Mi
5761
cpu: 250m
5862
env:
59-
- name: APP_NAME
60-
value: {{.ProjectName}}
6163
- name: DEPLOY_ENV
6264
value: kubernetes
6365
livenessProbe:

codegen/tml/mainfile/template_main.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ import (
2626
)
2727
2828
func main() {
29-
//stop watching config hot update
30-
defer config.Close()
31-
discoveryserververifydata := os.Getenv("DISCOVERY_SERVER_VERIFY_DATA")
32-
if discoveryserververifydata != "" {
33-
if e := discovery.NewDiscoveryClient(nil, api.Group, api.Name, []byte(discoveryserververifydata), nil); e != nil {
29+
if os.Getenv("DISCOVERY_SERVER_VERIFY_DATA") != "" {
30+
if e := discovery.NewDiscoveryClient(nil, api.Group, api.Name, os.Getenv("DISCOVERY_SERVER_VERIFY_DATA"), nil); e != nil {
3431
log.Error(e)
3532
return
3633
}
3734
}
35+
config.Init()
36+
defer config.Close()
3837
//start the whole business service
3938
if e := service.StartService(); e != nil {
4039
log.Error(e)
@@ -61,8 +60,9 @@ func main() {
6160
}
6261
wg.Done()
6362
}()
64-
stop := make(chan struct{}, 1)
65-
go func() {
63+
//try to register self to the discovery server
64+
stop := make(chan struct{})
65+
go func(){
6666
//delay 200ms to register self,if error happened in this 200ms,this server will not be registered
6767
tmer := time.NewTimer(time.Millisecond * 200)
6868
select {
@@ -87,8 +87,8 @@ func main() {
8787
}()
8888
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
8989
<-ch
90-
stop <- struct{}{}
91-
//stop the whole business service
90+
close(stop)
91+
//stop the whole business service
9292
service.StopService()
9393
//stop low level net service
9494
//grpc server,if don't need,please comment this

codegen/tml/readme/template_readme.go

+59-34
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,74 @@ package readme
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"text/template"
78
)
89

910
const text = `# {{.}}
10-
{{.}}是一个微服务.<br/>
11-
运行cmd脚本可查看使用方法.windows下将./cmd.sh换为cmd.bat<br/>
12-
./cmd.sh pb 解析proto文件,生成打桩代码<br/>
13-
./cmd.sh run 运行该程序<br/>
14-
./cmd.sh build 编译该程序,会在根目录下生成一个可执行文件<br/>
15-
./cmd.sh new 在该项目中创建一个新的子服务<br/>
16-
./cmd.sh help 输出帮助信息<br/>
17-
./cmd.sh kubernetes 增加或者更新kubernetes的配置<br/>
11+
$$$
12+
{{.}}是一个微服务.
13+
运行cmd脚本可查看使用方法.windows下将./cmd.sh换为cmd.bat
14+
./cmd.sh pb 解析proto文件,生成打桩代码
15+
./cmd.sh run 运行该程序
16+
./cmd.sh build 编译该程序,会在根目录下生成一个可执行文件
17+
./cmd.sh new 在该项目中创建一个新的子服务
18+
./cmd.sh help 输出帮助信息
19+
./cmd.sh kubernetes 增加或者更新kubernetes的配置
20+
$$$
21+
22+
23+
## 环境变量
24+
$$$
25+
DISCOVERY_SERVER_GROUP 指定注册中心服务器所属的group名字
26+
DISCOVERY_SERVER_NAME 指定注册中心服务器自身的名字
27+
DISCOVERY_SERVER_PORT 指定注册中心服务器监听的端口
28+
DISCOVERY_SERVER_VERIFY_DATA 连接注册中心服务器使用的校验数据
29+
OLD_RPC_VERIFY_DATA 自身启动rpc服务时,当进行校验数据更新时才使用
30+
RPC_VERIFY_DATA 自身启动rpc服务时,当前正在使用的最新的校验数据
31+
OLD_PPROF_VERIFY_DATA web服务器使用pprof时才有效,当进行校验数据更新时才使用
32+
PPROF_VERIFY_DATA web服务器使用pprof时才有效,当前正在使用的最新的校验数据
33+
REMOTE_CONFIG 是否使用远程配置中心的配置[true/false]
34+
RUN_ENV 当前运行环境,如:test,pre,prod
35+
DEPLOY_ENV 部署环境,如:k8s,host
36+
$$$
1837
1938
## 配置文件
20-
AppConfig.json该文件配置了该服务需要使用的业务配置,可热更新<br/>
21-
SourceConfig.json该文件配置了该服务需要使用的资源配置,不热更新<br/>
39+
$$$
40+
AppConfig.json该文件配置了该服务需要使用的业务配置,可热更新
41+
SourceConfig.json该文件配置了该服务需要使用的资源配置,不热更新
42+
$$$
2243
2344
## 初始化git
24-
在项目根目录下执行以下命令初始化git本地仓库<br/>
25-
git init<br/>
26-
git add .<br/>
27-
git commit -m "code generate"<br/>
28-
在git远程服务器上创建仓库,然后执行以下命令,将本地仓库与远程仓库关联<br/>
29-
git remote add origin path/to/your/remote/repo<br/>
30-
git push -u origin master<br/>
45+
$$$
46+
在项目根目录下执行以下命令初始化git本地仓库
47+
git init
48+
git add .
49+
git commit -m "code generate"
50+
在git远程服务器上创建仓库,然后执行以下命令,将本地仓库与远程仓库关联
51+
git remote add origin path/to/your/remote/repo
52+
git push -u origin main
53+
$$$
3154
3255
## 开发
33-
切换到开发分支<br/>
34-
git checkout -b your/branch/name/recommend/to/use/feature/name<br/>
35-
开发完成后先提交到开发分支<br/>
36-
git add .<br/>
37-
git commit -m "your/commit/to/this/code/change"<br/>
38-
将代码推送到git远程服务器,执行下面命令会要求set-upstream,直接复制git输出的命令运行<br/>
39-
git push<br/>
40-
登陆到git远程服务器前端页面,创建merge,将代码提交到master,等待review和审批(记得勾选删除开发分支选项)<br/>
41-
merge完成后在自己的本地仓库执行<br/>
42-
git checkout master<br/>
43-
git pull<br/>
44-
以下为可选步骤,用于删除本地开发分支和本地远程分支<br/>
45-
git checkout master<br/>
46-
git pull<br/>
47-
git branch -D your/branch/name/recommend/to/use/feature/name<br/>
48-
git remote prune origin<br/>`
56+
$$$
57+
切换到开发分支
58+
git checkout -b your/branch/name/recommend/to/use/feature/name
59+
开发完成后先提交到开发分支
60+
git add .
61+
git commit -m "your/commit/to/this/code/change"
62+
将代码推送到git远程服务器,执行下面命令会要求set-upstream,直接复制git输出的命令运行
63+
git push
64+
登陆到git远程服务器前端页面,创建merge,将代码提交到master,等待review和审批(记得勾选删除开发分支选项)
65+
merge完成后在自己的本地仓库执行
66+
git checkout main
67+
git pull
68+
以下为可选步骤,用于删除本地开发分支和本地远程分支
69+
git checkout main
70+
git pull
71+
git branch -D your/branch/name/recommend/to/use/feature/name
72+
git remote prune origin
73+
$$$`
4974

5075
const path = "./"
5176
const name = "README.md"
@@ -55,7 +80,7 @@ var file *os.File
5580

5681
func init() {
5782
var e error
58-
tml, e = template.New("api").Parse(text)
83+
tml, e = template.New("api").Parse(strings.Replace(text, "$", "`", -1))
5984
if e != nil {
6085
panic(fmt.Sprintf("create template for %s error:%s", path+name, e))
6186
}

codegen/tml/server/xrpc/template_xrpc.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ func StartRpcServer() {
3838
MaxBufferedWriteMsgNum: 1024,
3939
}
4040
var e error
41-
if s, e = rpc.NewRpcServer(rpcc, api.Group, api.Name, []byte(os.Getenv("RPC_VERIFY_DATA"))); e != nil {
41+
oldvd := os.Getenv("OLD_RPC_VERIFY_DATA")
42+
if oldvd == "<OLD_RPC_VERIFY_DATA>" {
43+
oldvd = ""
44+
}
45+
newvd := os.Getenv("RPC_VERIFY_DATA")
46+
if newvd == "<RPC_VERIFY_DATA>" {
47+
newvd = ""
48+
}
49+
if s, e = rpc.NewRpcServer(rpcc, api.Group, api.Name, oldvd, newvd); e != nil {
4250
log.Error("[xrpc] new rpc server error:", e)
4351
return
4452
}

codegen/tml/server/xweb/template_xweb.go

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
const text = `package xweb
1010
1111
import (
12+
"os"
1213
"time"
1314
1415
"{{.}}/api"
@@ -25,12 +26,23 @@ var s *web.WebServer
2526
//StartWebServer -
2627
func StartWebServer() {
2728
c := config.GetWebConfig()
29+
newvd := os.Getenv("PPROF_VERIFY_DATA")
30+
if newvd == "<PPROF_VERIFY_DATA>" {
31+
newvd = ""
32+
}
33+
oldvd := os.Getenv("OLD_PPROF_VERIFY_DATA")
34+
if oldvd == "<OLD_PPROF_VERIFY_DATA>" {
35+
oldvd = ""
36+
}
2837
webc := &web.Config{
38+
UsePprof: c.UsePprof,
2939
Timeout: time.Duration(c.WebTimeout),
3040
StaticFileRootPath: c.WebStaticFile,
3141
MaxHeader: 1024,
3242
ReadBuffer: 1024,
3343
WriteBuffer: 1024,
44+
PprofVerifyData: newvd,
45+
OldPprofVerifyData: oldvd,
3446
}
3547
if c.WebCors != nil {
3648
webc.Cors = &web.CorsConfig{

discovery/client.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type DiscoveryServerFinder func(manually chan struct{})
3232

3333
//serveruniquename = servername:ip:port
3434
type DiscoveryClient struct {
35-
verifydata []byte
35+
verifydata string
3636
instance *stream.Instance
3737
regdata []byte
3838
status int //0-closing,1-working
@@ -62,7 +62,7 @@ type servernode struct {
6262
var clientinstance *DiscoveryClient
6363

6464
//finder is to find the discovery servers
65-
func NewDiscoveryClient(c *stream.InstanceConfig, selfgroup, selfname string, vdata []byte, finder DiscoveryServerFinder) error {
65+
func NewDiscoveryClient(c *stream.InstanceConfig, selfgroup, selfname string, verifydata string, finder DiscoveryServerFinder) error {
6666
if e := common.NameCheck(selfname, false, true, false, true); e != nil {
6767
return e
6868
}
@@ -92,7 +92,7 @@ func NewDiscoveryClient(c *stream.InstanceConfig, selfgroup, selfname string, vd
9292
finder = defaultfinder
9393
}
9494
temp := &DiscoveryClient{
95-
verifydata: vdata,
95+
verifydata: verifydata,
9696
status: 1,
9797
finder: finder,
9898
manually: make(chan struct{}, 1),
@@ -182,7 +182,7 @@ func UpdateDiscoveryServers(serveraddrs []string) {
182182
}
183183
}
184184
func (c *DiscoveryClient) start(addr, servername string) {
185-
tempverifydata := common.Byte2str(clientinstance.verifydata) + "|" + servername
185+
tempverifydata := clientinstance.verifydata + "|" + servername
186186
if r := c.instance.StartTcpClient(addr, common.Str2byte(tempverifydata)); r == "" {
187187
c.lker.RLock()
188188
server, ok := c.servers[servername+":"+addr]
@@ -380,7 +380,7 @@ func getinfos(appname string, t int) (map[string][]string, []byte) {
380380
}
381381

382382
func (c *DiscoveryClient) verifyfunc(ctx context.Context, serveruniquename string, peerVerifyData []byte) ([]byte, bool) {
383-
if !bytes.Equal(peerVerifyData, c.verifydata) {
383+
if !bytes.Equal(peerVerifyData, common.Str2byte(c.verifydata)) {
384384
return nil, false
385385
}
386386
c.lker.RLock()

discovery/client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Test_Client1(t *testing.T) {
4343
SocketWBufLen: 1024,
4444
MaxBufferedWriteMsgNum: 256,
4545
},
46-
}, "testgroup", "testclient1", []byte{'t', 'e', 's', 't'}, finder)
46+
}, "testgroup", "testclient1", "test", finder)
4747
rch, e := NoticeRpcChanges("testgroup.testclient2")
4848
if e != nil {
4949
panic("notice grpc change error:" + e.Error())
@@ -116,7 +116,7 @@ func Test_Client2(t *testing.T) {
116116
SocketWBufLen: 1024,
117117
MaxBufferedWriteMsgNum: 256,
118118
},
119-
}, "testgroup", "testclient2", []byte{'t', 'e', 's', 't'}, finder)
119+
}, "testgroup", "testclient2", "test", finder)
120120
rch, e := NoticeRpcChanges("testgroup.testclient1")
121121
if e != nil {
122122
panic("notice grpc change error:" + e.Error())

0 commit comments

Comments
 (0)