Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add etcd registry #14

Merged
merged 2 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ jobs:
image: 'zookeeper:3.7.0'
ports:
- '2181:2181'
etcd:
image: bitnami/etcd:latest
ports:
- "2379:2379"
- "2380:2380"
env:
ALLOW_NONE_AUTHENTICATION: yes
ETCD_ADVERTISE_CLIENT_URLS: http://etcd:2379
nacos:
image: 'nacos/nacos-server:2.0.3'
ports:
- '8848:8848'
env:
MODE: standalone

steps:
- uses: actions/checkout@v3

Expand Down
18 changes: 18 additions & 0 deletions etcd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
NODE1=172.18.31.33
REGISTRY=bitnami/etcd
ETCD_VERSION=latest
# run cluster
prepare-cluster:
cd example
docker-compose up -d

# run single node in docker
prepare:
docker pull ${REGISTRY}:${ETCD_VERSION}
docker run -d --name Etcd-server \
--network app-tier \
--publish 2379:2379 \
--publish 2380:2380 \
--env ALLOW_NONE_AUTHENTICATION=yes \
--env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \
bitnami/etcd:latest
110 changes: 110 additions & 0 deletions etcd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2021 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package etcd

import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"

"github.com/cloudwego/hertz/pkg/app/server/registry"
"github.com/cloudwego/hertz/pkg/common/hlog"
clientv3 "go.etcd.io/etcd/client/v3"
)

const (
defaultTTL = 60
)

// instanceInfo used to stored service basic info in etcd.
type instanceInfo struct {
Network string `json:"network"`
Address string `json:"address"`
Weight int `json:"weight"`
Tags map[string]string `json:"tags"`
}

func serviceKeyPrefix(serviceName string) string {
return etcdPrefix + "/" + serviceName
}

// serviceKey generates the key used to stored in etcd.
func serviceKey(serviceName, addr string) string {
return serviceKeyPrefix(serviceName) + "/" + addr
}

// validateRegistryInfo validate the registry.Info
func validateRegistryInfo(info *registry.Info) error {
if info == nil {
return fmt.Errorf("registry.Info can not be empty")
}
if info.ServiceName == "" {
return fmt.Errorf("registry.Info ServiceName can not be empty")
}
if info.Addr == nil {
return fmt.Errorf("registry.Info Addr can not be empty")
}
return nil
}

// getTTL get the lease from default or from the env
func getTTL() int64 {
var ttl int64 = defaultTTL
if str, ok := os.LookupEnv(ttlKey); ok {
if t, err := strconv.Atoi(str); err == nil {
ttl = int64(t)
}
}
return ttl
}

// Option sets options such as username, tls etc.
type Option func(cfg *clientv3.Config)

// WithTLSOpt returns a option that authentication by tls/ssl.
func WithTLSOpt(certFile, keyFile, caFile string) Option {
return func(cfg *clientv3.Config) {
tlsCfg, err := newTLSConfig(certFile, keyFile, caFile, "")
if err != nil {
hlog.Errorf("HERTZ: tls failed with err: %v , skipping tls.", err)
}
cfg.TLS = tlsCfg
}
}

func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
successful := caCertPool.AppendCertsFromPEM(caCert)
if !successful {
return nil, errors.New("failed to parse ca certificate as PEM encoded content")
}
cfg := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
return cfg, nil
}
Loading