springboot整合Apollo

运行环境

jdk版本:jdk1.8.0_202
springboot版本:2.2.0
mysql版本:5.6.31

启动MySQL

命令:service mysql start
在这里插入图片描述

启动Apollo

命令:cd /usr/local/apollo/
命令:./demo.sh start
springboot整合Apollo_第1张图片

  • 查看防火墙状态
    命令:firewall-cmd --state
  • 关闭防火墙
    命令:systemctl stop firewalld.service
    springboot整合Apollo_第2张图片
  • 打开浏览器访问 http://192.168.1.108:8070
    springboot整合Apollo_第3张图片
  • 创建测试项目
    springboot整合Apollo_第4张图片
  • 创建测试数据
    springboot整合Apollo_第5张图片

springboot整合Apollo

1、在pom.xml中添加依赖包

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

2、在 “src/main/resources” 下创建Apollo配置文件(app.properties)
springboot整合Apollo_第6张图片

app.id=jeff
apollo.meta=http://192.168.1.108:8080

3、在springboot启动类开启Apollo配置,添加注解 “@EnableApolloConfig”

package com.jeff;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableApolloConfig
public class JfSpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(JfSpringbootApplication.class, args);
    }

}

4、创建controller测试Apollo(ApolloController.java)

package com.jeff.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jeff
 * @description 测试springboot整合Apollo
 * @date 2019/11/4
 */

@RestController
public class ApolloController {

    @Value("${name}")
    private String name;

    @Value("${nickname:jiefu}")//Apollo中不存在时,nickname默认值为jiefu
    private String nickname;

    @RequestMapping("/getName")
    public String getName() {
        return name;
    }

    @RequestMapping("/getNickname")
    public String getNickname() {
        return nickname;
    }

}

5、启动springboot项目,打开浏览器访问 http://localhost:8080/getName
springboot整合Apollo_第7张图片

源码地址:

https://gitee.com/jiefu813/jf-springboot/tree/jf_Apollo/

你可能感兴趣的:(SpringBoot,Apollo,springboot,Apollo)