Apollo整合SpringBoot开发

Posted 天宇轩-王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }

}

 

 进行读取配置

 

 说明配置已经生效了。

 

以上是关于Apollo整合SpringBoot开发的主要内容,如果未能解决你的问题,请参考以下文章

Apollo生产环境整合springboot

Apollo生产环境整合springboot

SpringBoot2.0之整合Apollo

springboot Thymeleaf 整合

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段

apollo客户端springboot实战