Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5469740

Browse files
apijson多数据源支持kafka
apijson5.4支持
1 parent e61da33 commit 5469740

File tree

16 files changed

+1247
-0
lines changed

16 files changed

+1247
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# APIJSONDemo
2+
3+
## 支持多数据源-消息队列
4+
5+
示例:kafka
6+
7+
原理说明:
8+
9+
Access表名 = 消息队列 topic
10+
11+
Access表配置说明:
12+
13+
![image-20230106155124881](/Users/xy/Library/Application Support/typora-user-images/image-20230106155124881.png)
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-starter-parent</artifactId>
7+
<version>2.5.13</version>
8+
<!-- <relativePath>./pom.xml</relativePath>-->
9+
</parent>
10+
<groupId>apijson.demo</groupId>
11+
<artifactId>apijsondemo-multidatasource-kafka</artifactId>
12+
<version>5.4.0</version>
13+
14+
<name>apijsondemo-multidatasource-kafka</name>
15+
<description>Demo project for testing APIJSON server based on SpringBoot</description>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
20+
<commons-lang3.version>3.12.0</commons-lang3.version>
21+
<druid.version>1.1.16</druid.version>
22+
<mybatisplus.version>3.5.1</mybatisplus.version>
23+
<mybatis-plus-support.version>2.3.3</mybatis-plus-support.version>
24+
<commons-collections4.version>4.4</commons-collections4.version>
25+
<commons.configuration.version>1.10</commons.configuration.version>
26+
<guava.version>30.1.1-jre</guava.version>
27+
<fastjson.version>1.2.72</fastjson.version>
28+
<hutool.version>4.1.1</hutool.version>
29+
<lombok.version>1.18.4</lombok.version>
30+
<commons-lang3.version>3.12.0</commons-lang3.version>
31+
<commons.io.version>2.5</commons.io.version>
32+
<commons.codec.version>1.10</commons.codec.version>
33+
<commons-collections4.version>4.4</commons-collections4.version>
34+
<commons.configuration.version>1.10</commons.configuration.version>
35+
<apijson.version>5.4.0</apijson.version>
36+
<mysql.version>8.0.31</mysql.version>
37+
<spring-context-support.version>5.3.18</spring-context-support.version>
38+
<spring-boot-configuration-processor.version>2.6.6</spring-boot-configuration-processor.version>
39+
<dynamic-datasource-spring-boot-starter.version>3.5.2</dynamic-datasource-spring-boot-starter.version>
40+
<java.version>1.8</java.version>
41+
<kafka.version>3.2.1</kafka.version>
42+
</properties>
43+
44+
<dependencies>
45+
<!-- JDK 11+ 需要,否则启动报错 NoClassDefFoundError: javax/activation/UnsupportedDataTypeException -->
46+
<dependency>
47+
<groupId>javax.activation</groupId>
48+
<artifactId>activation</artifactId>
49+
<version>1.1.1</version>
50+
</dependency>
51+
52+
<!-- 需要的 APIJSON 相关依赖 -->
53+
<dependency>
54+
<groupId>com.github.Tencent</groupId>
55+
<artifactId>APIJSON</artifactId>
56+
<version>${apijson.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.github.APIJSON</groupId>
60+
<artifactId>apijson-framework</artifactId>
61+
<version>${apijson.version}</version>
62+
</dependency>
63+
64+
<!-- 需要用的数据库 JDBC 驱动 -->
65+
66+
<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->
67+
68+
<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-web</artifactId>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework</groupId>
75+
<artifactId>spring-context-support</artifactId>
76+
<version>${spring-context-support.version}</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-configuration-processor</artifactId>
81+
<version>${spring-boot-configuration-processor.version}</version>
82+
<optional>true</optional>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.alibaba</groupId>
86+
<artifactId>druid-spring-boot-starter</artifactId>
87+
<version>${druid.version}</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>com.baomidou</groupId>
91+
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
92+
<version>${dynamic-datasource-spring-boot-starter.version}</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>com.baomidou</groupId>
96+
<artifactId>mybatis-plus-boot-starter</artifactId>
97+
<version>${mybatisplus.version}</version>
98+
<exclusions>
99+
<exclusion>
100+
<groupId>com.baomidou</groupId>
101+
<artifactId>mybatis-plus-generator</artifactId>
102+
</exclusion>
103+
</exclusions>
104+
</dependency>
105+
<dependency>
106+
<groupId>com.baomidou</groupId>
107+
<artifactId>mybatis-plus-support</artifactId>
108+
<version>${mybatis-plus-support.version}</version>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.apache.commons</groupId>
112+
<artifactId>commons-collections4</artifactId>
113+
<version>${commons-collections4.version}</version>
114+
</dependency>
115+
<dependency>
116+
<groupId>mysql</groupId>
117+
<artifactId>mysql-connector-java</artifactId>
118+
<version>${mysql.version}</version>
119+
</dependency>
120+
<dependency>
121+
<groupId>com.google.guava</groupId>
122+
<artifactId>guava</artifactId>
123+
<version>${guava.version}</version>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.projectlombok</groupId>
127+
<artifactId>lombok</artifactId>
128+
<version>${lombok.version}</version>
129+
</dependency>
130+
<dependency>
131+
<groupId>commons-io</groupId>
132+
<artifactId>commons-io</artifactId>
133+
<version>${commons.io.version}</version>
134+
</dependency>
135+
<dependency>
136+
<groupId>commons-codec</groupId>
137+
<artifactId>commons-codec</artifactId>
138+
<version>${commons.codec.version}</version>
139+
</dependency>
140+
<dependency>
141+
<groupId>commons-configuration</groupId>
142+
<artifactId>commons-configuration</artifactId>
143+
<version>${commons.configuration.version}</version>
144+
</dependency>
145+
<dependency>
146+
<groupId>org.apache.kafka</groupId>
147+
<artifactId>kafka-clients</artifactId>
148+
<version>${kafka.version}</version>
149+
</dependency>
150+
</dependencies>
151+
152+
<build>
153+
<plugins>
154+
<plugin>
155+
<groupId>org.springframework.boot</groupId>
156+
<artifactId>spring-boot-maven-plugin</artifactId>
157+
<configuration>
158+
<fork>true</fork>
159+
<mainClass>apijson.demo.DemoApplication</mainClass>
160+
</configuration>
161+
<executions>
162+
<execution>
163+
<goals>
164+
<goal>repackage</goal>
165+
</goals>
166+
</execution>
167+
</executions>
168+
</plugin>
169+
<plugin>
170+
<groupId>org.apache.maven.plugins</groupId>
171+
<artifactId>maven-compiler-plugin</artifactId>
172+
<configuration>
173+
<source>1.8</source>
174+
<target>1.8</target>
175+
</configuration>
176+
</plugin>
177+
</plugins>
178+
</build>
179+
180+
<repositories>
181+
<!-- APIJSON 必须用到的托管平台 -->
182+
<repository>
183+
<id>jitpack.io</id>
184+
<url>https://jitpack.io</url>
185+
<snapshots>
186+
<enabled>true</enabled>
187+
</snapshots>
188+
</repository>
189+
190+
<repository>
191+
<id>spring-snapshots</id>
192+
<url>https://repo.spring.io/snapshot</url>
193+
<snapshots>
194+
<enabled>true</enabled>
195+
</snapshots>
196+
</repository>
197+
<repository>
198+
<id>spring-milestones</id>
199+
<url>https://repo.spring.io/milestone</url>
200+
</repository>
201+
</repositories>
202+
203+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package apijson.demo;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class DataBaseConfig {
8+
private String primary;
9+
10+
@Value("${spring.datasource.dynamic.primary}")
11+
public void setPrimary(String primary) {
12+
this.primary = primary;
13+
}
14+
15+
public String getPrimary() {
16+
return primary;
17+
}
18+
19+
public static DataBaseConfig getInstence() {
20+
return SpringContextUtils.getBean(DataBaseConfig.class);
21+
}
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package apijson.demo;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
import lombok.extern.log4j.Log4j2;
7+
8+
@Log4j2
9+
public class DataBaseUtil {
10+
11+
/**
12+
* 根据url获取库名
13+
* @param url
14+
* @return
15+
*/
16+
public static String getLibname(String url) {
17+
Pattern p = Pattern.compile("jdbc:(?<db>\\w+):.*((//)|@)(?<host>.+):(?<port>\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\w+)\\??.*");
18+
Matcher m = p.matcher(url);
19+
if(m.find()) {
20+
return m.group("dbName");
21+
}
22+
return null;
23+
}
24+
25+
/***
26+
* primary: master
27+
* strict: false
28+
* @param datasource: 匹配不成功, 自动匹配默认数据库
29+
* @return
30+
*/
31+
public static javax.sql.DataSource getDataSource(String datasource) {
32+
try {
33+
return DynamicDataSource.getDetail(datasource).getDataSource(); // 数据源
34+
} catch (Exception e) {
35+
throw new IllegalArgumentException("动态数据源配置错误 " + datasource);
36+
}
37+
}
38+
39+
public static String getDruidUrl(String datasource) {
40+
return DynamicDataSource.getDetail(datasource).getUrl(); // 数据库连接url
41+
}
42+
43+
public static String getDruidSchema(String datasource) {
44+
return getLibname(DynamicDataSource.getDetail(datasource).getUrl()); // 数据库名;
45+
}
46+
47+
public static String getDruidDBAccount(String datasource) {
48+
return DynamicDataSource.getDetail(datasource).getDbAccount(); // 数据库用户名
49+
}
50+
51+
public static String getDruidDBPassword(String datasource) {
52+
return DynamicDataSource.getDetail(datasource).getDbPassword(); // 数据库密码
53+
}
54+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.demo;
16+
17+
import org.springframework.boot.SpringApplication;
18+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
21+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
22+
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
26+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
27+
28+
import apijson.Log;
29+
import apijson.framework.APIJSONApplication;
30+
import apijson.framework.APIJSONCreator;
31+
import apijson.orm.AbstractFunctionParser;
32+
import apijson.orm.AbstractVerifier;
33+
import apijson.orm.FunctionParser;
34+
import apijson.orm.Parser;
35+
import apijson.orm.SQLConfig;
36+
import apijson.orm.SQLExecutor;
37+
38+
/**
39+
* Demo SpringBoot Application 主应用程序启动类 右键这个类 > Run As > Java Application 具体见
40+
* SpringBoot 文档
41+
* https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class
42+
*
43+
* @author Lemon
44+
*/
45+
@Configuration
46+
@SpringBootApplication
47+
@EnableAutoConfiguration
48+
@EnableConfigurationProperties
49+
public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
50+
public static final String TAG = "DemoApplication";
51+
52+
public static void main(String[] args) throws Exception {
53+
SpringApplication.run(DemoApplication.class, args);
54+
Log.DEBUG = true;
55+
APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效
56+
}
57+
58+
// SpringBoot 2.x 自定义端口方式
59+
@Override
60+
public void customize(ConfigurableServletWebServerFactory server) {
61+
server.setPort(8080);
62+
}
63+
64+
// 支持 APIAuto 中 JavaScript 代码跨域请求
65+
@Bean
66+
public WebMvcConfigurer corsConfigurer() {
67+
return new WebMvcConfigurer() {
68+
@Override
69+
public void addCorsMappings(CorsRegistry registry) {
70+
registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("*").allowCredentials(true)
71+
.maxAge(3600);
72+
}
73+
};
74+
}
75+
76+
static {
77+
// 使用本项目的自定义处理类
78+
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<String>() {
79+
@Override
80+
public Parser<String> createParser() {
81+
return new DemoParser();
82+
}
83+
84+
@Override
85+
public SQLConfig createSQLConfig() {
86+
return new DemoSQLConfig();
87+
}
88+
89+
@Override
90+
public FunctionParser createFunctionParser() {
91+
return new DemoFunctionParser();
92+
}
93+
94+
@Override
95+
public SQLExecutor createSQLExecutor() {
96+
return new DemoSQLExecutor();
97+
}
98+
};
99+
}
100+
101+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /