apollo 站点部署C#接入

下载Apollo
https://www.apolloconfig.com/#/zh/deployment/quick-start
安装文档操作步骤来

先安装Java环境 https://www.java.com/zh-CN/

安装成功 验证 安装成功会有版本信息

java-version

MySQL
如果使用 H2 内存数据库/H2 文件数据库,则无需 MySQL,可以跳过此步骤
https://dev.mysql.com/downloads/mysql/
安装mysql
安装好后 测试是否可以连接
找到apollo 的这个两个文件 执行这个两个sql

注意事项
apollo 服务端进程需要分别使用8070, 8080, 8090端口,请确保这3个端口当前没有被使用。

安装NSSM(Non-Sucking Service Manager)把 java 注册成系统服务,支持崩溃自动重启、开机自启:
注册 Apollo 为 Windows 服务

安装地址\nssm.exeinstallApollo

在弹出的窗口里填写:

#查看java地址where.exejava

下载Quick Start安装包
https://github.com/apolloconfig/apollo-quick-start
下载好的文档 解压 放入指定的路径

注意 NSSM 要加 .\ 前缀(你现在在 nssm 的 win64 目录里):

cd"xxxx\nssm.exeinstallApollo

Path:你实际的 java 路径\bin\java.ex
Startup directory:apollo jar 所在目录 如:D:\Program Files (x86)\apollo
Arguments:填入以下一整串(一行):

cd"D:\Program Files (x86)\nssm-2.24-101-g897c7ad\win64".\nssm.exeinstallApollo"C:\你的java路径\java.exe"'-Dspring.profiles.active=github,database-discovery,auth -Dspring.h2.console.enabled=false "-Dspring.config.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai" "-Dspring.config.datasource.username=root" "-Dspring.config.datasource.password=你的密码" "-Dspring.portal.datasource.url=jdbc:mysql://localhost:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai" "-Dspring.portal.datasource.username=root" "-Dspring.portal.datasource.password=你的密码" -jar D:\apollo\apollo-all-in-one.jar'.\nssm.exesetApollo AppDirectory D:\apollo .\nssm.exesetApollo AppStdout D:\apollo\apollo.log .\nssm.exesetApollo AppStderr D:\apollo\apollo.log Start-Service Apollo Start-Sleep60Get-Service Apollo

浏览器访问 http://localhost:8070,apollo / admin 登录。想确认数据确实在 MySQL 里:

&"mysql安装地址\bin\mysql.exe"-uroot-p-e"USE ApolloPortalDB; SELECT username FROM Users;"


创建


**

创建c# 项目 net10

**
安装Com.Ctrip.Framework.Apollo.Configuration
Program.cs

using Com.Ctrip.Framework.Apollo;var builder=WebApplication.CreateBuilder(args);// Add services to the container. //添加apollo builder.Configuration.AddApollo(builder.Configuration.GetSection("apollo"));builder.Services.AddControllers();// Learnmoreabout configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi();var app=builder.Build();// Configure the HTTP request pipeline.if(app.Environment.IsDevelopment()){app.MapOpenApi();}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();app.Run();

WeatherForecastController

using Microsoft.AspNetCore.Mvc;namespace WebApplication1.Controllers{[ApiController][Route("[controller]")]#[Route("api/[controller]")]public class WeatherForecastController:ControllerBase{privatereadonlyIConfiguration _config;public WeatherForecastController(IConfiguration config){_config=config;}private staticreadonlystring[]Summaries=["Freezing","Bracing","Chilly","Cool","Mild","Warm","Balmy","Hot","Sweltering","Scorching"];//[HttpGet(Name="GetWeatherForecast")]// GetWeatherForecast[HttpGet("GetWeatherForecast")]// WeatherForecast/GetWeatherForecast public IEnumerable<WeatherForecast>Get(){var AIConfigjson=_config.GetValue<string>("AIConfig");var IsShare=_config.GetValue<bool>("IsShare");returnEnumerable.Range(1,5).Select(index=>new WeatherForecast{Date=DateOnly.FromDateTime(DateTime.Now.AddDays(index)), TemperatureC=Random.Shared.Next(-20,55), Summary=Summaries[Random.Shared.Next(Summaries.Length)]}).ToArray();}}}