Springboot apollo原理及使用方法詳解
文章背景
如果在spring boot中接入apollo官方文檔:https://github.com/ctripcorp/apollo/wiki使用官方的apollo
演示環(huán)境(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.
數(shù)據(jù)庫配置如何使用?
同理,generatorConfig.xml中也可以讀取數(shù)據(jù)庫配置
<?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 id="mysqlTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--目標(biāo)數(shù)據(jù)庫配置-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}" />
<!-- 指定生成的類型為java類型,避免數(shù)據(jù)庫中number等類型字段 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成model模型,對應(yīng)的包,存放位置可以指定具體的路徑,如/ProjectName/src,也可以使用MAVEN來自動生成 -->
<javaModelGenerator targetPackage="com.example.apollodemo.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
<property name="immutable" value="false"/>
</javaModelGenerator>
<!--對應(yīng)的xml mapper文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 對應(yīng)的dao接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.apollodemo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--定義需要操作的表及對應(yīng)的DTO名稱-->
<table tableName="t_user" domainObjectName="User"/>
</context>
</generatorConfiguration>
項目demo地址https://github.com/KoMiles/spring-example/tree/master/apollo-demo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?整合Mybatis-Plus并輸出SQL日志示例詳解
這篇文章主要介紹了SpringBoot整合Mybatis-Plus并輸出SQL日志,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
java基本教程之Thread中start()和run()的區(qū)別 java多線程教程
這篇文章主要介紹了Thread中start()和run()的區(qū)別,Thread類包含start()和run()方法,它們的區(qū)別是什么?下面將對此作出解答2014-01-01
解決Servlet4.0版本使用注解設(shè)置url但無法訪問的問題
在學(xué)習(xí)servlet過程中,使用web.xml文件配置servlet可以正常訪問,但使用WebServlet注解時出現(xiàn)404錯誤,解決方法是在web.xml文件中將metadata-complete屬性改為false,啟動標(biāo)注支持,然而該方法對我無效,最后通過重建項目和手動將新建的項目添加到tomcat服務(wù)器解決問題2024-10-10
JDBC+GUI實現(xiàn)簡單學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了JDBC+GUI實現(xiàn)簡單學(xué)生管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
Java?MyBatis是如何執(zhí)行一條SQL語句的
這篇文章主要介紹了Java?MyBatis是如何執(zhí)行一條SQL語句的,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
Java LinkedHashMap 底層實現(xiàn)原理分析
LinkedHashMap繼承自HashMap實現(xiàn)了Map接口?;緦崿F(xiàn)同HashMap一樣,不同之處在于LinkedHashMap保證了迭代的有序性。其內(nèi)部維護(hù)了一個雙向鏈表,解決了 HashMap不能隨時保持遍歷順序和插入順序一致的問題。2021-05-05

