1 下载apollo源码
2 使用docker-compose启动apollo
进入到启动docker compose 的文件路径,我这里是在centos系统启动的apollo。我已经提前安装好docker和docker-compose。
输入执行命令
[root@jamie docker-quick-start]# docker-compose up
启动成功后,我们就可以在浏览器输入http://ip:8070,访问apollo的页面。
默认账号:apollo
默认密码:admin
3 配置一个项目
登录成功进入页面后,点击创建项目
输入项目信息
提交过后,我们就来到项目信息页面,然后添加配置
这里我们在DEV环境添加了一个配置:name=JackRose
发布我们刚才新增的配置
发布成功后的显示情况
4 springboot应用使用apollo的配置
在pom文件中导入apollo客户端的依赖
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.4.0</version>
</dependency>
在application.properties文件中配置apollo
app.id=jamie
#本地启动apollo时,这样配置
#apollo.meta=http://127.0.0.1:8080
在启动类添加@EnableApolloConfig
注解
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
@EnableApolloConfig
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
注意,因为这里我的apollo部署在云服务器上,如果我本地想要使用apollo配置的话,需要设置JVM参数:
-Dapollo.configService=http://118.31.48.47:8080
使用apollo的配置
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDemoApplicationTests {
@Value("${name}")
private String name;
@Test
public void apolloTest() {
System.out.println(name);
}
}
打印结果