Springboot项目的携程Apollo配置中心

使用配置中心注入application.properties

什么是配置中心?为什么要用到配置中心?

配置中心是修改项目中的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
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目录右键选择:Git Base Here输入:

sh ./demo.sh start

启动成功!!!

3、项目配置:

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

com.ctrip.framework.apollo apollo-client 0.9.1 com.ctrip.framework.apollo apollo-core 0.9.1 (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();
}	

}
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
}

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

然后1保存、2发布

再次启动项目

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

value改成

this id a test223232

然后可以看到eclipse中输出:

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

普通的配置可以在不停机的情况下修改!!!
————————————————
版权声明:本文为CSDN博主「暮辰邪」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_37623470/article/details/79044988

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值