Mico.Demo/MicoService.Demo/Controllers/TestController.cs

57 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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";
}
}
}