File tree Expand file tree Collapse file tree 3 files changed +57
-9
lines changed
Expand file tree Collapse file tree 3 files changed +57
-9
lines changed Original file line number Diff line number Diff line change 1515package ant
1616
1717import (
18+ "net"
1819 "sync"
1920
21+ "github.com/henrylee2cn/goutil"
2022 tp "github.com/henrylee2cn/teleport"
2123)
2224
25+ // InnerIpPort returns the service's intranet address, such as '192.168.1.120:8080'.
26+ func InnerIpPort (port string ) (string , error ) {
27+ host , err := goutil .IntranetIP ()
28+ if err != nil {
29+ return "" , err
30+ }
31+ hostPort := net .JoinHostPort (host , port )
32+ return hostPort , nil
33+ }
34+
35+ // OuterIpPort returns the service's extranet address, such as '113.116.141.121:8080'.
36+ func OuterIpPort (port string ) (string , error ) {
37+ host , err := goutil .ExtranetIP ()
38+ if err != nil {
39+ return "" , err
40+ }
41+ hostPort := net .JoinHostPort (host , port )
42+ return hostPort , nil
43+ }
44+
2345var initOnce sync.Once
2446
2547func doInit () {
Original file line number Diff line number Diff line change @@ -24,17 +24,16 @@ func (p *P) Divide(args *Args) (int, *tp.Rerror) {
2424}
2525
2626func main () {
27- srv := ant . NewServer ( ant.SrvConfig {
27+ cfg := ant.SrvConfig {
2828 ListenAddress : ":9090" ,
2929 // EnableHeartbeat: true,
30- },
31- discovery .ServicePlugin (
32- "localhost:9090" ,
33- etcd.EasyConfig {
34- Endpoints : []string {"http://127.0.0.1:2379" },
35- },
36- ),
37- )
30+ }
31+ srv := ant .NewServer (cfg , discovery .ServicePlugin (
32+ cfg .InnerIpPort (),
33+ etcd.EasyConfig {
34+ Endpoints : []string {"http://127.0.0.1:2379" },
35+ },
36+ ))
3837 srv .RoutePull (new (P ))
3938 srv .Listen ()
4039}
Original file line number Diff line number Diff line change @@ -48,6 +48,33 @@ func (s *SrvConfig) Reload(bind cfgo.BindFunc) error {
4848 return bind ()
4949}
5050
51+ // ListenPort returns the listened port, such as '8080'.
52+ func (s * SrvConfig ) ListenPort () string {
53+ _ , port , err := net .SplitHostPort (s .ListenAddress )
54+ if err != nil {
55+ Fatalf ("%v" , err )
56+ }
57+ return port
58+ }
59+
60+ // InnerIpPort returns the service's intranet address, such as '192.168.1.120:8080'.
61+ func (s * SrvConfig ) InnerIpPort () string {
62+ hostPort , err := InnerIpPort (s .ListenPort ())
63+ if err != nil {
64+ Fatalf ("%v" , err )
65+ }
66+ return hostPort
67+ }
68+
69+ // OuterIpPort returns the service's extranet address, such as '113.116.141.121:8080'.
70+ func (s * SrvConfig ) OuterIpPort () string {
71+ hostPort , err := OuterIpPort (s .ListenPort ())
72+ if err != nil {
73+ Fatalf ("%v" , err )
74+ }
75+ return hostPort
76+ }
77+
5178func (s * SrvConfig ) peerConfig () tp.PeerConfig {
5279 return tp.PeerConfig {
5380 DefaultSessionAge : s .DefaultSessionAge ,
You can’t perform that action at this time.
0 commit comments