current position:Home>grpc uses consul for service registration and discovery
grpc uses consul for service registration and discovery
2022-08-06 09:24:14【kankan231】
目录结构:
client/main.go:
package main
import (
"context"
"demo/config"
"demo/proto"
"fmt"
"log"
"time"
_ "github.com/mbobakov/grpc-consul-resolver"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
var srvClient proto.GreeterClient
// 服务
if conn := InitSrvConn(config.ServiceName); conn != nil {
srvClient = proto.NewGreeterClient(conn)
}
req := &proto.HelloRequest{
Name: "",
}
for i := 0; i < 1000; i++ {
req.Name = fmt.Sprintf("james%d", i)
resp, err := srvClient.SayHello(context.Background(), req)
if err != nil {
log.Fatalln(err)
}
log.Println(resp.Message)
time.Sleep(time.Second)
}
}
// At the same time completed the service discovery and load balancing algorithm(轮询)
func InitSrvConn(srvName string) *grpc.ClientConn {
conn, err := grpc.Dial(
fmt.Sprintf("consul://%s:%d/%s?healthy=true&wait=14s",
config.ConsulIp, config.ConsulPort, srvName),
grpc.WithTransportCredentials(insecure.NewCredentials()),
// grpcThere seems to be only supports polling load balancing algorithm
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
)
if err != nil {
log.Println("serviceLayer the service discovery error: ", err)
return nil
}
return conn
}
ptoto/greet.proto
syntax = "proto3";
option go_package = ".;proto";
service Greeter{
rpc SayHello(HelloRequest) returns (HelloReply);
}
message HelloRequest{
string name = 1;
}
message HelloReply{
string message = 1;
}
server/main.go
package main
import (
"context"
"demo/proto"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"demo/config"
"github.com/hashicorp/consul/api"
uuid "github.com/satori/go.uuid"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
)
type Server struct{}
var (
Port *int
IP *string
)
func (s Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {
return &proto.HelloReply{Message: fmt.Sprintf("hello %s from %s:%d", request.Name, *IP, *Port)}, nil
}
func main() {
// Set to start a local serviceip和端口号
// 默认ip如果改成127.0.0.1会导致consul健康检查失败
IP = flag.String("ip", "192.168.168.6", "ip地址")
Port = flag.Int("port", 8100, "端口号")
flag.Parse()
g := grpc.NewServer()
proto.RegisterGreeterServer(g, &Server{})
tcpAddr := fmt.Sprintf("%s:%d", *IP, *Port)
log.Printf("service listen:%s", tcpAddr)
lis, err := net.Listen("tcp", tcpAddr)
if err != nil {
log.Panicln("failed to listen:" + err.Error())
}
// 注册健康检查
grpc_health_v1.RegisterHealthServer(g, health.NewServer())
// 将当前grpc服务注册到consul
serviceId := uuid.NewV4().String()
client, err := RegisterGRPCService(*IP, config.ServiceName, serviceId, *Port, nil)
if err != nil {
log.Panicln("grpc服务注册失败:", err)
}
// Will start the service part in coroutines inside,Execution of behind the listening part of termination signal can be
go func() {
err = g.Serve(lis)
if err != nil {
log.Panicln("failed to start grpc:" + err.Error())
}
}()
// 接收终止信号
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
if err = client.Agent().ServiceDeregister(serviceId); err != nil {
log.Println("Service logout failed")
} else {
log.Println("Service log out successfully")
}
}
// 函数功能:将grpc服务注册到consul
// 参数说明
// address:For the registered serviceip
// name:服务名称
// id:服务id
// port:服务端口
// tags:服务标签
func RegisterGRPCService(address, name, id string, port int, tags []string) (*api.Client, error) {
cfg := api.DefaultConfig()
// 设置consulService running inip和端口
cfg.Address = fmt.Sprintf("%s:%d", config.ConsulIp, config.ConsulPort)
//cfg.Address的ip可以是127.0.0.1
client, err := api.NewClient(cfg)
if err != nil {
log.Panic(err)
}
// Generate health inspection object
check := &api.AgentServiceCheck{
// 这里的ip不可以是127.0.0.1
GRPC: fmt.Sprintf("%s:%d", address, port), // 服务的运行地址
Timeout: "5s", // More than this time tell service status is not healthy
Interval: "5s", // 每5s检查一次
DeregisterCriticalServiceAfter: "30s", // How long after cancellation of service failure
}
// 生成注册对象
registration := &api.AgentServiceRegistration{
Name: name,
ID: id,
Address: address,
Port: port,
Tags: tags,
Check: check,
}
// 注册服务
return client, client.Agent().ServiceRegister(registration)
}
运行步骤:
1 启动consul
2 使用protoc工具生成greet.pb.go
3 启动多个服务端
4 启动客户端
运行结果大致如下,
copyright notice
author[kankan231],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060917043039.html
The sidebar is recommended
- 2022 Hailiang SC Travel Notes
- dalle2: hierarchical text-conditional image generation with clip
- Tencent Cloud VOD uploads video files to solve the path problem
- LeetCode - 1047. Remove all adjacent duplicates in a string
- 2022-08-05: What does the following go code output?A: 65, string; B: A, string; C: 65, int; D: error.
- LeetCode - 345. The reversal in the string vowels
- Page Loading Animation_Gradient Color Rotating Small Circle
- 韩流体小球加载动画
- Card hovering frosted glass effect
- How does the data security law apply to enterprises?
guess what you like
Full screen digital preload animation
Day 16 (Configuration BPDU, TCN BPDU)
VLAN experiment
ROS error [rospack] Error: package ‘.....‘ not found
Token design scheme under microservice
Combination of Leetcode77.
Native js implements table table
Day 17 (16 day bpdus related knowledge and STP configuration)
Native js implements mouse following to display floating box information
Exchange comprehensive experiment (to be supplemented)
Random recommended
- Detailed explanation of Mysql things (important)
- Linux - several ways to install MySQL
- /var/log/messages is empty
- The 22nd day of the special assault version of the sword offer
- Stone Atom Technology officially joined the openGauss community
- 18 days (link aggregation of configuration, the working process of the VRRP, IPV6 configuration)
- From "prairie cattle" to "digital cattle": Mengniu's digital transformation!
- Summary of the experience of project operation and maintenance work
- WPF - Styles and Templates
- BigEvent Demo
- rain cloud animation
- VS namespace names of different projects of the same solution are unique
- Flashing Neon Text Animation
- ACM common header files
- Free and open source web version of Xshell [Happy New Year to everyone]
- Timed task appears A component required a bean named ‘xxx‘ that could not be found
- Two important self-learning functions in pytorch dir(); help()
- [Mathematical Modeling] Linear Programming
- Folyd
- 【Untitled】
- HCIP 18 days notes
- The web version of Xshell supports FTP connection and SFTP connection
- The values in the array into another array, and capital
- Remember to deduplicate es6 Set to implement common menus
- View the Linux log on the web side, and view the Linux log on the web side
- 21-day Learning Challenge--Pick-in on the third day (dynamically change the app icon)
- Xshell download crack, the history of the most simple tutorial
- How is the LinkedList added?
- Web version Xshell supports FTP connection and SFTP connection [Detailed tutorial] Continue from the previous article
- Usage of torch.utils.data in pytorch ---- Loading Data
- Experiment 9 (Exchange Comprehensive Experiment)
- [Mathematical Modeling] Integer Programming
- "Introduction to nlp + actual combat: Chapter 9: Recurrent Neural Network"
- Expansion mechanism of ArrayList
- (5) BuyFigrines Hd 2022 school training
- [Nanny-level tutorial] How does Tencent Cloud obtain secretId and secretKey, and enable face service
- RL reinforcement learning summary (2)
- ELT.zip 】 【 OpenHarmony chew club - the methodology of academic research paper precipitation series
- Hdu 2022 Multi-School Training (5) Slipper
- Dijkstr heap optimization