ASP .net core 整合 nacos 通过Spring Cloud Gateway 网关访问

ASP .net core 整合 nacos 通过Spring Cloud Gateway 网关访问

  1. 使用vs创建web项目在这里插入图片描述
  2. 选择api 注意这里要取消掉Https配置否则使用网关转发也需要配置为https请求这里我们直接取消
  3. 添加nacos支持依赖包nacos-sdk-csharp-unofficial.AspNetCore
    工具——NuGet包管理器——管理解决方法的NuGet程序包
    注:版本选用0.2.6(.NET Core版本为3.1),最新版本0.2.7似乎不兼容无法通过网关调用仅仅只能注册,不知道是不是配置问题
    在这里插入图片描述
  4. 修改appsetting.json 配置nacos连接
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "nacos": {
    "ServerAddresses": [ "localhost:8848" ],
    "DefaultTimeOut": 15000,
    "Namespace": "",
    "ListenInterval": 1000,
    "ServiceName": "net-test"
  }
  1. 修改启动文件Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace nacose
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            //navos
            services.AddNacosAspNetCore(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
              //navos
            app.UseNacosAspNetCore();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

  1. 启动nacos后启动服务 注册到nacos
    在这里插入图片描述
    启动成功
    检查是否注册成功
    在这里插入图片描述
    注册成功

  2. 启动gateway网关并通过注册服务名访问.net服务
    在这里插入图片描述

在这里插入图片描述
通过gateway请求成功
完成!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值