Skip to content

Commit c02563f

Browse files
author
chenjie
committed
update
1 parent 6a8bdb6 commit c02563f

File tree

8 files changed

+74
-29
lines changed

8 files changed

+74
-29
lines changed

codegen/tml/config/template_config.go

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"time"
1818
"unsafe"
1919
20-
"github.com/chenjie199234/Corelib/discovery"
2120
"github.com/chenjie199234/Corelib/log"
2221
"github.com/chenjie199234/Corelib/redis"
2322
"github.com/chenjie199234/Corelib/sql"

codegen/tml/gomod/template_gomod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const text = `module {{.}}
1111
go 1.15
1212
1313
require (
14-
github.com/chenjie199234/Corelib v0.0.6
14+
github.com/chenjie199234/Corelib v0.0.7
1515
github.com/fsnotify/fsnotify v1.4.9
1616
github.com/golang/protobuf v1.4.3
1717
github.com/segmentio/kafka-go v0.4.8

codegen/tml/kubernetes/kubernetes.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ WORKDIR /root/app
1616
1717
SHELL ["sh","-c"]
1818
19-
ENV DiscoveryServerGroup <DiscoveryServerGroup>
20-
ENV DiscoveryServerName <DiscoveryServerName>
21-
ENV DiscoveryServerPort <DiscoveryServerPort>
22-
2319
ENTRYPOINT ["./main"]`
2420

2521
const deploymenttext = `apiVersion: apps/v1
@@ -64,6 +60,14 @@ spec:
6460
value: kubernetes
6561
- name: RUN_ENV
6662
value: <RUN_ENV>
63+
- name: DISCOVERY_SERVER_GROUP
64+
value: <DISCOVERY_SERVER_GROUP>
65+
- name: DISCOVERY_SERVER_NAME
66+
value: <DISCOVERY_SERVER_NAME>
67+
- name: DISCOVERY_SERVER_PORT
68+
value: <DISCOVERY_SERVER_PORT>
69+
- name: DISCOVERY_SERVER_VERIFY_DATA
70+
value: <DISCOVERY_SERVER_VERIFY_DATA>
6771
livenessProbe:
6872
tcpSocket:
6973
port: 8000

codegen/tml/mainfile/template_main.go

+39
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,28 @@ import (
1313
"os/signal"
1414
"sync"
1515
"syscall"
16+
"time"
1617
18+
"{{.}}/api"
1719
"{{.}}/config"
1820
"{{.}}/server/xrpc"
1921
"{{.}}/server/xweb"
2022
"{{.}}/service"
23+
24+
"github.com/chenjie199234/Corelib/discovery"
25+
"github.com/chenjie199234/Corelib/log"
2126
)
2227
2328
func main() {
2429
//stop watching config hot update
2530
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 {
34+
log.Error(e)
35+
return
36+
}
37+
}
2638
//start the whole business service
2739
service.StartService()
2840
//start low level net service
@@ -48,6 +60,33 @@ func main() {
4860
}
4961
wg.Done()
5062
}()
63+
stop := make(chan struct{}, 1)
64+
go func() {
65+
tmer := time.NewTimer(time.Millisecond * 200)
66+
select {
67+
case <-tmer.C:
68+
rpcc := config.GetRpcConfig()
69+
webc := config.GetHttpConfig()
70+
regmsg := &discovery.RegMsg{}
71+
if webc != nil {
72+
if webc.HttpKeyFile != "" && webc.HttpCertFile != "" {
73+
regmsg.WebScheme = "https"
74+
} else {
75+
regmsg.WebScheme = "http"
76+
}
77+
if webc.HttpPort != 0 {
78+
regmsg.WebPort = int(webc.HttpPort)
79+
}
80+
}
81+
if rpcc != nil {
82+
if rpcc.RpcPort != 0 {
83+
regmsg.RpcPort = int(rpcc.RpcPort)
84+
}
85+
}
86+
discovery.RegisterSelf(regmsg)
87+
case <-stop:
88+
}
89+
}()
5190
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
5291
<-ch
5392
//stop the whole business service

discovery/client.go

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"net"
9+
"os"
910
"strconv"
1011
"strings"
1112
"sync"
@@ -72,6 +73,22 @@ func NewDiscoveryClient(c *stream.InstanceConfig, selfgroup, selfname string, vd
7273
return e
7374
}
7475
if finder == nil {
76+
group := os.Getenv("DISCOVERY_SERVER_GROUP")
77+
if group == "" {
78+
return errors.New("[Discovery.client] missing system env DISCOVERY_SERVER_GROUP")
79+
}
80+
name := os.Getenv("DISCOVERY_SERVER_NAME")
81+
if name == "" {
82+
return errors.New("[Discovery.client] missing system env DISCOVERY_SERVER_NAME")
83+
}
84+
port := os.Getenv("DISCOVERY_SERVER_PORT")
85+
if port == "" {
86+
return errors.New("[Discovery.client] missing system env DISCOVERY_SERVER_PORT")
87+
}
88+
n, e := strconv.Atoi(port)
89+
if e != nil || n <= 0 || n > 65535 {
90+
return errors.New("[Discovery.client] system env DISCOVERY_SERVER_PORT must be number: 1-65535")
91+
}
7592
finder = defaultfinder
7693
}
7794
temp := &DiscoveryClient{

discovery/finder.go

+3-17
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,15 @@ import (
44
"context"
55
"net"
66
"os"
7-
"strconv"
87
"time"
98

109
"github.com/chenjie199234/Corelib/log"
1110
)
1211

1312
func defaultfinder(manually chan struct{}) {
14-
group := os.Getenv("DiscoveryServerGroup")
15-
if group == "" {
16-
panic("[Discovery.client.defaultfinder] missing system env DiscoveryServerGroup")
17-
}
18-
name := os.Getenv("DiscoveryServerName")
19-
if name == "" {
20-
panic("[Discovery.client.defaultfinder] missing system env DiscoveryServerName")
21-
}
22-
port := os.Getenv("DiscoveryServerPort")
23-
n, e := strconv.Atoi(port)
24-
if e != nil {
25-
panic("[Discovery.client.defaultfinder] missing system env DiscoveryServerPort or not number")
26-
}
27-
if n <= 0 || n > 65535 {
28-
panic("[Discovery.client.defaultfinder] system env DiscoveryServerPort out of range,(0-65535)")
29-
}
13+
group := os.Getenv("DISCOVERY_SERVER_GROUP")
14+
name := os.Getenv("DISCOVERY_SERVER_NAME")
15+
port := os.Getenv("DISCOVERY_SERVER_PORT")
3016
host := name + "-service." + group
3117
servername := group + "." + name
3218
finder := func() {

rpc/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type RpcServer struct {
2626
handler map[string]func(string, *Msg)
2727
instance *stream.Instance
2828
verifydata []byte
29-
status int32 //0 stop,1 starting
29+
status int32 //0-created,not started 1-started 2-closed
3030
stopch chan struct{}
3131
}
3232

@@ -63,13 +63,13 @@ func NewRpcServer(c *stream.InstanceConfig, group, name string, vdata []byte, gl
6363
return serverinstance, nil
6464
}
6565
func (s *RpcServer) StartRpcServer(listenaddr string) error {
66-
if atomic.SwapInt32(&s.status, 1) == 1 {
66+
if !atomic.CompareAndSwapInt32(&s.status, 0, 1) {
6767
return nil
6868
}
6969
return s.instance.StartTcpServer(listenaddr)
7070
}
7171
func (s *RpcServer) StopRpcServer() {
72-
if atomic.SwapInt32(&s.status, 0) == 0 {
72+
if atomic.SwapInt32(&s.status, 2) == 2 {
7373
return
7474
}
7575
d, _ := proto.Marshal(&Msg{

web/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type WebServer struct {
2626
global []OutsideHandler
2727
router *httprouter.Router
2828
ctxpool *sync.Pool
29-
status int32
29+
status int32 //0-created,not started 1-started 2-stoped
3030
stopch chan struct{}
3131
}
3232

@@ -215,7 +215,7 @@ func NewWebServer(c *Config, group, name string) (*WebServer, error) {
215215
return instance, nil
216216
}
217217
func (this *WebServer) StartWebServer(listenaddr string, cert, key string) error {
218-
if atomic.SwapInt32(&this.status, 1) == 1 {
218+
if !atomic.CompareAndSwapInt32(&this.status, 0, 1) {
219219
return nil
220220
}
221221
laddr, e := net.ResolveTCPAddr("tcp", listenaddr)
@@ -247,7 +247,7 @@ func (this *WebServer) StartWebServer(listenaddr string, cert, key string) error
247247
return nil
248248
}
249249
func (this *WebServer) StopWebServer() {
250-
if atomic.SwapInt32(&this.status, 0) == 0 {
250+
if atomic.SwapInt32(&this.status, 2) == 2 {
251251
return
252252
}
253253
tmer := time.NewTimer(time.Second)

0 commit comments

Comments
 (0)