您的位置:首页 > 编程语言 > Java开发

maven的Springboot项目的携程Apollo配置中心的配置以及使用

2018-01-12 15:52 711 查看
说明:转发,复制,必须标明文章的出处,标注原地址以及作者

http://mp.blog.csdn.net/postedit/79044988  作者:暮辰邪


使用配置中心注入application.properties

1、Client等jar包的制作

(1)Apollo
github下载地址:https://github.com/Lliangwenbo/apollo.git

Apollo文件如下图所示:



(2)其中最重要的文件就是scripts,如下图:



sql文件是两个mysql类型数据库文件,需要建立两个类型数据库,数据库名字以sql文件名命名,用户名密码都为admin,

修改build.sh文件中的数据库信息如下:

下面这几个的地址都为相同地址,其实只需要一个就行:dev_meta=http://localhost:8080

此地址就是Apollo Client(即客户的)部署的地址。dev_meta代表开发,fat_meta,uat_meta代表测试, pro_meta代表生产。

# apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/apolloconfigdb?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=root

# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/apolloportaldb?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=root

# meta server url, different environments should have different meta server addresses
dev_meta=http://localhost:8080
fat_meta=http://someIp:8080
uat_meta=http://anotherIp:8080
pro_meta=http://yetAnotherIp:8080


配置好之后用git的Git Base Here启动:在scripts目录右键选择:Git Base Here输入:

sh build.sh

自动在maven本地仓库中生成apollo的jar包

如果报错可能是maven的环境变量没有配置。windows系统下打开配置环境变量

MAVEN_HOME=E:\maven3.3.9

path中添加%MAVEN_HOME%\bin

Ctrl+R 输入cmd打开窗口输入mvn测试maven环境变量配置是否成功!

重新启动:sh build.sh

启动成功!!!

2、Cilent端 快速启动插件的下载

百度云下载地址:

github下载地址:https://github.com/Lliangwenbo/apollo-build-scripts.git

文件如下图所示






demo.sh中指定两个数据库地址,刚才在第一步中已经详细描述过,如下图所示:



接下来在Windows系统建立:C:\opt\settings\server.properties文件

如果是Linux系统,则创建/opt/settings/server.properties

文件内容:env=dev 

即指定开发环境,同第一步选择的有关!!!!!!!千万不要忘了!!!!

配置好之后用git的Git Base Here启动:在scripts目录<
b65b
/span>右键选择:Git
Base Here输入:

sh ./demo.sh start

启动成功!!!

3、项目配置:

(1),pom.xml 加入由第一步生成的jar包,记得版本和第一步生成的jar包保持一致,如下:

<!-- apollo 携程apollo配置中心框架 -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>0.9.1</version>
</dependency>
<!-- apollo -->
(2)添加app.properties文件

目录如下:



内容仅添加:app.id=apollodemo



(3)在springboot启动类上方加入两行注解:@Configuration,@EnableApolloConfig。

@EnableApolloConfig是依赖@Configuration注解的

package com.feeling.apollo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.feeling.apollo.util.TestJavaConfigBean;

@Configuration
@EnableApolloConfig
@ComponentScan(basePackages = { "com.feeling.*" }) // 将该包下的文件纳入容器中
@EnableAutoConfiguration
public class ApolloDemo {

public static void main(String[] args) throws Exception {
SpringApplication.run(ApolloDemo.class, args);
}
}

4、client配置中心配置文件

打开http://localhost:8070/

用户名默认:apollo

密码:admin



点击创建项目,



应用id项目中写第三部中创建的app.properties文件中的app.id的值!!!其它随便填.........






创建成果够进入新创建的项目中:把项目中的application.properties去掉备注和空行复制到文本中



然后点击发布!!!

然后把项目中的application.properties文件删掉

正常启动!!!

5、最后补充内容:项目监听配置中心的修改

(1)创建TestJavaConfigBean实体类监听

package com.feeling.apollo.util;

import org.springframework.beans.factory.annotation.Value;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;

public class TestJavaConfigBean {
@ApolloConfig("application")
private Config config; //inject config for namespace application

@Value("${test:test}")//如果配置中心没有值,默认key为test的value值为test
private String name;

//config change listener for namespace application
@ApolloConfigChangeListener("application")
private void anotherOnChange(ConfigChangeEvent changeEvent) {

ConfigChange change = changeEvent.getChange("test");
System.out.println(String.format("Found change - key: %s, oldValue: %s,"
+ " newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}

}
(2)在springboot启动类中加入红色部分
package com.feeling.apollo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.feeling.apollo.util.TestJavaConfigBean;

@Configuration
@EnableApolloConfig
@ComponentScan(basePackages = { "com.feeling.*" }) // 将该包下的文件纳入容器中
@EnableAutoConfiguration
public class ApolloDemo {

public static void main(String[] args) throws Exception {
SpringApplication.run(ApolloDemo.class, args);
}
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
}


(3)在配置中心给项目新增配置





然后1保存、2发布

再次启动项目

(4)进入配置中心修改刚刚的test配置

value改成

this id a test223232


然后可以看到eclipse中输出:



这种配置方式能不修改一些普通的key和value值,像springboot启动时就加载的类似数据库端口号的一些参数是不能再不停机的情况下使用的。

普通的配置可以在不停机的情况下修改!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐