Created
December 27, 2017 04:38
-
-
Save guoyoujin/2dfadbad6ebb9509889671091fa1e342 to your computer and use it in GitHub Desktop.
rpcx etcd tls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// go run -tags etcd server.go | |
package main | |
import ( | |
"context" | |
"flag" | |
"log" | |
"time" | |
"crypto/tls" | |
metrics "github.com/rcrowley/go-metrics" | |
example "github.com/rpcx-ecosystem/rpcx-examples3" | |
"github.com/smallnest/rpcx/server" | |
"github.com/smallnest/rpcx/serverplugin" | |
"github.com/docker/libkv/store" | |
"crypto/x509" | |
"io/ioutil" | |
) | |
var ( | |
addr = flag.String("addr", "110.210.114.227:8973", "server address") | |
etcdAddr = flag.String("etcdAddr", "110.210.114.227:2379", "etcd address") | |
basePath = flag.String("base", "/rpcx_test", "prefix path") | |
) | |
type Arith2 int | |
func (t *Arith2) Mul(ctx context.Context, args *example.Args, reply *example.Reply) error { | |
reply.C = args.A * args.B * 10 | |
return nil | |
} | |
func main() { | |
flag.Parse() | |
s := server.NewServer() | |
addRegistryPlugin(s) | |
s.RegisterName("Arith", new(Arith2), "") | |
s.Serve("tcp", *addr) | |
} | |
func addRegistryPlugin(s *server.Server) { | |
cert, err_0 := ioutil.ReadFile("etcd-root-ca.pem") | |
if err_0 != nil { | |
log.Fatalf("Couldn't load file", err_0) | |
} | |
certPool := x509.NewCertPool() | |
certPool.AppendCertsFromPEM(cert) | |
conf := &tls.Config{ | |
RootCAs: certPool, | |
} | |
op := &store.Config{} | |
op.TLS = conf | |
r := &serverplugin.EtcdRegisterPlugin{ | |
ServiceAddress: "tcp@" + *addr, | |
EtcdServers: []string{*etcdAddr}, | |
BasePath: *basePath, | |
Metrics: metrics.NewRegistry(), | |
UpdateInterval: time.Second, | |
Options: op, | |
} | |
err := r.Start() | |
if err != nil { | |
log.Fatal(err) | |
} | |
s.Plugins.Add(r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment