文章背景
- 本文介绍如果在spring boot中接入apollo
- 官方文档:https://github.com/ctripcorp/apollo/wiki#%E4%B8%80%E6%99%AE%E9%80%9A%E5%BA%94%E7%94%A8%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
使用官方的apollo
演示环境(Demo):
- 106.54.227.205
- 账号/密码:apollo/admin
添加配置
spring-boot中如何使用呢
pom.xml中添加配置
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
配置文件中添加apollo地址
app:
id: komiles
apollo:
meta: http://106.54.227.205:8080
bootstrap:
enabled: true
namespaces: application
启动类中添加代码
添加@EnableApolloConfig注解
package com.example.apollodemo;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableApolloConfig
@MapperScan("com.example.apollodemo.mapper")
public class ApolloDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ApolloDemoApplication.class, args);
System.out.println("============ apollo demo application end =============");
}
}
controller类新增文件
ApolloController.java
package com.example.apollodemo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author komiles@163.com
* @date 2020-05-06 17:28
*/
@RestController
@RequestMapping("/apollo")
public class ApolloController {
@Value("${name}")
private String name;
@GetMapping("/name")
public String name()
{
return name;
}
}
可以读取到配置为kongming.
数据库配置如何使用?
同理,generatorConfig.xml中也可以读取数据库配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context />
</context>
</generatorConfiguration>
项目demo地址