-
Notifications
You must be signed in to change notification settings - Fork 280
apijson多数据源-kafka #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
APIJSON-Java-Server/APIJSONDemo-MultiDataSource-Kafka/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# APIJSONDemo | ||
|
||
## 支持多数据源-消息队列 | ||
|
||
示例:kafka | ||
|
||
原理说明: | ||
|
||
Access表名 = 消息队列 topic | ||
|
||
Access表配置说明: | ||
 | ||
Request表配置post权限 | ||
 | ||
|
||
apijson发送mq消息: | ||
单条<br/> | ||
{ | ||
"@datasource": "kafka", | ||
"Topic_User":{ | ||
"message":"test-101" | ||
}, | ||
"tag": "Topic_User", | ||
"@explain": false | ||
}<br/> | ||
多条<br/> | ||
{ | ||
"Topic_User[]": [ | ||
{ | ||
"message":"test-100" | ||
}, | ||
{ | ||
"message":"test-101" | ||
} | ||
], | ||
"tag": "Topic_User[]", | ||
"@datasource": "kafka", | ||
"@explain": true | ||
} | ||
|
||
客户端接收消息: | ||
|
||
offset = 47, key = null, value = test-101<br/> | ||
offset = 48, key = null, value = test-100<br/> | ||
offset = 49, key = null, value = test-101<br/> | ||
|
||
|
||
用java代码方式,获取具体数据源,调用即可 |
203 changes: 203 additions & 0 deletions
APIJSON-Java-Server/APIJSONDemo-MultiDataSource-Kafka/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.5.13</version> | ||
<!-- <relativePath>./pom.xml</relativePath>--> | ||
</parent> | ||
<groupId>apijson.demo</groupId> | ||
<artifactId>apijsondemo-multidatasource-kafka</artifactId> | ||
<version>5.4.0</version> | ||
|
||
<name>apijsondemo-multidatasource-kafka</name> | ||
<description>Demo project for testing APIJSON server based on SpringBoot</description> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<commons-lang3.version>3.12.0</commons-lang3.version> | ||
<druid.version>1.1.16</druid.version> | ||
<mybatisplus.version>3.5.1</mybatisplus.version> | ||
<mybatis-plus-support.version>2.3.3</mybatis-plus-support.version> | ||
<commons-collections4.version>4.4</commons-collections4.version> | ||
<commons.configuration.version>1.10</commons.configuration.version> | ||
<guava.version>30.1.1-jre</guava.version> | ||
<fastjson.version>1.2.72</fastjson.version> | ||
<hutool.version>4.1.1</hutool.version> | ||
<lombok.version>1.18.4</lombok.version> | ||
<commons-lang3.version>3.12.0</commons-lang3.version> | ||
<commons.io.version>2.5</commons.io.version> | ||
<commons.codec.version>1.10</commons.codec.version> | ||
<commons-collections4.version>4.4</commons-collections4.version> | ||
<commons.configuration.version>1.10</commons.configuration.version> | ||
<apijson.version>5.4.0</apijson.version> | ||
<mysql.version>8.0.31</mysql.version> | ||
<spring-context-support.version>5.3.18</spring-context-support.version> | ||
<spring-boot-configuration-processor.version>2.6.6</spring-boot-configuration-processor.version> | ||
<dynamic-datasource-spring-boot-starter.version>3.5.2</dynamic-datasource-spring-boot-starter.version> | ||
<java.version>1.8</java.version> | ||
<kafka.version>3.2.1</kafka.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- JDK 11+ 需要,否则启动报错 NoClassDefFoundError: javax/activation/UnsupportedDataTypeException --> | ||
<dependency> | ||
<groupId>javax.activation</groupId> | ||
<artifactId>activation</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
|
||
<!-- 需要的 APIJSON 相关依赖 --> | ||
<dependency> | ||
<groupId>com.github.Tencent</groupId> | ||
<artifactId>APIJSON</artifactId> | ||
<version>${apijson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.APIJSON</groupId> | ||
<artifactId>apijson-framework</artifactId> | ||
<version>${apijson.version}</version> | ||
</dependency> | ||
|
||
<!-- 需要用的数据库 JDBC 驱动 --> | ||
|
||
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 --> | ||
|
||
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
<version>${spring-context-support.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<version>${spring-boot-configuration-processor.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>${druid.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> | ||
<version>${dynamic-datasource-spring-boot-starter.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-boot-starter</artifactId> | ||
<version>${mybatisplus.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-generator</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-support</artifactId> | ||
<version>${mybatis-plus-support.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-collections4</artifactId> | ||
<version>${commons-collections4.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>${mysql.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>${guava.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>${lombok.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>${commons.io.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-codec</groupId> | ||
<artifactId>commons-codec</artifactId> | ||
<version>${commons.codec.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-configuration</groupId> | ||
<artifactId>commons-configuration</artifactId> | ||
<version>${commons.configuration.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.kafka</groupId> | ||
<artifactId>kafka-clients</artifactId> | ||
<version>${kafka.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<fork>true</fork> | ||
<mainClass>apijson.demo.DemoApplication</mainClass> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>repackage</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<!-- APIJSON 必须用到的托管平台 --> | ||
<repository> | ||
<id>jitpack.io</id> | ||
<url>https://jitpack.io</url> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
</repository> | ||
|
||
<repository> | ||
<id>spring-snapshots</id> | ||
<url>https://repo.spring.io/snapshot</url> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
</repository> | ||
<repository> | ||
<id>spring-milestones</id> | ||
<url>https://repo.spring.io/milestone</url> | ||
</repository> | ||
</repositories> | ||
|
||
</project> |
22 changes: 22 additions & 0 deletions
...a-Server/APIJSONDemo-MultiDataSource-Kafka/src/main/java/apijson/demo/DataBaseConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package apijson.demo; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class DataBaseConfig { | ||
private String primary; | ||
|
||
@Value("${spring.datasource.dynamic.primary}") | ||
public void setPrimary(String primary) { | ||
this.primary = primary; | ||
} | ||
|
||
public String getPrimary() { | ||
return primary; | ||
} | ||
|
||
public static DataBaseConfig getInstence() { | ||
return SpringContextUtils.getBean(DataBaseConfig.class); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
...ava-Server/APIJSONDemo-MultiDataSource-Kafka/src/main/java/apijson/demo/DataBaseUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package apijson.demo; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
|
||
@Log4j2 | ||
public class DataBaseUtil { | ||
|
||
/** | ||
* 根据url获取库名 | ||
* @param url | ||
* @return | ||
*/ | ||
public static String getLibname(String url) { | ||
Pattern p = Pattern.compile("jdbc:(?<db>\\w+):.*((//)|@)(?<host>.+):(?<port>\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\w+)\\??.*"); | ||
Matcher m = p.matcher(url); | ||
if(m.find()) { | ||
return m.group("dbName"); | ||
} | ||
return null; | ||
} | ||
|
||
/*** | ||
* primary: master | ||
* strict: false | ||
* @param datasource: 匹配不成功, 自动匹配默认数据库 | ||
* @return | ||
*/ | ||
public static javax.sql.DataSource getDataSource(String datasource) { | ||
try { | ||
return DynamicDataSource.getDetail(datasource).getDataSource(); // 数据源 | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException("动态数据源配置错误 " + datasource); | ||
} | ||
} | ||
|
||
public static String getDruidUrl(String datasource) { | ||
return DynamicDataSource.getDetail(datasource).getUrl(); // 数据库连接url | ||
} | ||
|
||
public static String getDruidSchema(String datasource) { | ||
return getLibname(DynamicDataSource.getDetail(datasource).getUrl()); // 数据库名; | ||
} | ||
|
||
public static String getDruidDBAccount(String datasource) { | ||
return DynamicDataSource.getDetail(datasource).getDbAccount(); // 数据库用户名 | ||
} | ||
|
||
public static String getDruidDBPassword(String datasource) { | ||
return DynamicDataSource.getDetail(datasource).getDbPassword(); // 数据库密码 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.