add cmux multiplexer to run raft and ws on the same port
fix discovery broadcast
This commit is contained in:
1 parent
415a694228
commit
8c15105087
9 files changed
+85
-39
No files matched your search
Generated
+17
@@ -327,6 +327,14 @@
|
||||
pruneopts = "UT"
|
||||
revision = "5475a8b08e62e41fa886641861ac225e7f91e4e5"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:0c71f0a72776491131fad381f212cbacfac2b9470813acbf0bae1abc9a17aad8"
|
||||
name = "github.com/integration-system/swag"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "06a1b734ba9338f6a5c476bd2f08c09168d9c791"
|
||||
version = "v1.6.3"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:01ed62f8f4f574d8aff1d88caee113700a2b44c42351943fa73cc1808f736a50"
|
||||
name = "github.com/jinzhu/inflection"
|
||||
@@ -502,6 +510,14 @@
|
||||
revision = "839c75faf7f98a33d445d181f3018b5c3409a45e"
|
||||
version = "v1.4.2"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:30e06e6d62a1d694e3cdbff29d8a9a96022e05a487d0c6eaf0ef898965ef28fb"
|
||||
name = "github.com/soheilhy/cmux"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "e09e9389d85d8492d313d73d1469c029e710623f"
|
||||
version = "v0.1.4"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:bb495ec276ab82d3dd08504bbc0594a65de8c3b22c6f2aaa92d05b73fbf3a82e"
|
||||
name = "github.com/spf13/afero"
|
||||
@@ -771,6 +787,7 @@
|
||||
"github.com/pkg/errors",
|
||||
"github.com/satori/go.uuid",
|
||||
"github.com/sirupsen/logrus",
|
||||
"github.com/soheilhy/cmux",
|
||||
"github.com/spf13/cast",
|
||||
"github.com/thecodeteam/goodbye",
|
||||
"google.golang.org/grpc/codes",
|
||||
|
||||
@@ -51,3 +51,7 @@
|
||||
[[override]]
|
||||
name = "github.com/hashicorp/raft"
|
||||
branch = "master"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/soheilhy/cmux"
|
||||
version = "0.1.4"
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -93,27 +92,12 @@ func getUrl(address string) string {
|
||||
if err != nil {
|
||||
panic(err) // must never occurred
|
||||
}
|
||||
|
||||
cfg := config.Get().(*conf.Configuration)
|
||||
port, err := strconv.Atoi(cfg.WS.Rest.Port)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// TODO логика для определения порта пира, т.к всё тестируется на одной машине
|
||||
//peerNumber := addr.Port - 9000
|
||||
//switch peerNumber {
|
||||
//case 2:
|
||||
// port = 9011
|
||||
//case 3:
|
||||
// port = 9021
|
||||
//case 4:
|
||||
// port = 9031
|
||||
//}
|
||||
//
|
||||
|
||||
params := url.Values{}
|
||||
params.Add(ClusterParam, "true")
|
||||
// TODO вынести ключи в константы в isp-lib и выпилить instance_uuid
|
||||
params.Add("module_name", cfg.ModuleName)
|
||||
params.Add("instance_uuid", "9d89354b-c728-4b48-b002-a7d3b229f151")
|
||||
return fmt.Sprintf("ws://%s:%d/isp-etp/?%s", addr.IP.String(), port, params.Encode())
|
||||
return fmt.Sprintf("ws://%s:%d/isp-etp/?%s", addr.IP.String(), addr.Port, params.Encode())
|
||||
}
|
||||
@@ -33,4 +33,7 @@ const (
|
||||
DiscoveryServiceSendModulesError = 1019
|
||||
RoutesServiceSendRoutesError = 1020
|
||||
ConfigServiceBroadcastConfigError = 1021
|
||||
|
||||
// Cmux
|
||||
InitCmuxError = 1022
|
||||
)
|
||||
+3
-3
@@ -20,7 +20,7 @@ ws:
|
||||
port: "9092"
|
||||
ip: "127.0.0.1"
|
||||
raft:
|
||||
port: "9003"
|
||||
port: "9001"
|
||||
ip: "0.0.0.0"
|
||||
|
||||
metrics:
|
||||
@@ -35,6 +35,6 @@ metrics:
|
||||
cluster:
|
||||
dataDir: ./data
|
||||
inMemory: true
|
||||
outerAddress: 127.0.0.1:9003
|
||||
outerAddress: 127.0.0.1:9001
|
||||
peers:
|
||||
- 127.0.0.1:9003
|
||||
- 127.0.0.1:9001
|
||||
@@ -2,12 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/integration-system/isp-etp-go"
|
||||
"github.com/integration-system/isp-lib/backend"
|
||||
"github.com/integration-system/isp-lib/bootstrap"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/soheilhy/cmux"
|
||||
"github.com/thecodeteam/goodbye"
|
||||
"isp-config-service/cluster"
|
||||
"isp-config-service/codes"
|
||||
@@ -24,7 +26,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -64,8 +65,14 @@ func main() {
|
||||
}
|
||||
|
||||
model.DbClient.ReceiveConfiguration(cfg.Database)
|
||||
_, raftStore := initRaft(cfg.WS.Raft.GetAddress(), cfg.Cluster, declaration)
|
||||
initWebsocket(ctx, cfg.WS.Rest.GetAddress(), raftStore)
|
||||
|
||||
httpListener, raftListener, err := initMultiplexer(cfg.WS.Rest)
|
||||
if err != nil {
|
||||
log.Fatalf(codes.InitCmuxError, "init cmux: %v", err)
|
||||
}
|
||||
|
||||
_, raftStore := initRaft(raftListener, cfg.Cluster, declaration)
|
||||
initWebsocket(ctx, httpListener, raftStore)
|
||||
initGrpc(cfg.WS.Grpc, raftStore)
|
||||
|
||||
defer goodbye.Exit(ctx, -1)
|
||||
@@ -75,7 +82,29 @@ func main() {
|
||||
<-shutdownChan
|
||||
}
|
||||
|
||||
func initWebsocket(ctx context.Context, bindAddress string, raftStore *store.Store) {
|
||||
func initMultiplexer(addressConfiguration structure.AddressConfiguration) (net.Listener, net.Listener, error) {
|
||||
outerAddr, err := net.ResolveTCPAddr("tcp4", addressConfiguration.GetAddress())
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("resolve outer address: %v", err)
|
||||
}
|
||||
tcpListener, err := net.ListenTCP("tcp4", outerAddr)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("create tcp transport: %v", err)
|
||||
}
|
||||
|
||||
m := cmux.New(tcpListener)
|
||||
httpListener := m.Match(cmux.HTTP1())
|
||||
raftListener := m.Match(cmux.Any())
|
||||
|
||||
go func() {
|
||||
if err := m.Serve(); err != nil {
|
||||
log.Fatalf(codes.InitCmuxError, "serve cmux: %v", err)
|
||||
}
|
||||
}()
|
||||
return httpListener, raftListener, nil
|
||||
}
|
||||
|
||||
func initWebsocket(ctx context.Context, listener net.Listener, raftStore *store.Store) {
|
||||
etpConfig := etp.ServerConfig{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
@@ -84,9 +113,9 @@ func initWebsocket(ctx context.Context, bindAddress string, raftStore *store.Sto
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/isp-etp/", etpServer.ServeHttp)
|
||||
httpServer := &http.Server{Addr: bindAddress, Handler: mux}
|
||||
httpServer := &http.Server{Handler: mux}
|
||||
go func() {
|
||||
if err := httpServer.ListenAndServe(); err != nil {
|
||||
if err := httpServer.Serve(listener); err != nil {
|
||||
log.Fatalf(codes.StartHttpServerError, "http server closed: %v", err)
|
||||
}
|
||||
}()
|
||||
@@ -94,14 +123,14 @@ func initWebsocket(ctx context.Context, bindAddress string, raftStore *store.Sto
|
||||
holder.HttpServer = httpServer
|
||||
}
|
||||
|
||||
func initRaft(bindAddress string, clusterCfg conf.ClusterConfiguration, declaration structure.BackendDeclaration) (*cluster.Client, *store.Store) {
|
||||
func initRaft(listener net.Listener, clusterCfg conf.ClusterConfiguration, declaration structure.BackendDeclaration) (*cluster.Client, *store.Store) {
|
||||
raftState, err := store.NewStateFromRepository()
|
||||
if err != nil {
|
||||
log.Fatal(codes.RestoreFromRepositoryError, err)
|
||||
return nil, nil
|
||||
}
|
||||
raftStore := store.NewStateStore(raftState)
|
||||
r, err := raft.NewRaft(bindAddress, clusterCfg, raftStore)
|
||||
r, err := raft.NewRaft(listener, clusterCfg, raftStore)
|
||||
if err != nil {
|
||||
log.Fatalf(codes.InitRaftError, "unable to create raft server. %v", err)
|
||||
return nil, nil
|
||||
|
||||
+3
-9
@@ -83,20 +83,14 @@ func (r *Raft) listenLeader() {
|
||||
}
|
||||
}
|
||||
|
||||
func NewRaft(bind string, configuration conf.ClusterConfiguration, state raft.FSM) (*Raft, error) {
|
||||
func NewRaft(tcpListener net.Listener, configuration conf.ClusterConfiguration, state raft.FSM) (*Raft, error) {
|
||||
logStore, store, snapshotStore, err := makeStores(configuration)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outerAddr, err := net.ResolveTCPAddr("tcp", configuration.OuterAddress)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "resolve outer address")
|
||||
}
|
||||
trans, err := raft.NewTCPTransport(bind, outerAddr, len(configuration.Peers), defaultConnectTimeout, os.Stdout)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create tcp transport")
|
||||
}
|
||||
streamLayer := &StreamLayer{Listener: tcpListener}
|
||||
trans := raft.NewNetworkTransport(streamLayer, len(configuration.Peers), defaultConnectTimeout, os.Stdout)
|
||||
|
||||
cfg := raft.DefaultConfig()
|
||||
cfg.Logger = &LoggerAdapter{name: "RAFT"}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package raft
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/raft"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type StreamLayer struct {
|
||||
net.Listener
|
||||
}
|
||||
|
||||
func (r *StreamLayer) Dial(address raft.ServerAddress, timeout time.Duration) (net.Conn, error) {
|
||||
return net.DialTimeout("tcp", string(address), timeout)
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func (ds *discoveryService) BroadcastModuleAddresses(moduleName string, mesh sta
|
||||
if err != nil {
|
||||
log.Errorf(codes.DiscoveryServiceSendModulesError, "broadcast module connected %v", err)
|
||||
}
|
||||
}(moduleName, event, addressList)
|
||||
}(event, event, addressList)
|
||||
}
|
||||
|
||||
func (ds *discoveryService) broadcastAddrList(room string, event string, addressList []structure.AddressConfiguration) error {
|
||||
|
||||
Reference in new issue
Block a user