Loading

Consul 服务注册中心安装与配置

Docker 安装 Consul

8500 HTTP 端口
8600 DNS 端口

docker run -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600/udp consul consul agent -dev -client=0.0.0.0

# 设置开机自启
docker container update --restart=always 容器名字/ID

Consul Web

http:// [docker-ip]:8500

image-20220218132920968

Consul DNS

Consul提供 DNS 功能,可以让我们通过, 可以通过dig命令行来测试,consul默认的dns端口是8600

Linux

yay -S bind

Windows

https://www.isc.org/download/

dig @172.17.0.1 -p 8600 consul.service.consul SRV

image-20220218134750247

Consul APi

注册服务与健康检查

HTTP Golang

服务注册 https://www.consul.io/api-docs/agent/service#register-service
健康检查 https://www.consul.io/api-docs/agent/check

import (
	"fmt"
	"github.com/hashicorp/consul/api"
)
// 注册服务
func Register(id, name, address string, port int) {
	// 默认配置
	cfg := api.DefaultConfig()
	cfg.Address = "172.17.0.1:8500"

	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	// 生成服务注册对象
	registation := &api.AgentServiceRegistration{
		ID:      id,
		Name:    name,
		Address: address,
		Port:    port,
		Check: &api.AgentServiceCheck{
			// 每个服务都要提供一个GET接口返回{code:200,success:true}
			HTTP:   func main() {
	// 不能使用 127.0.0.1 因为 Consul 已经部署到 docker 不能识别

	//deregister("mirco-shop-web")
}                        fmt.Sprintf("http://%s:%d/health", address, port),
			Interval:                       "5s", // 定时检查
			Timeout:                        "5s", // 超时时间
			DeregisterCriticalServiceAfter: "1m", // 服务失效多少秒后注销
		},
	}
	err = client.Agent().ServiceRegister(registation)
	if err != nil {
		panic(err)
	}
	fmt.Println("注册成功")
}

func main(){
    // 不能使用 127.0.0.1 因为 Consul 已经部署到 docker 不能识别
    .Register("mirco-shop-api", "mirco-shop-api", "192.168.200.110", 8021)

}

gRPC Python

Protobuf https://github.com/grpc/grpc/blob/master/doc/health-checking.md

Health.py https://grpc.github.io/grpc/python/_modules/grpc_health/v1/health.html

protobuf health.proto

不要修改任何内容

syntax = "proto3";

package grpc.health.v1;

message HealthCheckRequest {
  string service = 1;
}

message HealthCheckResponse {
  enum ServingStatus {
    UNKNOWN = 0;
    SERVING = 1;
    NOT_SERVING = 2;
    SERVICE_UNKNOWN = 3;  // Used only by the Watch method.
  }
  ServingStatus status = 1;
}

service Health {
  rpc Check(HealthCheckRequest) returns (HealthCheckResponse);

  rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
}

生成代码

 python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I.  health.proto
  1. 实现Check和Watch方法 health.py 复制进去即可

https://grpc.github.io/grpc/python/_modules/grpc_health/v1/health.html

  1. 新建_async.py 复制以下内容
# Copyright 2020 The gRPC 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.
"""Reference implementation for health checking in gRPC Python."""

import asyncio
import collections
from typing import MutableMapping
import grpc

from common.grpc_health.v1 import health_pb2 as _health_pb2
from common.grpc_health.v1 import health_pb2_grpc as _health_pb2_grpc


class HealthServicer(_health_pb2_grpc.HealthServicer):
    """An AsyncIO implementation of health checking servicer."""
    _server_status: MutableMapping[
        str, '_health_pb2.HealthCheckResponse.ServingStatus']
    _server_watchers: MutableMapping[str, asyncio.Condition]
    _gracefully_shutting_down: bool

    def __init__(self) -> None:
        self._server_status = {"": _health_pb2.HealthCheckResponse.SERVING}
        self._server_watchers = collections.defaultdict(asyncio.Condition)
        self._gracefully_shutting_down = False

    async def Check(self, request: _health_pb2.HealthCheckRequest,
                    context) -> None:
        status = self._server_status.get(request.service)

        if status is None:
            await context.abort(grpc.StatusCode.NOT_FOUND)
        else:
            return _health_pb2.HealthCheckResponse(status=status)

    async def Watch(self, request: _health_pb2.HealthCheckRequest,
                    context) -> None:
        condition = self._server_watchers[request.service]
        last_status = None
        try:
            async with condition:
                while True:
                    status = self._server_status.get(
                        request.service,
                        _health_pb2.HealthCheckResponse.SERVICE_UNKNOWN)

                    # NOTE(lidiz) If the observed status is the same, it means
                    # there are missing intermediate statuses. It's considered
                    # acceptable since peer only interested in eventual status.
                    if status != last_status:
                        # Responds with current health state
                        await context.write(
                            _health_pb2.HealthCheckResponse(status=status))

                    # Records the last sent status
                    last_status = status

                    # Polling on health state changes
                    await condition.wait()
        finally:
            if request.service in self._server_watchers:
                del self._server_watchers[request.service]

    async def _set(self, service: str,
                   status: _health_pb2.HealthCheckResponse.ServingStatus
                  ) -> None:
        if service in self._server_watchers:
            condition = self._server_watchers.get(service)
            async with condition:
                self._server_status[service] = status
                condition.notify_all()
        else:
            self._server_status[service] = status

    async def set(self, service: str,
                  status: _health_pb2.HealthCheckResponse.ServingStatus
                 ) -> None:
        """Sets the status of a service.

        Args:
          service: string, the name of the service.
          status: HealthCheckResponse.status enum value indicating the status of
            the service
        """
        if self._gracefully_shutting_down:
            return
        else:
            await self._set(service, status)

    async def enter_graceful_shutdown(self) -> None:
        """Permanently sets the status of all services to NOT_SERVING.

        This should be invoked when the server is entering a graceful shutdown
        period. After this method is invoked, future attempts to set the status
        of a service will be ignored.
        """
        if self._gracefully_shutting_down:
            return
        else:
            self._gracefully_shutting_down = True
            for service in self._server_status:
                await self._set(service,
                                _health_pb2.HealthCheckResponse.NOT_SERVING)

注册健康检查服务

请确使用Consul能访问的IP,特别是Consul使用Docker部署

import requests

headers = {
    "Content-Type": "application/json"
}


def register(name, id, address, port):
    url = "http://172.17.0.1:8500/v1/agent/service/register"
    rsp = requests.put(url, headers=headers, json={
        "ID": id,  # ID 如果不设置则和Name保持一致
        "Name": name,
        "Address": address,
        "Port": port,
        "Tags": ["micro-shop", "lzscxb", "web"],  # 标签
        "Check": {  # 健康检查
            "GRPC": f"{address}:{port}",
            "GRPCUseTLS": False,
            "Timeout": "10s",
            "Interval": "10s",
            "DeregisterCriticalServiceAfter": "1m",
        },
    })
    if rsp.status_code != 200:
        print(f"注册失败:{rsp.status_code}")
        print(rsp.text)
        return

    print("注册成功")


if __name__ == "__main__":
    register("micro-shop-srv", "micro-shop-srv", "192.168.200.110", 50051)

image-20220220140820786

注销服务

func Deregister(id string) {
	// 默认配置
	cfg := api.DefaultConfig()
	cfg.Address = "172.17.0.1:8500"

	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	err = client.Agent().ServiceDeregister(id)
	if err != nil {
		panic(err)
	}
	fmt.Println("注销服务成功")
}// 注销服务
func deregister(id string) {
	url := "http://172.17.0.1:8500/v1/agent/service/deregister/" + id
	data := map[string]string{
		"service_id": id,
	}
	dataJson, err := json.Marshal(data)
	if err != nil {
		panic(err)
	}
	body := bytes.NewReader(dataJson)
	rsq, err := http.NewRequest("PUT", url, body)
	if err != nil {
		panic(err)
	}
	defer rsq.Body.Close()
	rsq.Header.Add("Content-Type", "application/json")
	rsp, err := http.DefaultClient.Do(rsq)
	if err != nil {
		panic(err)
	}
	if rsp.StatusCode != 200 {
		fmt.Println("注销失败")
		return
	}
	fmt.Println("注销成功")
}

获取服务

// 获取所有服务
func AllServices() {
   // 默认配置
   cfg := api.DefaultConfig()
   cfg.Address = "172.17.0.1:8500"

   client, err := api.NewClient(cfg)
   if err != nil {
      panic(err)
   }
   services, err := client.Agent().Services()
   if err != nil {
      return
   }
   fmt.Println(services)
}

// 过滤服务名称
func FilterService(name string) {
   // 默认配置
   cfg := api.DefaultConfig()
   cfg.Address = "172.17.0.1:8500"

   client, err := api.NewClient(cfg)
   if err != nil {
      panic(err)
   }
   services, err := client.Agent().ServicesWithFilter(fmt.Sprintf(`Service == "%s"`, name))
   if err != nil {
      return
   }
   fmt.Println(services)
}
posted @ 2022-06-07 15:59  白日醒梦  阅读(828)  评论(0编辑  收藏  举报