.net core使用Apollo做统一配置管理(二)启动apollo服务,及在core项目中使用

4 篇文章 0 订阅
2 篇文章 0 订阅

https://github.com/ctripcorp/apollo/wiki/Quick-Start

按照官方的案例一步步搭建,介绍的非常清楚

在Git bash中运行.sh脚本,相当于部署发布包


Administrator@DESKTOP-U3A77SG MINGW64 /g/BaiduNetdiskDownload/apollo-quick-start/apollo-quick-start-1.8.0
$ ./demo.sh start
Windows new JAVA_HOME is: /c/PROGRA~1/Java/JDK18~1.0_2
==== starting service ====
Service logging file is ./service/apollo-service.log
Started [1667]
Waiting for config service startup......
Config service started. You may visit http://localhost:8080 for service status now!
Waiting for admin service startup..
Admin service started
==== starting portal ====
Portal logging file is ./portal/apollo-portal.log
Started [1727]
Waiting for portal startup.....
Portal started. You can visit http://localhost:8070 now!

Administrator@DESKTOP-U3A77SG MINGW64 /g/BaiduNetdiskDownload/apollo-quick-start/apollo-quick-start-1.8.0
$ ./demo.sh client
Windows new JAVA_HOME is: /c/PROGRA~1/Java/JDK18~1.0_2
[apollo-demo][main]2021-02-18 16:58:05,100 INFO  [com.ctrip.framework.foundation.internals.provider.DefaultApplicationProvider] App ID is set to SampleApp by app.id property from /META-INF/app.properties
[apollo-demo][main]2021-02-18 16:58:05,103 INFO  [com.ctrip.framework.foundation.internals.provider.DefaultServerProvider] Environment is set to [dev] by JVM system property 'env'.
[apollo-demo][main]2021-02-18 16:58:05,162 INFO  [com.ctrip.framework.apollo.internals.DefaultMetaServerProvider] Located meta services from apollo.meta configuration: http://localhost:8080!
[apollo-demo][main]2021-02-18 16:58:05,163 INFO  [com.ctrip.framework.apollo.core.MetaDomainConsts] Located meta server address http://localhost:8080 for env DEV from com.ctrip.framework.apollo.internals.DefaultMetaServerProvider
Apollo Config Demo. Please input key to get the value. Input quit to exit.
> timeout
Loading key : timeout with value: 100
> Changes for namespace application
Change - key: timeout, oldValue: 100, newValue: 1000, changeType: MODIFIED

> test1
Loading key : [Atest1 with value: undefined
> test2
Loading key : test2 with value: undefined
Loading key : [A with value: undefined
> test1
Loading key : test1 with value: undefined

打开配置中心界面:可以新建项目ApolloDemo,新建配置后发布如下:

在core项目中使用:

1.安装相关包,

PM> Install-Package Com.Ctrip.Framework.Apollo.Configuration

2.appsettings.json文件中添加

 "Apollo": {
    "AppId": "xw19900219",
    "Env": "DEV",
    "MetaServer": "http://localhost:8080",
    "ConfigServer": [ "http://localhost:8080" ]
  }, 

3.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ApolloDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        //[HttpGet]
        //public ActionResult<IEnumerable<string>> Get()
        //{
        //    return new string[] { "value1", "value2" };
        //}

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }

        [HttpGet]
        //[Route("Apollo")]
        public IActionResult ApolloTest([FromServices] IConfiguration configuration, string key)
        {
            return Content(configuration.GetValue<string>(key));
        }
    }
}

有图有真相:

修改value后

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值