【Apollo】02-Apollo配置中心与SpringCloud

Apollo配置中心与SpringCloud

创建SpringCloud-Apollo项目

  1. 添加依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.8.RELEASE</version>
    </parent>

    <properties>
    <apollo.version>1.6.0</apollo.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>${apollo.version}</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>
    </dependencies>
  2. 添加配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    server:
    port: 9200
    spring:
    application:
    name: springboot-apollo

    # apollo 相关配置
    app:
    id: springboot-apollo-config # 与Apollo配置中心的AppId一致
    apollo:
    meta: http://localhost:8080 # Apollo 配置中心的 注册中心 Eureka
    # cluster: # 指定 Apollo 的集群,相同集群实例,使用对应集群的配置
    # cacheDir: # 配置缓存目录,网络不可用时仍然可提供配置服务
    bootstrap:
    enable: true # 启用 Apollo
    env: DEV # 指定环境

    name: zhangsan
    user:
    username: lisi
    age: 20
    gender:
    description: 美男子
  3. 启动类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @EnableApolloConfig  //开启Apollo
    @SpringBootApplication
    public class ApolloApplication {

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

    }
  4. 获取配置的实体类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Component
    public class ApolloConfig {

    @Value("${name}")
    private String name;
    @Value("${user.username}")
    private String username;
    @Value("${user.age}")
    private String age;
    @Value("${user.gender}")
    private String gender;
    @Value("${user.description}")
    private String description;

    }
  5. 添加接口,输出获取的配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @RestController
    @RequestMapping("/apollo")
    public class ApolloController {

    @Resource
    private ApolloConfig apolloConfig;

    @GetMapping("/config")
    public String getApolloConfig() {
    return apolloConfig.toString();
    }

    }

Apollo项目中添加配置

  1. 在Apollo管理界面中创建项目

    img-10

    1. 部门、项目负责人这些配置可以自己设置
    2. AppId:配置项目的唯一标识。
  2. 测试一:此时未在Apollo配置中心中添加配置

    1. 发出请求:localhost:9200/apollo/config

      得到结果:

      img-11

  3. 测试二:向Apollo配置中心添加配置

    1. 添加配置:
      img-12

    2. 发布配置
      img-13

    3. SpringBoot项目收到来自Apollo配置中心的指令,项目更新配置
      img-14

    4. 发出请求:localhost:9200/apollo/config
      img-15

-------------本文结束感谢您的阅读-------------