1. 程式人生 > 程式設計 >Springboot apollo原理及使用方法詳解

Springboot apollo原理及使用方法詳解

文章背景

如果在spring boot中接入apollo官方文件:https://github.com/ctripcorp/apollo/wiki使用官方的apollo

演示環境(Demo):

106.54.227.205賬號/密碼:apollo/admin

新增配置

Springboot apollo原理及使用方法詳解

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 [email protected]
 * @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 id="mysqlTables" targetRuntime="MyBatis3">
    <commentGenerator>
      <property name="suppressDate" value="false"/>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--目標資料庫配置-->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="${spring.datasource.url}"
        userId="${spring.datasource.username}"
        password="${spring.datasource.password}" />
    <!-- 指定生成的型別為java型別,避免資料庫中number等型別欄位 -->
    <javaTypeResolver>
      <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
    <!-- 生成model模型,對應的包,存放位置可以指定具體的路徑,如/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>
    <!--對應的xml mapper檔案 -->

    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis">
      <property name="enableSubPackages" value="false"/>
    </sqlMapGenerator>
    <!-- 對應的dao介面 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.apollodemo.mapper" targetProject="src/main/java">
      <property name="enableSubPackages" value="false"/>
    </javaClientGenerator>
    <!--定義需要操作的表及對應的DTO名稱-->
    <table tableName="t_user" domainObjectName="User"/>
  </context>
</generatorConfiguration>

專案demo地址https://github.com/KoMiles/spring-example/tree/master/apollo-demo

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。