springboot整合apollo配置中心

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);
    }
}

打印结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值