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

     进行读取配置

     说明配置已经生效了。

     

  • 相关阅读:
    求1977!
    三进制小数
    回文数C语言
    JAVA知识点必看
    servlet HttpServletRequest
    为什么web工程要输入localhost或者是127.0.0.1
    service $sce or ng-bind-html
    jQuery的deferred对象详解
    理解promise
    理解Angular中的$apply()以及$digest()
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12215513.html
Copyright © 2020-2023  润新知