Skip to content

easy grpc naming and resolver/loadbalancing for golang

License

Notifications You must be signed in to change notification settings

whatvn/discovery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

discovery

simple naming and resolver service for grpc, support etcd and redis.

Usage

1. Redis

Server

package main

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/whatvn/discovery/example/protobuf"
	"github.com/whatvn/discovery/redis"
	"google.golang.org/grpc"
	"net"
)

type Hello struct{}

func (s *Hello) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {

	response := &pb.HelloResponse{
		Reply: "hi",
	}
	return response, nil
}


func (s *Hello) SayHelloAnonymous(ctx context.Context, in *empty.Empty) (*pb.HelloResponse, error) {

	response := &pb.HelloResponse{
		Reply: "hoho",
	}
	return response, nil
}
func main()  {
	addr := "127.0.0.1:8080"
	registry := redis.New("127.0.0.1:6379", "","hello.svc")
	if err := registry.Register(addr, 5); err != nil {
		panic(err)
	}
	listener, err := net.Listen("tcp", addr)

	if err != nil {
		panic(err)
	}
	server := grpc.NewServer()
	pb.RegisterHelloServiceServer(server, new(Hello))
	server.Serve(listener)
}

Client

package main

import (
	"context"
	"fmt"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/whatvn/discovery"
	pb "github.com/whatvn/discovery/example/protobuf"
	"github.com/whatvn/discovery/redis"
	"google.golang.org/grpc"
)

func main() {
	registry := redis.NewResolver("127.0.0.1:6379", "", "hello.svc")
	conn, err := grpc.Dial(registry.SvcName(), discovery.DefaultBalancePolicy(), grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	client := pb.NewHelloServiceClient(conn)
	response, err := client.SayHelloAnonymous(context.Background(), &empty.Empty{})
	fmt.Println(response, err)
}

2. Etcd

Server

package main

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/whatvn/discovery/etcd"
	"github.com/whatvn/discovery/example/protobuf"
	"google.golang.org/grpc"
	"net"
)

type Hello struct{}

func (s *Hello) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {

	response := &pb.HelloResponse{
		Reply: "hi",
	}
	return response, nil
}


func (s *Hello) SayHelloAnonymous(ctx context.Context, in *empty.Empty) (*pb.HelloResponse, error) {

	response := &pb.HelloResponse{
		Reply: "hoho",
	}
	return response, nil
}
func main()  {
	addr := "127.0.0.1:8080"
	registry := etcd.New("10.109.3.93:7379", "hello.svc")
	if err := registry.Register(addr, 5); err != nil {
		panic(err)
	}
	listener, err := net.Listen("tcp", addr)

	if err != nil {
		panic(err)
	}
	server := grpc.NewServer()
	pb.RegisterHelloServiceServer(server, new(Hello))
	server.Serve(listener)
}

Client

package main

import (
	"context"
	"fmt"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/whatvn/discovery"
	"github.com/whatvn/discovery/etcd"
	pb "github.com/whatvn/discovery/example/protobuf"
	"google.golang.org/grpc"
)

func main() {
	registry := etcd.NewResolver("10.109.3.93:7379", "hello.svc")
	conn, err := grpc.Dial(registry.SvcName(), discovery.DefaultBalancePolicy(), grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	client := pb.NewHelloServiceClient(conn)
	response, err := client.SayHelloAnonymous(context.Background(), &empty.Empty{})
	fmt.Println(response, err)
}

if you like this project, you may also like denny, this is extracted from denny

About

easy grpc naming and resolver/loadbalancing for golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages