5

ABP微服务系列学习-对接Apollo配置中心

 1 year ago
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.
neoserver,ios ssh client

前面我们把服务都已经成功启动,并且对接前端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可以看到界面

image.png

浏览器打开http://localhost:8070或者http://127.0.0.1:8070可以看到管理界面

image.png

添加配置#

在管理界面输入账号apollo,密码admin即可进入管理后台。
然后我们根据服务创建应用,每一个服务对应一个应用

image.png


这里按照服务创建了5个应用

image.png

删除默认Namespcae#

点击应用名称进去应用管理,先删除默认自带的Namespace application,因为默认的Namespace是properties类型,不符合我们JSON的配置。虽然能读取,但是识别内容都是字符串。

image.png

创建新的Namespace#

在应用管理界面左下角,可以看到添加Namespace的按钮,点击添加Namespace。

image.png


然后输入我们的配置名称,以及配置类型,配置类型选择JSON

image.png


创建完成后我们返回应用管理页面修改配置,把我们服务中appsettings.json文件内容复制到配置里面。

image.png


网关服务的配置,多个配置文件就添加多个Namespace

image.png


这里每一个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脚本,可以看到服务全部正常启动。

image.png


image.png


这里就不上那么多图片了,稍微放两个图就可以了。
好了,到这我们就成功接入apollo配置中心了。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK