go 微服务 consul

服务发现

在微服务中每一个服务都有一个ip+端口,如果由客户端来之间进行连接会不方便,因此服务将自己的ip+端口提交给服务发现(常见的有consul,etcd,nacos),客户端通过服务发现来获取服务的ip+端口
在这里插入图片描述

consul

去下载:https://www.consul.io/downloads,下载后解压,并添加到环境变量

启动使用命令:

consul agent -dev

可以去浏览器访问:http://localhost:8500/

在这里插入图片描述

将grpc服务注册到consul

服务端:

package main

import (
	"context"
	"fmt"
	"github.com/hashicorp/consul/api"
	"google.golang.org/grpc"
	"grpc_consul/pb"
	"net"
)

type My struct {
    
    
	pb.UnimplementedHelloServer //必须嵌套这个
}

func (m *My) SayHello(c context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) {
    
    
	var resp pb.HelloResponse
	resp.ResponseMsg = req.RequestName + " hello !!"
	return &resp, nil
}

func main() {
    
    
	/把grpc服务注册到consul上

	//1.初始化consul配置
	consulConfig := api.DefaultConfig()
	//设置consul服务器地址: 默认127.0.0.1:8500, 如果consul部署到其它服务器上,则填写其它服务器地址
	//consulConfig.Address = "127.0.0.1:8500"

	//2.创建consul对象
	consulClient, err := api.NewClient(consulConfig)
	if err != nil {
    
    
		fmt.Println("api.NewClient error")
		return
	}

	//3. 告诉consul,注册服务的配置信息
	agentService :=api.AgentServiceRegistration{
    
    
		ID:      "1",              // 服务id,顺序填写即可
		Tags:    []string{
    
    "test"}, // tag标签,别名
		Name:    "HelloService",   //服务名称, 注册到服务发现(consul)的K
		Port:    8801,             // 端口号: 需要与下面的监听, 指定 IP、port一致
		Address: "127.0.0.1",      // 当前微服务部署地址: 结合Port在consul设置为V: 需要与下面的监听, 指定 IP、port一致
		Check: &api.AgentServiceCheck{
    
     //健康检测
			TCP:      "127.0.0.1:8801", //前微服务部署地址,端口 : 需要与下面的监听, 指定 IP、port一致
			Timeout:  "3s",             // 超时时间
			Interval: "15s",            // 循环检测间隔时间
		},
	}

	//4、注册服务到consul上
	consulClient.Agent().ServiceRegister(&agentService)

	/grpc远程服务调用
	//1 初始化一个gpc对象
	grpcServer := grpc.NewServer()
	//2 注册服务
	pb.RegisterHelloServer(grpcServer, &My{
    
    })

	//3 设置监听,指定IP
	listener, err := net.Listen("tcp", "127.0.0.1:8801")
	if err != nil {
    
    
		fmt.Println("net Listen error")
		return
	}
	defer listener.Close()

	//4 启动服务
	grpcServer.Serve(listener)
}

客户端:

package main

import (
	"context"
	"fmt"
	"github.com/hashicorp/consul/api"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"grpc_consul/pb"
	"log"
	"strconv"
)

func main() {
    
    
	//----------------------------consul相关---------------------------
	//初始化consul配置, 客户端服务器需要一致
	consulConfig := api.DefaultConfig()

	//2、获取consul操作对象
	consulClient, _ := api.NewClient(consulConfig)

	//3、获取consul服务发现地址,返回的ServiceEntry是一个结构体数组(服务集群)
	//参数说明:service:服务名称,服务端设置的那个Name, tag:标签,服务端设置的那个Tags,, passingOnly bool, q: 参数
	serviceEntry, _, _ := consulClient.Health().Service("HelloService", "test", false, nil)

	//打印地址
	fmt.Println(serviceEntry[0].Service.Address)
	fmt.Println(serviceEntry[0].Service.Port)

	//拼接从服务发现中获取的地址
	//strconv.Itoa: int转string型
	address := serviceEntry[0].Service.Address + ":" + strconv.Itoa(serviceEntry[0].Service.Port)

	//----------------------------hello微服务相关------------------------------

	//连接grpc服务(无加密)
	conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
    
    
		log.Fatal(err)
	}
	defer conn.Close()

	//初始化grpc客户端
	grpcClient := pb.NewHelloClient(conn)
	resp, err := grpcClient.SayHello(context.Background(), &pb.HelloRequest{
    
    RequestName: "诗仙李白"})
	if err != nil {
    
    
		log.Fatal(err)
	}
	fmt.Println(resp.ResponseMsg)
}

猜你喜欢

转载自blog.csdn.net/qxjswl/article/details/132393569