ASP .net core 整合 nacos 通过Spring Cloud Gateway 网关访问
- 使用vs创建web项目
- 选择api 注意这里要取消掉Https配置否则使用网关转发也需要配置为https请求这里我们直接取消
- 添加nacos支持依赖包nacos-sdk-csharp-unofficial.AspNetCore
工具——NuGet包管理器——管理解决方法的NuGet程序包
注:版本选用0.2.6(.NET Core版本为3.1),最新版本0.2.7似乎不兼容无法通过网关调用仅仅只能注册,不知道是不是配置问题
- 修改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"
}
- 修改启动文件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();
});
}
}
}
-
启动nacos后启动服务 注册到nacos
启动成功
检查是否注册成功
注册成功 -
启动gateway网关并通过注册服务名访问.net服务
通过gateway请求成功
完成!!!