分布式配置中心Apollo 客户端集成springboot

1引入maven依赖

Apollo的客户端jar包已经上传到中央仓库,应用在实际使用时只需要按照如下方式引入即可

 <dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>1.1.0</version>
    </dependency>

2配置

Apollo客户端依赖于AppIdApollo Meta Server等环境信息来工作

(1)AppId

AppId是应用的身份信息,是从服务端获取配置的一个重要信息

方法1:Spring Boot 配置文件application.properties

Apollo 1.0.0+支持通过Spring Boot的application.properties文件配置,如

app.id=YOUR-APP-ID

该配置方式不适用于多个war包部署在同一个tomcat的使用场景

方法2:使用app.properties

确保classpath:/META-INF/app.properties文件存在,并且其中内容形如:

app.id=YOUR-APP-ID

文件位置参考如下:

app-id-location

注:app.id是用来标识应用身份的唯一id,格式为string。

(2)Apollo Meta Server

Apollo支持应用在不同的环境有不同的配置,所以需要在运行提供给Apollo客户端当前环境的Apollo Meta Server信息。默认情况下,meta server和config service是部署在同一个JVM进程,所以meta server的地址就是config service的地址。

为了实现meta server的高可用,推荐通过SLB(Software Load Balancer)做动态负载均衡。Meta server地址也可以填入IP,如http://1.1.1.1:8080,http://2.2.2.2:8080,不过生产环境还是建议使用域名(走slb),因为机器扩容、缩容等都可能导致IP列表的变化。

方法1:通过Spring Boot的配置文件

可以在Spring Boot的application.propertiesbootstrap.properties中指定apollo.meta=http://config-service-url

方法2:通过server.properties配置文件

可以在server.properties配置文件中指定apollo.meta=http://config-service-url

对于Mac/Linux,文件位置为/opt/settings/server.properties

对于Windows,文件位置为C:\opt\settings\server.properties

方法3:通过app.properties配置文件

可以在classpath:/META-INF/app.properties指定apollo.meta=http://config-service-url

方法4:通过apollo-env.properties文件

用户也可以创建一个apollo-env.properties,放在程序的classpath下,或者放在spring boot应用的config目录下

配置内容为

dev.meta=http://1.1.1.1:8080
fat.meta=http://apollo.fat.xxx.com
uat.meta=http://apollo.uat.xxx.com
pro.meta=http://apollo.xxx.com

(3)本地缓存路径

Apollo客户端会把从服务端获取到的配置在本地文件系统缓存一份,用于在遇到服务不可用,或网络不通的时候,依然能从本地恢复配置,不影响应用正常运行。

本地缓存路径默认位于以下路径,所以请确保/opt/dataC:\opt\data\目录存在,且应用有读写权限。

可选设置

Environment

通过配置文件

  • 对于Mac/Linux,文件位置为/opt/settings/server.properties
  • 对于Windows,文件位置为C:\opt\settings\server.properties

内容形式

env=DEV

具体demo代码 集成springboot

pom依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xuxu</groupId>
	<artifactId>apollo_test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- apollo -->
		<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-client</artifactId>
			<version>1.1.0</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

配置文件

app.id=test_app

local.meta=http://xx.xx.xx.xx:8080
dev.meta=http://xx.xx.xx.xx:8080

server:
  port: 8055

启动类上加上@EnableApolloConfig注解

@SpringBootApplication
@EnableApolloConfig
public class ApolloClient {
	public static void main(String[] args) {
		SpringApplication.run(ApolloClient.class, args);
	}
}

测试请求

@RestController
public class TestController {
	@Value("${config}")
	private String config;
	
	@RequestMapping("/test")
	public String test(){
		return config;
	}
}

其中config将从配置中心获取

 

访问配置中心配置

访问http://xxx.xxx.xxx.xxx:8070/

首页中选择你appid对应的项目

可以看到现在已经有对应的key 和value

访问测试服务和配置中心中值一致

修改值为test_config 确定并发布

再次访问测试地址

如果发现修改错误可以点击回滚

点击回滚并确定后会立即回滚到上一次的配置

同时刚刚设置的新值会变为未发布状态

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值