• Apollo整合SpringBoot开发


    创建项目

    pom.xml

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>com.ctrip.framework.apollo</groupId>
                <artifactId>apollo-client</artifactId>
                <version>1.3.0</version>
            </dependency>
    
        </dependencies>

     必选配置

    app.id=account-service
    apollo.cacheDir=/opt/data/apollo‐config
    apollo.cluster=SHAJQ
    apollo:
      meta: http://192.168.180.137:8080
      bootstrap:
        enabled: true
        eagerLoad:
          enabled: true
        namespaces: application,micro_service.spring‐boot‐http,spring‐rocketmq,spring-boot-druid

    配置类

    @SpringBootApplication
    @EnableApolloConfig
    public class AccountServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AccountServiceApplication.class, args);
        }
    
    }

    web层

    @RestController
    public class AccountController {
    
        @Value("${sms.enable}")
        private Boolean smsEnable;
    
        @GetMapping("/hi")
        public String hi() {
            return "hi";
        }
    
        @Value("${timeout}")
        private Long timeout;
    
        @GetMapping("/timeout")
        public Long getTimeout() {
            return timeout;
        }
    
    }

     进行读取配置

     说明配置已经生效了。

     

  • 相关阅读:
    DBAccess
    业务耗时及数组
    QB资料学习.01
    格式化参数
    日志记录.02_线程处理
    nginx 开启gzip
    js 变量提升和函数提升
    js 深拷贝 vs 浅拷贝
    js 伪数组 转 数组
    js 对象原型和原型链
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12215513.html
Copyright © 2020-2023  润新知