appollo介绍
1 提供哪些功能
简单说提供了配置管理功能
配置之外提供
- 按照集群,机器,环境,命名空间推送数据
- 权限管理
- 高可用
- 实时生效
- 版本管理
- 提供java和.net客户端
官方地址: https://www.apolloconfig.com/#/zh/README
安装
- 下载 git clone https://github.com/apolloconfig/apollo-quick-start.git
- 本地构建数据库
- sh demo.sh start
- 访问 http://localhost:8070/signin
2 整体架构:见官网
3 使用方式
引入依赖
代码语言:txt复制 <!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-client -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${apollo-version}</version>
</dependency>
代码语言:txt复制spring.application.name=apollo-simple
server.port=7180
app.id=1
apollo.cache-dir=/tmp/data
apollo.meta=http://localhost:8080
apollo.bootstrap.enabled=true
apollo.bootstrap.namespaces=application
apollo.bootstrap.eagerLoad.enabled=true
四种方式获取配置
代码语言:txt复制package cn.beckbi.controller;
import cn.beckbi.response.IdData;
import cn.beckbi.util.JacksonTool;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
/**
* @program: spring-cloud
* @description:
* @author: bikang
* @create: 2022-02-09 23:04
*/
@Slf4j
@Api(tags={"用户接口"})
@RestController
public class UserController {
@ApolloConfig
private Config config;
@Value("${user_info}")
private String configData;
@ApolloJsonValue("${user_info}")
private IdData idData;
private Optional<IdData> getIdDataFromConfig() {
String data = config.getProperty("user_info", "");
return Optional.ofNullable(JacksonTool.parseObject(data, IdData.class));
}
@GetMapping("/config1")
public String configInfo1() {
return configData;
}
@GetMapping("/config2")
public IdData configInfo2() {
return idData;
}
@GetMapping("/config3")
public String configInfo3() {
Config config = ConfigService.getConfig("application");
return config.getProperty("user_info", "");
}
@GetMapping("/user/{uid}")
public IdData info(@PathVariable long uid) {
IdData idData = new IdData();
idData.setUid(uid);
idData.setId("");
getIdDataFromConfig().ifPresent(
data -> {
log.info("{}", data);
idData.setId(data.getId());
}
);
log.info("apollo-config:" JacksonTool.toJSONString(config.getPropertyNames()));
return idData;
}
@ApolloConfigChangeListener
private void changer(ConfigChangeEvent changeEvent) {
if(changeEvent.isChanged("user_info")) {
log.info("user_info changeed");
}
}
}
代码详情
https://github.com/beckbikang/spring-cloud/tree/main/kapollo