54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
using JNPF.DependencyInjection;
|
|
using JNPF.DynamicApiController;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Tnb.BasicData;
|
|
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
// public class ValuesController : ControllerBase
|
|
public class ValuesController : IDynamicApiController, ITransient
|
|
{
|
|
private readonly Nacos.V2.INacosNamingService _nacosNamingService;
|
|
|
|
public ValuesController(Nacos.V2.INacosNamingService nacosNamingService)
|
|
{
|
|
_nacosNamingService = nacosNamingService;
|
|
}
|
|
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public string Get()
|
|
{
|
|
return "Ok~" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
|
|
}
|
|
|
|
|
|
[HttpGet("test")]
|
|
[AllowAnonymous]
|
|
public async Task<string> Test()
|
|
{
|
|
// 被调用方的服务名称
|
|
var instance = await _nacosNamingService.SelectOneHealthyInstance("tnb-core", "DEFAULT_GROUP");
|
|
var host = $"{instance.Ip}:{instance.Port}";
|
|
|
|
var baseUrl = instance.Metadata.TryGetValue("secure", out _)
|
|
? $"https://{host}"
|
|
: $"http://{host}";
|
|
|
|
if(string.IsNullOrWhiteSpace(baseUrl))
|
|
{
|
|
return "empty";
|
|
}
|
|
|
|
var url = $"{baseUrl}/api/values";
|
|
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
var result = await client.GetAsync(url);
|
|
return await result.Content.ReadAsStringAsync();
|
|
}
|
|
}
|
|
|
|
} |