部署Apollo
我这里按照官方文档使用docker快速部署了一个测试学习用。
创建应用
新增配置
创建.net8版本的webapi项目
修改appsettings.json
{ "Apollo": { "AppId": "ApolloTest001", "MetaServer": "http://127.0.0.1:8080" } }
- AppId是应用的身份信息,是从服务端获取配置的一个重要信息,上面创建应用时,输入的AppId。
- MetaServer:服务地址
修改Program.cs
//在var builder = WebApplication.CreateBuilder(args);下面继续写 builder.Configuration.AddApollo(builder.Configuration.GetSection("Apollo")).AddDefault();
创建测试控制器,并测试
using Microsoft.AspNetCore.Mvc; namespace DotNet8TestApi.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class TestController : ControllerBase { private readonly IConfiguration _configuration; public TestController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public string GetStr() { var a =_configuration.GetValue<string>("PostgreSQLConnStr"); return a; } } }
打断点发现,可以正常获取到配置信息。
appsettings.json写法2
{ "Apollo": { "AppId": "ApolloTest001", "Env": "DEV", "Meta": { "DEV": "http://127.0.0.1:8080/", "FAT": "http://127.0.0.1:8080/", "UAT": "http://127.0.0.1:8080/", "PRO": "http://127.0.0.1:8080/" } } }
参考连接
https://github.com/apolloconfig/apollo.net/blob/main/src/Apollo.Configuration/README.md
https://www.apolloconfig.com/#/zh/client/dotnet-sdk-user-guide
标签:集成,127.0,http,0.1,configuration,net8,8080,Apollo From: https://www.cnblogs.com/LTQ151/p/18059664