Springboot apollo原理及使用方法详解
程序员文章站
2023-12-25 21:37:09
文章背景如果在spring boot中接入apollo官方文档:使用官方的apollo演示环境(demo):106.54.227.205账号/密码:apollo/admin添加配置spring-boo...
文章背景
如果在spring boot中接入apollo官方文档:使用官方的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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。