net6微服务实践 Consul(服务发现+负载均衡)、Ocelot(网关)、Polly(熔断和重试)

本文介绍了如何在net6微服务中整合Consul进行服务发现,Ocelot作为API网关,并利用Polly实现容错策略。详细步骤包括下载安装Consul,为WebApi添加Consul服务注册,安装Ocelot及其相关依赖,配置Ocelot的configuration.json文件,以及在服务注册和网关运行后观察负载均衡的效果。
摘要由CSDN通过智能技术生成

一、下载安装Consul(服务发现)程序

1、官网地址

https://developer.hashicorp.com/consul/install

2、运行服务发现
consul agent -dev

二、服务发现 Consul

1、添加Consul Nuget包

在这里插入图片描述

2、新建WebApi,然后注册服务
/// <summary>
    /// 自己封装的注册类
    /// </summary>
    public static class ConsulHelper
    {
   
        public static void ConsulRegist(this IConfiguration configuration)
        {
   
            ConsulClient client = new ConsulClient(c =>
            {
   
                c.Address = new Uri("http://localhost:8500/");
                c.Datacenter = "dc1";
            });
            string ip = configuration["IP"];
            int port = int.Parse(configuration["Port"]);//命令行参数必须传入
            int weight = string.IsNullOrWhiteSpace(configuration["weight"]) ? 1 : int.Parse(configuration["weight"]);//命令行参数必须传入
            Console.WriteLine($"健康检查地址: http://{
     ip}:{
     port}/api/Health/Index");
            client.Agent.ServiceRegister(new AgentServiceRegistration()
            {
   
                ID = "service" 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值