ABP微服务系列学习-对接Apollo配置中心
source link: https://www.cnblogs.com/fanshaoO/p/17192180.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
前面我们把服务都已经成功启动,并且对接前端Angular界面。
但是在微服务结构中,多个服务意味着需要配置多个配置文件,这时就需要引入配置中心这玩意了。
配置中心有很多现成的方案,比如携程的阿波罗,K8S自带的ConfigMap等等。
这里介绍一下如何对接携程的阿波罗配置中心。
部署Apollo服务#
在开发环境,我们可以使用Docker一键搭建我们Apollo服务。
安装部署过程直接看官方文档https://www.apolloconfig.com/#/zh/deployment/quick-start-docker
这里就不赘述了。
使用docker-compsoe up启动服务后看到日志说明启动成功。
apollo-quick-start | Started [66]
apollo-quick-start | Waiting for config service startup...
apollo-quick-start | Config service started. You may visit http://localhost:8080 for service status now!
apollo-quick-start | Waiting for admin service startup.
apollo-quick-start | Admin service started
apollo-quick-start | ==== starting portal ====
apollo-quick-start | Portal logging file is ./portal/apollo-portal.log
apollo-quick-start | Application is running as root (UID 0). This is considered insecure.
apollo-quick-start | Started [247]
apollo-quick-start | Waiting for portal startup...
apollo-quick-start | Portal started. You can visit http://localhost:8070 now!
浏览器打开http://localhost:8080或者http://127.0.0.1:8080可以看到界面
浏览器打开http://localhost:8070或者http://127.0.0.1:8070可以看到管理界面
添加配置#
在管理界面输入账号apollo,密码admin即可进入管理后台。
然后我们根据服务创建应用,每一个服务对应一个应用
删除默认Namespcae#
点击应用名称进去应用管理,先删除默认自带的Namespace application,因为默认的Namespace是properties类型,不符合我们JSON的配置。虽然能读取,但是识别内容都是字符串。
创建新的Namespace#
在应用管理界面左下角,可以看到添加Namespace的按钮,点击添加Namespace。
创建完成后我们返回应用管理页面修改配置,把我们服务中appsettings.json文件内容复制到配置里面。
这里每一个Namespace就相当于一个文件的概念。
更详细的说明可以看官网文档介绍 https://www.apolloconfig.com/#/zh/design/apollo-core-concept-namespace?id=_1-%e4%bb%80%e4%b9%88%e6%98%afnamespace
发布配置#
修改配置内容后,一定要点击发布,不然配置不会生效。
微服务对接Apollo配置中心#
安装nuget#
Apollo官网有.net的sdk,nuget包名字是Com.Ctrip.Framework.Apollo.Configuration。
在微服务shared模块的FunShow.Shared.Hosting.AspNetCore项目中添加Com.Ctrip.Framework.Apollo.Configuration引用。
配置#
然后封装一下IHostBuilder的扩展方法
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace FunShow.Shared.Hosting.AspNetCore
{
public static class ApolloConfigurationExtension
{
public static IHostBuilder UseApollo(this IHostBuilder builder)
{
return builder
.ConfigureAppConfiguration((config) =>
{
config.AddApollo(config.Build().GetSection("apollo"));
});
}
}
}
然后在每个服务的Program代码中的HostBuilder添加UseApollo方法
var builder = WebApplication.CreateBuilder(args);
builder.Host
.AddAppSettingsSecretsJson()
.UseAutofac()
.UseApollo() //添加Apollo配置中心
.UseSerilog();
最后修改每个服务的appsettings.json文件,只需要配置apollo的参数,比如:
{
"apollo": {
"AppId": "FunShowWebGateway",
"ConfigServer": [ "http://localhost:8080" ],
"Namespaces": [ "appsettings.json", "yarp.json" ]
}
}
这里AppId指的是我们创建应用时指定的AppId。
ConfigServer是请求配置中心的服务地址,注意不是8070端口,8070是管理后台的端口。
Namespaces则是指明我们需要加载的配置文件名称。
为什么不是appsettings而是appsettings.json呢,我们上面创建的Namespaces明明只是写了名字是appsettings。
因为这里的.json是向底层说明我们这个文件用json格式解析,如果不加.json后缀,则默认使用properties格式解析内容,官方默认支持json和xml后缀的文件解析。别的后缀可以自行扩展。
启动服务#
所有配置都发布以及我们的服务代码都修改完成后,我们就可以尝试启动服务啦~
执行我们的run-tye.ps1脚本,可以看到服务全部正常启动。
这里就不上那么多图片了,稍微放两个图就可以了。
好了,到这我们就成功接入apollo配置中心了。
Recommend
-
68
一、介绍 Apollo(阿波罗)[参考附录]是携程框架部研发并开源的一款生产级的配置中心产品,它能够集中管理应用在不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性...
-
63
利用apollo的api方式测试apollo集群是运维的首选方式,java方式的测试只是spring接入的一种,可以参考官方文档继续研究使用
-
84
-
39
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应...
-
41
通过上篇文章分布式部署 的介绍 ,接下来就可以在程序中接入 Apollo 客户端进行调用,这边文章主要介绍在 Java 和 .NET Core 中如何接入,更多可参考
-
31
通过上篇文章 分布式部署 的介绍 ,接下来就可以在程序中接入 Apollo 客户端进行调用,这边文章主要介绍在 Java 和...
-
11
原文首发地址:不问不要紧,一问要人命,绝对的面试加分项--配置中心Apollo深度解读 为什么要使用配置中心 随着程序功能的日益复杂,程序的配置日益增多...
-
3
前面有写过一篇《分布式配置中心apollo是如何实时感知配置被修改》,也就是客户端client是如何知道配置被修改了,有不少读者私信我你既然说了client端是如何感知的,那服务端又是如何知道配置...
-
4
前面我们把后端的微服务架子基本搭建完成并成功启动了,现在我们可以对接前端界面了。这里我们直接用ABP模板里面的Angular的前端界面。 创建应用程序模板
-
5
原创文章,转载请标注。https://www.cnblogs.com/boycelee/p/17993697 《码头工人的一千零一夜》是一位专注于技术干货分享的博主,追随博主的文章,你将深入了解业界最新的技术趋...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK