1、开场白mysql
在系统设计里咱们有不少配置但愿独立于系统以外,而又可以被系统实时读取。可是在传统的系统设计里,配置信息一般是耦合在系统内的,好比.net里一般会放在App.config或者web.config里,.net core则是appsettings.json里,这些都不够灵活,若是在制度严格,不容许随便登陆服务器的中大型公司里,每次的配置更改就意味着系统的发布,毫无疑问,确定有带哥要吐槽了,什么垃圾架构!为了解决这一问题,Apollo应运而生,专门用来构建微服务架构里的配置中心,在实际生产项目里为了达到高可用,咱们会将其以分布式的方式部署。git
粘贴下官方的下载和文档:github
官网:https://github.com/ctripcorp/apolloweb
Wiki:https://github.com/ctripcorp/apollo/wiki(一切的集成方式和使用方法都在这里)sql
Issues:https://github.com/ctripcorp/apollo/issues(若是期间有任何问题,请经过这里查找大部分解决方法)docker
2、Maven编译源码数据库
首先要说明一下,官方提供的Quick Start以及预先打好的安装包若是不进行特殊配置都只能单机搭建使用,经过因此若是你想把Apollo搭建在公有云或者调用放和部署不在同一环境,最好本身编译项目。apache
我这里使用阿里云搭建Apollo,系统是CentOs 7。json
1.首先咱们须要在如下地址下载源码:bootstrap
https://github.com/ctripcorp/apollo/releases
2. 源码下载完成后咱们须要修改两个地方,Apoolo是微服务架构,使用Eureka实现服务的注册和发现,分布式部署的时候,apollo-configservice
和apollo-adminservice
须要把本身的IP和端口注册到MetaServer(apollo-configservice自己)Server(apollo-configservice自己)。因此若是实际部署的机器有多块网卡(如docker),或者存在某些网卡的IP是Apollo客户端和Portal没法访问的(如网络安全限制),那么咱们就须要在apollo-configservice
和apollo-adminservice
中作相关限制以免Eureka将这些网卡的IP注册到Meta Server。
我这里的解决方式是直接指定IP。经过修改apollo-adminservice或apollo-configservice 的bootstrap.yml文件,指定apollo-configservice和apollo-adminservice的IP端口。
解压源码文件,经过 apollo-1.3.0\apollo-adminservice\src\main\resources 找到 bootstrap.yml 文件,添加如下配置:
eureka: instance: ip-address: xx.xx.xx.xx hostname: ${hostname:47.99.92.76} preferIpAddress: true status-page-url-path: /info health-check-url-path: /health client: serviceUrl: # This setting will be overridden by eureka.service.url setting from ApolloConfigDB.ServerConfig or System Property # see com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfig defaultZone: http://${eureka.instance.hostname}:8080/eureka/ healthcheck: enabled: true eurekaServiceUrlPollIntervalSeconds: 60 management: health: status: order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
ip-address是我阿里云的公有IP,其它部署环境同理。
经过 apollo-1.3.0\apollo-configservice\src\main\resources 找到 bootstrap.yml 文件,配置同上。
同时咱们还须要配置下MySQL数据库的连接信息,经过 apollo-1.3.0\scripts 找到 build.sh 文件,编辑连接信息:
# apollo config db info apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 apollo_config_db_username=xxx apollo_config_db_password=xxx # apollo portal db info apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8 apollo_portal_db_username=xxx apollo_portal_db_password=xxx
由于个人MySQL就是部署在同一个阿里云上,因此就使用localhost,你们酌情修改。
Apollo依赖于MySQL,咱们须要先把其对应的数据库跑出来,经过如下路径加载两个sql文件:
Configdb:apollo-1.3.0\scripts\db\migration\configdb
Portaldb:apollo-1.3.0\scripts\db\migration\portaldb
配置就到这里,接下来咱们要安装Maven环境来编译源码:
1.安装maven
Windows环境经过本文安装:http://www.javashuo.com/article/p-atxqppei-nb.html
二、解压安装
tar -zxvf apache-maven-3.3.9-bin.tar.gz
mv apache-maven-3.3.9 /usr/local/maven-3.3.9
三、配置环境变量
vi /etc/profile
#在适当的位置添加
export M2_HOME=/usr/local/maven3 (这里须要制定你的安装目录 自定义的哈)
export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
四、使配置生效
保存退出后运行下面的命令使配置生效,或者重启服务器生效。
source /etc/profile
五、验证版本
mvn -v
6.配置阿里云仓库,国内速度快
在maven的settings.xml 文件里配置mirrors的子节点,添加以下mirror:
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
7.阿里云cd到源码文件夹路径 apollo-1.3.0\scripts ,执行如下命令编译源码:
./build.sh
该脚本会依次打包apollo-configservice, apollo-adminservice, apollo-portal。
8.编译完成后
获取位于apollo-configservice/target/
目录下的apollo-configservice-x.x.x-github.zip。解压后执行scripts/startup.sh便可。如需中止服务,执行scripts/shutdown.sh.
获取位于apollo-adminservice/target/
目录下的apollo-adminservice-x.x.x-github.zip。解压后执行scripts/startup.sh便可。如需中止服务,执行scripts/shutdown.sh.
获取位于apollo-portal/target/
目录下的apollo-portal-x.x.x-github.zip。解压后执行scripts/startup.sh便可。如需中止服务,执行scripts/shutdown.sh.
OK,上面3个启动后,咱们就能够经过Ip:8070 访问Apollo的UI界面的,经过默认的 帐户密码登陆:apollo/admin
经过IP:8080访问Eureka的UI界面查看被注册的configure和admin两个进程。
2、新建.Net Core API项目试用
1.新建.net core api项目,经过nuget引入项目包:Com.Ctrip.Framework.Apollo.Configuration。
2.在Program.cs中添加以下代码:
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, builder) => { builder .AddApollo(builder.Build().GetSection("apollo")) .AddDefault(); }) .UseStartup<Startup>(); }
3.appsettings.json中进行以下配置,咱们这里试用默认自带的SimpleApp测试:
{ "apollo": { "AppId": "SampleApp", "MetaServer": "http://47.99.92.76:8080", "Env": "Dev" } }
这里配置的意思是客户端须要经过Eureka查询Configure服务的地址,从而获取配置信息。
private IConfiguration _configuration; public ValuesController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public ActionResult<IEnumerable<string>> Get() { string title = _configuration["timeout"]; return new string[] { "value1", "value2", title }; }
运行项目查看:
OK,运行成功,更多信息你们能够去官方gitHub查看哦,目前博主所在的公司已经使用Apollo好久了,并且.net 端一个分支的维护者仍是博主公司架构组的一位同事,就是下面这个:
看下使用Apollo后的项目运行图,服务配置数据能够正常请求:
今天就到这了,12点睡觉了!!