# tianyun-framework
**Repository Path**: download/tianyun-framework
## Basic Information
- **Project Name**: tianyun-framework
- **Description**: Java开源框架,Spring Cloud
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 4
- **Forks**: 0
- **Created**: 2020-12-12
- **Last Updated**: 2026-01-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# tianyun-framework 天有不测风云
## 介绍
**天云壁垒Framework**
基于Java语言的开源技术框架,适用于应用系统、Web服务的快速开发部署。使用中发现问题请在Issues里反馈,外部引用请注明出处。
## 使用说明
### 快速开始
pom.xml
```
4.0.0
org.tianyun
tianyun-framework-dependencies
1.0.0-SNAPSHOT
tianyun-cloud-sample
jar
org.tianyun
tianyun-cloud-web-starter
org.tianyun
tianyun-cloud-test
test
```
启动类:
```
@CloudBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
```
### 一、基本组件
组件包括:ServletContext、RequestContext、i18n、ThreadPool、SmartScope、控制台组件管理等;
```
org.tianyun
tianyun-cloud-context
org.tianyun
tianyun-cloud-config
```
1.1、ServletContextHolder.setContextProperty(String key, String value)
往Servlet上下文中设置一个值,该值在当前线程中可以访问,可以使用ServletContextHolder.dispatch(ContextOperator operator)
跨服务传递,ServletContext包含的默认值有context-trace-id、context-language;
1.2、RequestContextHolder.set(Class contextClass, BiConsumer setter, V value)
往Request上下文中设置一个值,该值在当前Request可以访问,可以使用RequestContextHolder.dispatch(ContextOperator operator)
跨服务传递。可以自定义RequestContext中的字段属性,继承RequestContext即可,如:loginUserId。若使用mybatis框架,可直接在mapper.xml中访问RequestContext,变量名为context,例如:#{context.loginUserId};
1.3、i18n为语言国际化,在bootstrap.yml或配置中心加上配置即可,服务应该加上以下默认配置:
```
i18n:
locale: zh-CN
messages:
zh-CN:
response:
200: 成功
5000: 哎哟,出错了~
5001: 请求处理失败[{0}],请稍后再试
4001: 请求Content-Type错误
4002: 缺少必要的请求参数
4003: '{0}'
4004: 错误的输入数据
4005: 请求参数{0}不能为空
4006: 请求参数不合法
properties:
order.pay: 本次需要支付{0}元
en-US:
response:
200: Success
5000: Oh, wrong ~
5001: Request processing failure[{0}], please try again later
4001: Request the Content-Type errors
4002: Lack the necessary request parameters
4003: '{0}'
4004: The wrong input data
4005: Request parameters {0} cannot be empty
4006: Request parameter is illegal
properties:
order.pay: The need to pay RMB {0}
```
1.4、线程池请使用new ThreadPool(...),继承自ThreadPoolExecutor,如需使用其他线程池,请使用ThreadContext.wrap(Runnable
command)对Runnable进行包装;单独创建一个线程,请使用new WorkThread(...)。
1.5、配置容器热刷新,提供两种方式支持刷新,第一种注解方式:在Bean上加@SmartScope,第二种方式:使用SmartBeanDefinition来手动注册Bean。
1.6、控制台组件管理,框架已经对DataSource、Redis、RabbitMQ等中间件组件化管理,启动后或组件刷新自动收集组件自动参数上报到控制管理台,控制台可对某应用或使用到某个服务集群的所有应用整体进行管控,比如:对使用到A集群Redis的所有应用切换到B集群,控制台支持这种能力。
```
控制台也可以作为配置中心使用,可设置全局属性源、组件属性源:
1)全局属性源可以覆盖系统配置和其他配置的属性值;
2)支持组件属性源全局配置,如:Redis全局配置使用A集群(即使应用使用的是B集群);
3)组件属性源属性通配符支持,如:假设要将所有应用的redis由集群改为单机则可以使用如下配置:
# 通配符设置,将cluster的配置全部清空。
# 这里不需要指定应用服务配置的属性前缀,可自动识别需要替换的配置
redis.cluster.*=
redis.host=xxxx
redis.database=xxxx
redis.password=xxx
...
```
### 二、WEB组件
WEB服务应用的基础组件包,主要核心功能Filter过滤器、异常处理、日志处理等。
```
org.tianyun
tianyun-cloud-web
```
2.1、控制器:BaseController为所有Controller的基类,Controller的出入参参数默认会打印输出,可以通过@EnableLogPrint注解来控制输入输出;
2.2、过滤器:可以直接继承AbstractFilter来自定义过滤器;
2.3、异常处理:BussinessException为业务异常,业务检查时可以抛出此异常来控制流程,SystemException为系统异常,系统出现未知异常时抛出此异常;
2.4 语言地区:可以在请求头中加入x-http-language: en-US,或者在url后面加上lang=en-US来改变语言选项,默认语言地区是zh-CN。
### 三、多数据源配置及使用
数据库连接池使用HikariCP,框架已经对数据源初始化过程进行了封装,只需要作相应配置就可以上手使用。
```
org.tianyun
tianyun-cloud-datasource
```
3.1、多数据+主从,动态路由
支持多数据源和主从配置,可以使用@RoutingWith来动态切换数据源,注解建议写在mapper上,mapper类上不加将使用默认数据源(primary=true),如果启用了事务,同一事务中使用同一个数据源,也就是@RoutingWith注解将会失效。初始化数据源,只需要在bootstrap.yml中加入如下配置即可:
```
datasource:
client-type: routing
routing:
db0:
primary: true
props:
cachePrepStmts: true
useServerPrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
master:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.1.1:3306/test
username: test
password: 123456
maximum-pool-size: 30
connection-timeout: 60000
props:
cachePrepStmts: true
useServerPrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
slave:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.1.1:3307/test
username: test
password: 123456
maximum-pool-size: 30
connection-timeout: 60000
```
如果要支持多个数据源只需要在代码中加入如下配置,并在Mapper或Service上使用@RoutingWith来指定数据源,如不指定将使用默认数据源(primary设置为true的数据源):
```
@DataSourceContainer(prefix = "datasource.routing.custom", datasource = "customDataSource")
public DataSourceFactoryProperties dataSourceFactoryProperties() {
return new DataSourceFactoryProperties();
}
```
参数说明:
```
datasource.routing,动态路由数据源,可配置多个数据源。
datasource.routing.common,默认数据源自动装配前缀。
datasource.routing.common.primary,设置为true,表示为默认数据源,只能有一个数据源设置为true。
datasource.routing.common.master,主库数据源。
datasource.routing.common.slave,从库数据源。
```
3.2、分片数据源
使用sharding-sphere,配置分库分表,需要编写自定义分片规则。
sharding和routing两个数据源只能二选一,可以加上配置:datasource.client-type=sharding,默认为routing。
```
datasource:
client-type: sharding
sharding:
nodes:
db0:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.1.1:3306/test01
username: test
password: 123456
maximum-pool-size: 30
connection-timeout: 60000
db1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.1.1:3306/test02
username: test
password: 123456
maximum-pool-size: 30
connection-timeout: 60000
props:
cachePrepStmts: true
useServerPrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
props:
sql.show: true
cachePrepStmts: true
useServerPrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
rule: (自定义分片规则,替换此部分)
```
### 四、Mybatis
添加了相应的插件,如:MybatisPagePlugin、RequestContextPlugin,mapper扫描路径默认为:classpath*:mapper/**/*.xml。
```
org.tianyun
tianyun-cloud-mybatis
```
4.1、RequestContextPlugin,可直接在mapper.xml中访问RequestContext,变量名为context,例如:#{context.loginUserId};
4.2、MybatisPagePlugin,分页插件目前支持的数据库包括:Db2、HerdDB、Mysql、Hsqldb、Oracle9i、Oracle、SqlServer2012;
```
PageBuilder pb = PageHelper.startPage(PageRequest);
// 查询列表,不需要写分页sql
List result = ...;
pb.setResult(result);
PageResult result = pb.build();
```
### 五、Redis
组件对Redis进行了封装,简化了Redis的初始化过程,以及增加了常用操作命令的支持,例如:分布式锁、Cache等。使用几个注解就可以完成一系列操作。
```
org.tianyun
tianyun-cloud-data-redis
```
5.1、使用@RedisContainer注解初始化Redis连接,示例:
```
@RedisContainer(prefix = "redis.customer", connectionFactory = "customRedisConnectionFactory")
public RedisContainerProperties customRedisContainerProperties() {
return new RedisContainerProperties();
}
```
注意:框架会自动默认初始化RedisConnectionFactory,装备配置前缀为reids.common,直接配置即可,如果应用需要初始化两个RedisConnectionFactory,则按上面方式进行配置,参数配置请参考spring.redis。
5.2、Cache常用注解:@SetCache、@FlushCache、@TryLock,缓存key变量可以使用{0},
{1}...{N}占位,使用SpEL表达式来获取动态变量值,也可以直接在代码中注入ObjectRedisTemplate。
### 六、消息组件
目前仅支持rabbitmq,其他消息组件待后续支持。
```
org.tianyun
tianyun-cloud-data-redis
```
6.1、使用@RabbitContainer注解初始化连接,示例:
```
@RabbitContainer(prefix = "rabbitmq.custom", connectionFactory = "customRabbitConnectionFactory", containerFactory = "customRabbitListenerContainerFactory", rabbitTemplate = "customRabbitTemplate")
public RabbitMqProperties customRabbitMqProperties() {
return new RabbitMqProperties();
}
```
注意:框架会自动默认初始化ConnectionFactory,装备配置前缀为rabbitmq.common,直接配置即可,如果应用需要初始化两个ConnectionFactory,则按上面方式初始化即可。
6.2、消息生产方可以使用@RabbitProducer注解,定义在接口上即可,接口无需写实现类,自动创建代理对象(Jdk动态代理),直接注入即可使用。
```
/**
* 消息生产者示例
*
* @auther ebert_chan
*/
@RabbitProducer(exchange = "exchange.test")
public interface RabbitProducerSample {
/**
* 发送消息
*
* @param msg
* 消息体
* @auther ebert_chan
*/
@RoutingKey("test.send")
void send(Object msg);
/**
* 发送延迟消息
*
* @param msg
* 消息体
* @param delayTime
* 延时时间,单位秒
* @auther ebert_chan
*/
@RoutingKey("test.delay-message")
void sendDelayMessage(Object msg, Integer delayTime);
}
```
### 七、注册中心&配置中心
```
org.tianyun
tianyun-cloud-nacos-discovery
org.tianyun
tianyun-cloud-nacos-config
```
## 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
5. 发送给仓库管理员审核代码