文章背景
- 本文介绍如果在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中添加配置
csharp;gutter:true;
com.ctrip.framework.apollo
apollo-client
1.1.0</p>
<pre><code>
配置文件中添加apollo地址
;gutter:true;
app:
id: komiles
apollo:
meta: http://106.54.227.205:8080
bootstrap:
enabled: true
namespaces: application
启动类中添加代码
添加@EnableApolloConfig注解
csharp;gutter:true;
package com.example.apollodemo;</p>
<p>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;</p>
<p>@SpringBootApplication
@EnableApolloConfig
@MapperScan("com.example.apollodemo.mapper")
public class ApolloDemoApplication {</p>
<pre><code>public static void main(String[] args) {
SpringApplication.run(ApolloDemoApplication.class, args);
System.out.println("============ apollo demo application end =============");
}
</code></pre>
<p>}</p>
<pre><code>
controller类新增文件
ApolloController.java
;gutter:true;
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中也可以读取数据库配置
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}" />
项目demo地址
Original: https://www.cnblogs.com/wangkongming/p/12838472.html
Author: KoMiles
Title: 【spring-boot】如何使用apollo
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/539712/
转载文章受原作者版权保护。转载请注明原作者出处!