前言
Consul由Go语言开发,部署起来非常容易,只需要极少的可执行程序和配置文件,具有绿色、轻量级的特点。Consul
是分布式
的、高可用
的、 可横向扩展
的用于实现分布式系统的服务发现与配置。
特点
Consul
提供了通过DNS或者HTTP接口的方式来注册服务和发现服务。一些外部的服务通过Consul很容易的找到它所依赖的服务。Consul 集群架构图
Consul的配置
安装包在项目的根目录下 /DevelopmentPackage /consul_1.8.0_windows_amd64.zip 解压后的文件 consul.exe
consul agent -dev --简单一行命令启动服务
安装 consul neget 包
1、Consul 1.6.1.1
2、NConsul 0.7.4
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ip": "127.0.0.1",
"port": 5000,
"weight": 1,
"ConsulAddress": "http://localhost:8500/",
"ConsulCenter": "dc1"
}
//服务注册
this.Configuration.ConsulRegist();
using Consul;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Contione.Microservices.Service01.Utility
{
public static class ConsulHelper
{
public static async void ConsulRegist(this IConfiguration configuration)
{
string ip = configuration["ip"];
int port = int.Parse(configuration["port"]);
int weight = string.IsNullOrWhiteSpace(configuration["weight"]) ? 1 : int.Parse(configuration["weight"]);
using (ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri(configuration["ConsulAddress"]);
c.Datacenter = configuration["ConsulCenter"];
}))
{
await client.Agent.ServiceRegister(new AgentServiceRegistration()
{
ID = "service " + ip + ":" + port,
Name = "Contione.Microservices.Consul",
Address = ip,
Port = port,
Tags = new string[] { weight.ToString() },
Check = new AgentServiceCheck()
{
Interval = TimeSpan.FromSeconds(12),
HTTP = $"http://{ip}:{port}/api/health/index",
Timeout = TimeSpan.FromSeconds(5),
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(20)
}
});
Console.WriteLine($"{ip}:{port}--weight:{weight}");
}
}
}
}
docker pull consul:latest
docker run -d --name=node1 --restart=always \
-e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302/udp \
-p 8302:8302 \
-p 8400:8400 \
-p 8500:8500 \
-p 8600:8600 \
-h node1 \
consul agent -server -bind=0.0.0.0 -bootstrap-expect=3 -node=node1 \
-data-dir=/tmp/data-dir -client 0.0.0.0 -ui
docker run -d --name=node2 --restart=always \
-e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \
-p 9300:8300 \
-p 9301:8301 \
-p 9301:8301/udp \
-p 9302:8302/udp \
-p 9302:8302 \
-p 9400:8400 \
-p 9500:8500 \
-p 9600:8600 \
-h node2 \
consul agent -server -bind=0.0.0.0 \
-join=123.57.174.20 -node-id=$(uuidgen | awk '{print tolower($0)}') \
-node=node2 \
-data-dir=/tmp/data-dir -client 0.0.0.0 -ui
docker run -d --name=node3 --restart=always \
-e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \
-p 10300:8300 \
-p 10301:8301 \
-p 10301:8301/udp \
-p 10302:8302/udp \
-p 10302:8302 \
-p 10400:8400 \
-p 10500:8500 \
-p 10600:8600 \
-h node2 \
consul agent -server -bind=0.0.0.0 \
-join=123.57.174.20 -node-id=$(uuidgen | awk '{print tolower($0)}') \
-node=node3 \
-data-dir=/tmp/data-dir -client 0.0.0.0 -ui
dotnet Contione.Microservices.OcelotGateway.dll --urls="http://*:8089" --ip="127.0.0.1" --port=8089
dotnet Contione.Microservices.Service02.dll --urls="http://*:5727" --ip="127.0.0.1" --port=5727 --weight=1
代码地址
版权声明:本文由Contione原创出品,转载请注明出处!
本文主要介绍.net core Consul 服务注册发现 后面会介绍 Ocelot 网关实现 路由、请求聚合、服务发现、认证、鉴权、限流熔断等
10/21/2020 20:54:44回复