Springboot apollo原理及使用方法详解

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

文章背景

如果在spring boot中接入apollo官方文档:https://github.com/ctripcorp/apollo/wiki使用官方的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 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多