57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using Nacos.V2;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace MicoService.Demo.Controllers
|
||
{
|
||
[ApiController]
|
||
[Route("[controller]")]
|
||
public class TestController : ControllerBase
|
||
{
|
||
|
||
private readonly IConfiguration _configuration;
|
||
private readonly INacosNamingService _nacosNamingService;
|
||
|
||
public TestController(IConfiguration configuration,
|
||
INacosNamingService nacosNamingService)
|
||
{
|
||
this._configuration = configuration;
|
||
this._nacosNamingService = nacosNamingService;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 演示:读取 Nacos 配置中心配置
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("config/{key}")]
|
||
public string GetConfig(string key)
|
||
{
|
||
return _configuration[key];
|
||
}
|
||
|
||
/// <summary>
|
||
/// 演示:调用其他微服务
|
||
/// </summary>
|
||
[HttpGet("call")]
|
||
public async Task CallOtherService()
|
||
{
|
||
// 核心API:获取单个实例(内置负载均衡)
|
||
var instance = await _nacosNamingService.SelectOneHealthyInstance("Mico_Demo2222");
|
||
|
||
if (instance == null)
|
||
{
|
||
Console.WriteLine("无可用实例");
|
||
}
|
||
|
||
// 拼接调用地址,如:http://192.168.2.10:8080/api/xxx
|
||
var callUrl = $"http://{instance.Ip}:{instance.Port}/user/config/info";
|
||
|
||
}
|
||
|
||
|
||
}
|
||
}
|