├── README.md
├── pom.xml
├── spring-boot-dubbo-plugin
├── pom.xml
└── src
│ └── main
│ └── java
│ └── cn
│ └── kiiwii
│ └── framework
│ └── dubbo
│ ├── AnnotationBeanConfiguration.java
│ ├── DubboProperties.java
│ ├── consumer
│ └── DubboComsumerConfiguration.java
│ └── provider
│ └── DubboProviderConfiguration.java
├── spring-boot-with-druid
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── cn
│ │ └── kiiwii
│ │ └── framework
│ │ ├── Application.java
│ │ ├── aop
│ │ ├── ServiceAop.java
│ │ └── WebAspect.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── dao
│ │ ├── ITestDAO.java
│ │ └── impl
│ │ │ └── TestDAOImpl.java
│ │ ├── druid
│ │ ├── DruidConfiguration.java
│ │ └── DruidDataSourceProperties.java
│ │ └── service
│ │ ├── ITestService.java
│ │ └── impl
│ │ └── TestServiceImpl.java
│ ├── resources
│ ├── application.properties
│ └── log4j.properties
│ └── script
│ └── test.sql
├── spring-boot-with-dubbo
├── dobbo-consumer
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── cn
│ │ │ └── kiiwii
│ │ │ └── framework
│ │ │ ├── Application.java
│ │ │ └── controller
│ │ │ └── UserController.java
│ │ └── resources
│ │ ├── application-dev.properties
│ │ ├── application-test.properties
│ │ ├── application.properties
│ │ └── log4j.properties
├── dobbo-interface
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── cn
│ │ └── kiiwii
│ │ └── framework
│ │ └── dubbo
│ │ └── api
│ │ └── IPerson.java
├── dobbo-provider
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── cn
│ │ │ └── kiiwii
│ │ │ └── framework
│ │ │ └── dubbo
│ │ │ └── provider
│ │ │ ├── Application.java
│ │ │ ├── dao
│ │ │ ├── ITestDAO.java
│ │ │ └── impl
│ │ │ │ └── TestDAOImpl.java
│ │ │ ├── druid
│ │ │ ├── DruidConfiguration.java
│ │ │ └── DruidDataSourceProperties.java
│ │ │ └── service
│ │ │ └── PersonServiceImpl.java
│ │ └── resources
│ │ ├── application.properties
│ │ └── log4j.properties
└── pom.xml
├── spring-boot-with-dynamic-datasource
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── cn
│ │ └── kiiwii
│ │ └── framework
│ │ ├── Application.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── dao
│ │ ├── ITestDAO.java
│ │ └── impl
│ │ │ └── TestDAOImpl.java
│ │ ├── druid
│ │ ├── DruidConfiguration.java
│ │ └── DynamicDataSource
│ │ │ ├── DynamicDataSource.java
│ │ │ ├── DynamicDataSourceAspect.java
│ │ │ ├── DynamicDataSourceContextHolder.java
│ │ │ ├── DynamicDataSourceRegister.java
│ │ │ ├── MProxyTransactionManagementConfiguration.java
│ │ │ ├── MTransactionInterceptor.java
│ │ │ └── TargetDataSource.java
│ │ └── service
│ │ ├── ITestService.java
│ │ └── impl
│ │ └── TestServiceImpl.java
│ ├── resources
│ ├── application.properties
│ └── log4j.properties
│ └── script
│ └── test.sql
├── spring-boot-with-freemarker
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── cn
│ │ └── kiiwii
│ │ └── framework
│ │ └── freemarker
│ │ ├── Application.java
│ │ ├── controller
│ │ ├── ApiController.java
│ │ └── WebController.java
│ │ ├── model
│ │ └── User.java
│ │ └── service
│ │ ├── IApiService.java
│ │ └── impl
│ │ └── ApiServiceImpl.java
│ └── resources
│ ├── application-dev.properties
│ ├── application-prod.properties
│ ├── application-test.properties
│ ├── application.properties
│ ├── log4j.properties
│ ├── static
│ └── bootstrap
│ │ ├── css
│ │ ├── bootstrap-override.css
│ │ ├── bootstrap.css
│ │ └── bootstrap.min.css
│ │ ├── fonts
│ │ └── bootstrap
│ │ │ ├── glyphicons-halflings-regular.eot
│ │ │ ├── glyphicons-halflings-regular.svg
│ │ │ ├── glyphicons-halflings-regular.ttf
│ │ │ ├── glyphicons-halflings-regular.woff
│ │ │ └── glyphicons-halflings-regular.woff2
│ │ └── js
│ │ ├── bootstrap.js
│ │ └── bootstrap.min.js
│ └── templates
│ └── index.html
├── spring-boot-with-hibernate
├── README.md
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── cn
│ │ └── kiiwii
│ │ └── framework
│ │ └── hibernate
│ │ ├── Application.java
│ │ ├── aop
│ │ └── ServiceAop.java
│ │ ├── conf
│ │ ├── ApplicationConfiguration.java
│ │ ├── DruidDataSourceProperties.java
│ │ └── HibernatePropertes.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── dao
│ │ ├── ITestDAO.java
│ │ └── impl
│ │ │ └── TestDAOImpl.java
│ │ ├── model
│ │ ├── SpringBootWithHinernateTest.java
│ │ └── SpringBootWithHinernateTest2.java
│ │ └── service
│ │ ├── ITestService.java
│ │ └── impl
│ │ └── TestServiceImpl.java
│ ├── resources
│ ├── application.properties
│ └── log4j.properties
│ └── script
│ └── init.sql
├── spring-boot-with-jpa
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── cn
│ │ └── kiiwii
│ │ └── framework
│ │ ├── Application.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── dao
│ │ ├── ITestDAO.java
│ │ └── TestRepository.java
│ │ ├── druid
│ │ └── DruidConfiguration.java
│ │ └── model
│ │ └── Test.java
│ ├── resources
│ ├── application.properties
│ └── log4j.properties
│ └── script
│ └── test.sql
├── spring-boot-with-mybatis
├── README.md
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── cn
│ │ │ └── kiiwii
│ │ │ └── framework
│ │ │ └── mybatis
│ │ │ ├── Application.java
│ │ │ ├── aop
│ │ │ └── ServiceAop.java
│ │ │ ├── controller
│ │ │ └── TestController.java
│ │ │ ├── druid
│ │ │ └── DruidConfiguration.java
│ │ │ ├── mapper
│ │ │ ├── TestMapper.java
│ │ │ └── TestXmlMapper.java
│ │ │ ├── mapping
│ │ │ └── UserMapper.xml
│ │ │ ├── model
│ │ │ └── Account.java
│ │ │ └── service
│ │ │ ├── ITestService.java
│ │ │ ├── ITestXmlService.java
│ │ │ └── impl
│ │ │ ├── TestServiceImpl.java
│ │ │ └── TestXmlServiceImpl.java
│ ├── resources
│ │ ├── application.properties
│ │ └── log4j.properties
│ └── script
│ │ └── test.sql
│ └── test
│ └── java
│ └── cn
│ └── kiiwii
│ └── framework
│ └── mybatis
│ └── TestServiceTest.java
└── spring-boot-with-schedule
├── pom.xml
└── src
├── main
├── java
│ └── cn
│ │ └── kiiwii
│ │ └── framework
│ │ ├── Application.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── dao
│ │ ├── ITestDAO.java
│ │ └── impl
│ │ │ └── TestDAOImpl.java
│ │ ├── druid
│ │ └── DruidConfiguration.java
│ │ ├── schedule
│ │ └── Scheduling.java
│ │ └── service
│ │ ├── ITestService.java
│ │ └── impl
│ │ └── TestServiceImpl.java
├── resources
│ ├── application.properties
│ └── log4j.properties
└── script
│ └── test.sql
└── test
└── java
└── cn
└── kiiwii
└── framework
└── AppTest.java
/README.md:
--------------------------------------------------------------------------------
1 | # spring-boot druid mybatis schedule dynamic-datasource jpa
2 |
3 | 本系列是spring-boot相关的一些列子,比如spring-boot集成druid,以及druid的动态数据源切换,
4 | spring-boot 集成mybatis,spring-boot集成定时器等等
5 |
6 | # 1、spring-boot集成druid数据库连接池
7 |
8 | 详情查看项目 [spring-boot-with-druid](spring-boot-with-druid/)
9 |
10 | # 2、spring-boot实现druid数据库连接池的动态数据源切换,
11 |
12 | 详情查看项目 [spring-boot-with-dynamic-datasource](spring-boot-with-dynamic-datasource/)
13 |
14 | # 3、spring-boot实现定时任务
15 |
16 | 详情查看项目 [spring-boot-with-schedule](spring-boot-with-schedule/)
17 |
18 | # 4、spring-boot集成mybatis
19 |
20 | 详情查看项目 [spring-boot-with-mybatis](spring-boot-with-mybatis/)
21 |
22 | # 5、spring-boot集成jpa
23 |
24 | 详情查看项目 [spring-boot-with-jpa](spring-boot-with-jpa/)
25 |
26 | # 6、spring-boot集成hibernate(dao继承HibernateDaoSupport)
27 |
28 | 详情查看项目 [spring-boot-with-hibernate](spring-boot-with-hibernate/)
29 |
30 | # 7、spring-boot集成dubbo,所有配置均使用application.properties
31 |
32 | 详情查看项目 [spring-boot-with-dubbo](spring-boot-with-dubbo/)
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | cn.kiiwii.framework
6 | spring-boot-sample
7 | 1.0-SNAPSHOT
8 |
9 | spring-boot-with-schedule
10 | spring-boot-with-hibernate
11 | spring-boot-with-dynamic-datasource
12 | spring-boot-with-druid
13 | spring-boot-with-dubbo
14 | spring-boot-with-jpa
15 | spring-boot-with-mybatis
16 | spring-boot-with-druid-dpcp
17 | spring-boot-dubbo-plugin
18 | spring-boot-with-freemarker
19 |
20 | pom
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-parent
25 | 1.4.0.RELEASE
26 |
27 |
28 | spring-boot-sample
29 | http://maven.apache.org
30 |
31 |
32 | UTF-8
33 |
34 |
35 |
36 |
37 | junit
38 | junit
39 | 4.12
40 | test
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/spring-boot-dubbo-plugin/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-boot-sample
7 | cn.kiiwii.framework
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | 1.0.0-RELEASE
13 | spring-boot-dubbo-plugin
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot
19 | 1.4.0.RELEASE
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-configuration-processor
24 | true
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-autoconfigure
29 | 1.4.0.RELEASE
30 |
31 |
32 | org.apache.zookeeper
33 | zookeeper
34 | 3.4.8
35 |
36 |
37 | com.101tec
38 | zkclient
39 | 0.9
40 |
41 |
42 | com.alibaba
43 | dubbo
44 | 2.8.4
45 |
46 |
47 | org.jboss.netty
48 | netty
49 |
50 |
51 | spring
52 | org.springframework
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/spring-boot-dubbo-plugin/src/main/java/cn/kiiwii/framework/dubbo/AnnotationBeanConfiguration.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.dubbo;
2 |
3 | import com.alibaba.dubbo.config.spring.AnnotationBean;
4 | import org.springframework.beans.factory.annotation.Value;
5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 | import org.springframework.context.annotation.PropertySource;
10 |
11 | /**
12 | * Created by zhong on 2017/3/13.
13 | */
14 | @Configuration
15 | @ConditionalOnMissingClass
16 | @PropertySource(value = "classpath:/application.properties")
17 | public class AnnotationBeanConfiguration {
18 |
19 | @Bean
20 | @ConditionalOnMissingBean
21 | public AnnotationBean annotationBean(@Value("${dubbo.annotation.package-name}") String packageName) {
22 | AnnotationBean annotationBean = new AnnotationBean();
23 | annotationBean.setPackage(packageName);
24 | return annotationBean;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/spring-boot-dubbo-plugin/src/main/java/cn/kiiwii/framework/dubbo/consumer/DubboComsumerConfiguration.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.dubbo.consumer;
2 |
3 | import cn.kiiwii.framework.dubbo.DubboProperties;
4 | import com.alibaba.dubbo.config.ApplicationConfig;
5 | import com.alibaba.dubbo.config.ConsumerConfig;
6 | import com.alibaba.dubbo.config.RegistryConfig;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
9 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 |
13 | /**
14 | * Created by zhong on 2016/11/22.
15 | */
16 | @Configuration
17 | @EnableConfigurationProperties({DubboProperties.class})
18 | public class DubboComsumerConfiguration {
19 |
20 | @Autowired
21 | private DubboProperties dubboProperties;
22 |
23 | /**
24 | * 注入dubbo上下文
25 | *
26 | * @return
27 | */
28 | @Bean
29 | @ConditionalOnMissingBean
30 | public ApplicationConfig applicationConfig(RegistryConfig registryConfig) {
31 | // 当前应用配置
32 | ApplicationConfig applicationConfig = new ApplicationConfig();
33 | applicationConfig.setName(dubboProperties.getApplication().getName());
34 | applicationConfig.setVersion(dubboProperties.getApplication().getVersion());
35 | applicationConfig.setArchitecture(dubboProperties.getApplication().getArchitecture());
36 | applicationConfig.setCompiler(dubboProperties.getApplication().getCompiler());
37 | applicationConfig.setDefault(dubboProperties.getApplication().getDefault());
38 | applicationConfig.setEnvironment(dubboProperties.getApplication().getEnvironment());
39 | applicationConfig.setLogger(dubboProperties.getApplication().getLogger());
40 | applicationConfig.setMonitor(dubboProperties.getApplication().getArchitecture());
41 | applicationConfig.setOrganization(dubboProperties.getApplication().getOrganization());
42 | applicationConfig.setOwner(dubboProperties.getApplication().getOwner());
43 | applicationConfig.setRegistry(registryConfig);
44 | return applicationConfig;
45 | }
46 |
47 | /**
48 | * 注入dubbo注册中心配置,基于zookeeper
49 | *
50 | * @return
51 | */
52 | @Bean
53 | @ConditionalOnMissingBean
54 | public RegistryConfig registryConfig() {
55 | // 连接注册中心配置
56 | RegistryConfig registry = new RegistryConfig();
57 | registry.setProtocol(dubboProperties.getRegistry().getProtocol());
58 | registry.setAddress(dubboProperties.getRegistry().getAddress());
59 | registry.setVersion(dubboProperties.getRegistry().getVersion());
60 | registry.setUsername(dubboProperties.getRegistry().getUsername());
61 | registry.setPassword(dubboProperties.getRegistry().getPassword());
62 | registry.setGroup(dubboProperties.getRegistry().getGroup());
63 | registry.setCheck(dubboProperties.getRegistry().getCheck());
64 | registry.setClient(dubboProperties.getRegistry().getClient());
65 | registry.setCluster(dubboProperties.getRegistry().getCluster());
66 | registry.setDefault(dubboProperties.getRegistry().getDefault());
67 | registry.setDynamic(dubboProperties.getRegistry().getDynamic());
68 | registry.setFile(dubboProperties.getRegistry().getFile());
69 | registry.setPort(dubboProperties.getRegistry().getPort());
70 | registry.setRegister(dubboProperties.getRegistry().getRegister());
71 | registry.setServer(dubboProperties.getRegistry().getServer());
72 | registry.setSubscribe(dubboProperties.getRegistry().getSubscribe());
73 | registry.setTimeout(dubboProperties.getRegistry().getTimeout());
74 | registry.setTransporter(dubboProperties.getRegistry().getTransporter());
75 | return registry;
76 | }
77 |
78 | /**
79 | * dubbo消费
80 | *
81 | * @param applicationConfig
82 | * @param registryConfig
83 | * @return
84 | */
85 | @Bean(name="defaultConsumer")
86 | @ConditionalOnMissingBean
87 | public ConsumerConfig consumerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
88 | ConsumerConfig consumerConfig = new ConsumerConfig();
89 | consumerConfig.setApplication(applicationConfig);
90 | consumerConfig.setRegistry(registryConfig);
91 | consumerConfig.setOwner(dubboProperties.getConsumer().getOwner());
92 | consumerConfig.setMonitor(dubboProperties.getConsumer().getMonitor());
93 | consumerConfig.setDefault(dubboProperties.getConsumer().getDefault());
94 | consumerConfig.setTimeout(dubboProperties.getConsumer().getTimeout());
95 | consumerConfig.setActives(dubboProperties.getConsumer().getActives());
96 | consumerConfig.setAsync(dubboProperties.getConsumer().getAsync());
97 | consumerConfig.setCache(dubboProperties.getConsumer().getCache());
98 | consumerConfig.setCallbacks(dubboProperties.getConsumer().getCallbacks());
99 | consumerConfig.setCheck(dubboProperties.getConsumer().getCheck());
100 | consumerConfig.setCluster(dubboProperties.getConsumer().getCluster());
101 | consumerConfig.setConnections(dubboProperties.getConsumer().getConnections());
102 | consumerConfig.setFilter(dubboProperties.getConsumer().getFilter());
103 | consumerConfig.setGeneric(dubboProperties.getConsumer().getGeneric());
104 | consumerConfig.setGroup(dubboProperties.getConsumer().getGroup());
105 | consumerConfig.setVersion(dubboProperties.getConsumer().getVersion());
106 | return consumerConfig;
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/spring-boot-dubbo-plugin/src/main/java/cn/kiiwii/framework/dubbo/provider/DubboProviderConfiguration.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.dubbo.provider;
2 |
3 | import cn.kiiwii.framework.dubbo.DubboProperties;
4 | import com.alibaba.dubbo.config.*;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
7 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
8 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.Configuration;
11 | import org.springframework.core.annotation.Order;
12 |
13 | /**
14 | * Created by zhong on 2016/11/22.
15 | */
16 | @Configuration
17 | @EnableConfigurationProperties({DubboProperties.class})
18 | @ConditionalOnMissingClass
19 | @Order(value = -1)
20 | public class DubboProviderConfiguration {
21 |
22 | @Autowired
23 | private DubboProperties dubboProperties;
24 |
25 | /**
26 | * 注入dubbo注册中心配置,基于zookeeper
27 | * @return
28 | */
29 | @Bean
30 | @ConditionalOnMissingBean
31 | public RegistryConfig registryConfig() {
32 | // 连接注册中心配置
33 | RegistryConfig registry = new RegistryConfig();
34 | registry.setProtocol(dubboProperties.getRegistry().getProtocol());
35 | registry.setAddress(dubboProperties.getRegistry().getAddress());
36 | registry.setVersion(dubboProperties.getRegistry().getVersion());
37 | registry.setUsername(dubboProperties.getRegistry().getUsername());
38 | registry.setPassword(dubboProperties.getRegistry().getPassword());
39 | registry.setGroup(dubboProperties.getRegistry().getGroup());
40 | registry.setCheck(dubboProperties.getRegistry().getCheck());
41 | registry.setClient(dubboProperties.getRegistry().getClient());
42 | registry.setCluster(dubboProperties.getRegistry().getCluster());
43 | registry.setDefault(dubboProperties.getRegistry().getDefault());
44 | registry.setDynamic(dubboProperties.getRegistry().getDynamic());
45 | registry.setFile(dubboProperties.getRegistry().getFile());
46 | registry.setPort(dubboProperties.getRegistry().getPort());
47 | registry.setRegister(dubboProperties.getRegistry().getRegister());
48 | registry.setServer(dubboProperties.getRegistry().getServer());
49 | registry.setSubscribe(dubboProperties.getRegistry().getSubscribe());
50 | registry.setTimeout(dubboProperties.getRegistry().getTimeout());
51 | registry.setTransporter(dubboProperties.getRegistry().getTransporter());
52 | return registry;
53 | }
54 |
55 | /* *
56 | * 注入dubbo上下文
57 | *
58 | * @return
59 | */
60 | @Bean
61 | @ConditionalOnMissingBean
62 | public ApplicationConfig applicationConfig(RegistryConfig registryConfig) {
63 | // 当前应用配置
64 | ApplicationConfig applicationConfig = new ApplicationConfig();
65 | applicationConfig.setName(dubboProperties.getApplication().getName());
66 | applicationConfig.setVersion(dubboProperties.getApplication().getVersion());
67 | applicationConfig.setArchitecture(dubboProperties.getApplication().getArchitecture());
68 | applicationConfig.setCompiler(dubboProperties.getApplication().getCompiler());
69 | applicationConfig.setDefault(dubboProperties.getApplication().getDefault());
70 | applicationConfig.setEnvironment(dubboProperties.getApplication().getEnvironment());
71 | applicationConfig.setLogger(dubboProperties.getApplication().getLogger());
72 | applicationConfig.setMonitor(dubboProperties.getApplication().getArchitecture());
73 | applicationConfig.setOrganization(dubboProperties.getApplication().getOrganization());
74 | applicationConfig.setOwner(dubboProperties.getApplication().getOwner());
75 | applicationConfig.setRegistry(registryConfig);
76 | return applicationConfig;
77 | }
78 |
79 | /* *
80 | * 默认基于dubbo协议提供服务
81 | *
82 | * @return
83 |
84 | */
85 | @Bean
86 | @ConditionalOnMissingBean
87 | public ProtocolConfig protocolConfig() {
88 | // 服务提供者协议配置
89 | ProtocolConfig protocolConfig = new ProtocolConfig();
90 | protocolConfig.setName(dubboProperties.getProtocol().getName());
91 | protocolConfig.setPort(dubboProperties.getProtocol().getPort());
92 | protocolConfig.setAccepts(dubboProperties.getProtocol().getAccepts());
93 | protocolConfig.setAccesslog(dubboProperties.getProtocol().getAccesslog());
94 | protocolConfig.setBuffer(dubboProperties.getProtocol().getBuffer());
95 | protocolConfig.setCharset(dubboProperties.getProtocol().getCharset());
96 | protocolConfig.setClient(dubboProperties.getProtocol().getClient());
97 | protocolConfig.setCodec(dubboProperties.getProtocol().getCodec());
98 | protocolConfig.setContextpath(dubboProperties.getProtocol().getContextpath());
99 | protocolConfig.setDefault(dubboProperties.getProtocol().getDefault());
100 | protocolConfig.setDispatcher(dubboProperties.getProtocol().getDispatcher());
101 | protocolConfig.setExchanger(dubboProperties.getProtocol().getExchanger());
102 | protocolConfig.setExtension(dubboProperties.getProtocol().getExtension());
103 | protocolConfig.setHeartbeat(dubboProperties.getProtocol().getHeartbeat());
104 | protocolConfig.setHost(dubboProperties.getProtocol().getHost());
105 | protocolConfig.setIothreads(dubboProperties.getProtocol().getIothreads());
106 | protocolConfig.setKeepAlive(dubboProperties.getProtocol().getKeepAlive());
107 | protocolConfig.setNetworker(dubboProperties.getProtocol().getNetworker());
108 | protocolConfig.setOptimizer(dubboProperties.getProtocol().getOptimizer());
109 | protocolConfig.setPayload(dubboProperties.getProtocol().getPayload());
110 | protocolConfig.setPrompt(dubboProperties.getProtocol().getPrompt());
111 | protocolConfig.setQueues(dubboProperties.getProtocol().getQueues());
112 | protocolConfig.setRegister(dubboProperties.getProtocol().getRegister());
113 | protocolConfig.setSerialization(dubboProperties.getProtocol().getSerialization());
114 | protocolConfig.setServer(dubboProperties.getProtocol().getServer());
115 | protocolConfig.setStatus(dubboProperties.getProtocol().getStatus());
116 | protocolConfig.setTelnet(dubboProperties.getProtocol().getTelnet());
117 | protocolConfig.setThreadpool(dubboProperties.getProtocol().getThreadpool());
118 | protocolConfig.setThreads(dubboProperties.getProtocol().getThreads());
119 | protocolConfig.setTransporter(dubboProperties.getProtocol().getTransporter());
120 | return protocolConfig;
121 | }
122 |
123 | /* *
124 | * dubbo服务提供
125 | *
126 | * @param applicationConfig
127 | * @param registryConfig
128 | * @param protocolConfig
129 | * @return
130 |
131 | */
132 | @Bean
133 | @ConditionalOnMissingBean
134 | public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ProtocolConfig protocolConfig) {
135 | ProviderConfig providerConfig = new ProviderConfig();
136 | providerConfig.setTimeout(3000);
137 | providerConfig.setRetries(3);
138 | providerConfig.setDelay(300);
139 | providerConfig.setApplication(applicationConfig);
140 | providerConfig.setRegistry(registryConfig);
141 | providerConfig.setProtocol(protocolConfig);
142 | return providerConfig;
143 | }
144 |
145 | @Bean
146 | @ConditionalOnMissingBean
147 | public MonitorConfig monitorConfig() {
148 | MonitorConfig monitorConfig = new MonitorConfig();
149 | monitorConfig.setAddress(dubboProperties.getMonitor().getAddress());
150 | monitorConfig.setGroup(dubboProperties.getMonitor().getGroup());
151 | monitorConfig.setPassword(dubboProperties.getMonitor().getPassword());
152 | monitorConfig.setProtocol(dubboProperties.getMonitor().getProtocol());
153 | monitorConfig.setUsername(dubboProperties.getMonitor().getUsername());
154 | monitorConfig.setVersion(dubboProperties.getMonitor().getVersion());
155 | monitorConfig.setDefault(dubboProperties.getMonitor().getDefault());
156 | return monitorConfig;
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/spring-boot-with-druid/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-boot-sample
7 | cn.kiiwii.framework
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | spring-boot-with-druid
13 |
14 |
15 |
16 | junit
17 | junit
18 | 4.12
19 | test
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-web
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-logging
32 |
33 |
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-configuration-processor
38 | true
39 |
40 |
41 | org.springframework.boot
42 | spring-boot-starter-jdbc
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-log4j
47 | 1.3.7.RELEASE
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-starter-aop
52 |
53 |
54 | mysql
55 | mysql-connector-java
56 | 5.1.38
57 |
58 |
59 | com.alibaba
60 | druid
61 | 1.0.25
62 |
63 |
64 | org.springframework
65 | spring-context
66 | 4.3.2.RELEASE
67 |
68 |
69 | org.springframework
70 | spring-orm
71 | 4.3.2.RELEASE
72 |
73 |
74 |
75 |
76 |
77 |
78 | org.apache.maven.plugins
79 | maven-compiler-plugin
80 | 3.2
81 |
82 | 1.7
83 | 1.7
84 |
85 |
86 |
87 | org.springframework.boot
88 | spring-boot-maven-plugin
89 |
90 |
91 |
92 | repackage
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/spring-boot-with-druid/src/main/java/cn/kiiwii/framework/Application.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
10 | import org.springframework.boot.builder.SpringApplicationBuilder;
11 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
12 | import org.springframework.boot.web.support.SpringBootServletInitializer;
13 | import org.springframework.context.annotation.Bean;
14 | import org.springframework.context.annotation.Configuration;
15 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
16 | import org.springframework.transaction.PlatformTransactionManager;
17 | import org.springframework.transaction.annotation.EnableTransactionManagement;
18 | import org.springframework.web.bind.annotation.RestController;
19 |
20 | import javax.sql.DataSource;
21 |
22 | /**
23 | * Created by zhong on 2016/11/10.
24 | */
25 | @RestController
26 | @SpringBootApplication
27 | @EnableAutoConfiguration
28 | @ImportAutoConfiguration
29 | @EnableTransactionManagement
30 | @EnableConfigurationProperties
31 | @Configuration
32 | public class Application extends SpringBootServletInitializer {
33 |
34 | static Logger logger = LoggerFactory.getLogger(Application.class);
35 |
36 | @Bean
37 | @ConditionalOnMissingBean
38 | public PlatformTransactionManager txManager(DataSource dataSource){
39 | return new DataSourceTransactionManager(dataSource);
40 | }
41 |
42 | @Override
43 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
44 | return application.sources(Application.class);
45 | }
46 |
47 | public static void main(String[] args) throws Exception {
48 | SpringApplication.run(Application.class, args);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/spring-boot-with-druid/src/main/java/cn/kiiwii/framework/aop/ServiceAop.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.aop;
2 |
3 | import org.aspectj.lang.JoinPoint;
4 | import org.aspectj.lang.ProceedingJoinPoint;
5 | import org.aspectj.lang.annotation.*;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.stereotype.Component;
10 | import org.springframework.web.context.request.RequestContextHolder;
11 | import org.springframework.web.context.request.ServletRequestAttributes;
12 | import org.springframework.web.servlet.HandlerMapping;
13 |
14 | import javax.servlet.http.HttpServletRequest;
15 | import java.util.Map;
16 |
17 | /**
18 | * Created by zhong on 2016/11/24.
19 | */
20 | @Aspect
21 | @Component
22 | public class ServiceAop {
23 | private static Logger logger = LoggerFactory.getLogger(ServiceAop.class);
24 |
25 | /* private ThreadLocal tlocal = new ThreadLocal();
26 |
27 | @Autowired
28 | private OptLogService optLogService;*/
29 |
30 | @Pointcut("execution(public * cn.kiiwii.framework.service.impl.*.*(..))")
31 | public void webRequestLog() {}
32 |
33 | @Before("webRequestLog()")
34 | public void doBefore(JoinPoint joinPoint) {
35 | ThreadLocal threadLocal;
36 | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
37 | HttpServletRequest request = attributes.getRequest();
38 | String beanName = joinPoint.getSignature().getDeclaringTypeName();
39 | String methodName = joinPoint.getSignature().getName();
40 | String uri = request.getRequestURI();
41 | String remoteAddr = getIpAddr(request);
42 | String sessionId = request.getSession().getId();
43 | String user = (String) request.getSession().getAttribute("user");
44 | String method = request.getMethod();
45 | String params = "";
46 | /*try {
47 |
48 | long beginTime = System.currentTimeMillis();
49 |
50 | // 接收到请求,记录请求内容
51 | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
52 | HttpServletRequest request = attributes.getRequest();
53 | String beanName = joinPoint.getSignature().getDeclaringTypeName();
54 | String methodName = joinPoint.getSignature().getName();
55 | String uri = request.getRequestURI();
56 | String remoteAddr = getIpAddr(request);
57 | String sessionId = request.getSession().getId();
58 | String user = (String) request.getSession().getAttribute("user");
59 | String method = request.getMethod();
60 | String params = "";
61 | if ("POST".equals(method)) {
62 | Object[] paramsArray = joinPoint.getArgs();
63 | params = argsArrayToString(paramsArray);
64 | } else {
65 | Map, ?> paramsMap = (Map, ?>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
66 | params = paramsMap.toString();
67 | }
68 |
69 | logger.debug("uri=" + uri + "; beanName=" + beanName + "; remoteAddr=" + remoteAddr + "; user=" + user
70 | + "; methodName=" + methodName + "; params=" + params);
71 |
72 | OperatorLog optLog = new OperatorLog();
73 | optLog.setBeanName(beanName);
74 | optLog.setCurUser(user);
75 | optLog.setMethodName(methodName);
76 | optLog.setParams(params != null ? params.toString() : "");
77 | optLog.setRemoteAddr(remoteAddr);
78 | optLog.setSessionId(sessionId);
79 | optLog.setUri(uri);
80 | optLog.setRequestTime(beginTime);
81 | tlocal.set(optLog);
82 |
83 | } catch (Exception e) {
84 | logger.error("***操作请求日志记录失败doBefore()***", e);
85 | }*/
86 | }
87 |
88 | @AfterReturning(returning = "result", pointcut = "webRequestLog()")
89 | public void doAfterReturning(Object result) {
90 | /*try {
91 | // 处理完请求,返回内容
92 | OperatorLog optLog = tlocal.get();
93 | optLog.setResult(result.toString());
94 | long beginTime = optLog.getRequestTime();
95 | long requestTime = (System.currentTimeMillis() - beginTime) / 1000;
96 | optLog.setRequestTime(requestTime);
97 |
98 | System.out.println("请求耗时:" + requestTime + optLog.getUri() + " ** " + optLog.getParams() + " ** "
99 | + optLog.getMethodName());
100 | System.out.println("RESPONSE : " + result);
101 |
102 | optLogService.saveLog(optLog);
103 | } catch (Exception e) {
104 | logger.error("***操作请求日志记录失败doAfterReturning()***", e);
105 | }*/
106 | }
107 |
108 | @Around("webRequestLog()")
109 | public Object around(ProceedingJoinPoint pjp) throws Throwable {
110 | System.out.println("方法环绕start.....");
111 | Object o = null;
112 | System.out.println("before---------------------");
113 | o = pjp.proceed();
114 | System.out.println("after---------------------");
115 | return o;
116 | }
117 |
118 | /**
119 | * 获取登录用户远程主机ip地址
120 | *
121 | * @param request
122 | * @return
123 | */
124 | private String getIpAddr(HttpServletRequest request) {
125 | String ip = request.getHeader("x-forwarded-for");
126 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
127 | ip = request.getHeader("Proxy-Client-IP");
128 | }
129 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
130 | ip = request.getHeader("WL-Proxy-Client-IP");
131 | }
132 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
133 | ip = request.getRemoteAddr();
134 | }
135 | return ip;
136 | }
137 |
138 | /**
139 | * 请求参数拼装
140 | *
141 | * @param paramsArray
142 | * @return
143 | */
144 | private String argsArrayToString(Object[] paramsArray) {
145 | String params = "";
146 | if (paramsArray != null && paramsArray.length > 0) {
147 | for (int i = 0; i < paramsArray.length; i++) {
148 | Object jsonObj = paramsArray[i];
149 | params += jsonObj.toString() + " ";
150 | }
151 | }
152 | return params.trim();
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/spring-boot-with-druid/src/main/java/cn/kiiwii/framework/aop/WebAspect.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.aop;
2 |
3 | import org.aspectj.lang.JoinPoint;
4 | import org.aspectj.lang.ProceedingJoinPoint;
5 | import org.aspectj.lang.annotation.*;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.stereotype.Component;
9 | import org.springframework.web.context.request.RequestContextHolder;
10 | import org.springframework.web.context.request.ServletRequestAttributes;
11 |
12 | import javax.servlet.http.HttpServletRequest;
13 |
14 | /**
15 | * Created by zhong on 2016/11/24.
16 | */
17 | @Aspect
18 | @Component
19 | public class WebAspect {
20 | private static Logger logger = LoggerFactory.getLogger(WebAspect.class);
21 |
22 | @Pointcut("execution(public * cn.kiiwii.framework.controller.*.*(..))")
23 | public void webAspect() {
24 | }
25 |
26 | @Before("webAspect()")
27 | public void doBefore(JoinPoint joinPoint) {
28 | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
29 | HttpServletRequest request = attributes.getRequest();
30 | String beanName = joinPoint.getSignature().getDeclaringTypeName();
31 | String methodName = joinPoint.getSignature().getName();
32 | String uri = request.getRequestURI();
33 | String remoteAddr = getIpAddr(request);
34 | String sessionId = request.getSession().getId();
35 | String user = (String) request.getSession().getAttribute("user");
36 | String method = request.getMethod();
37 | String params = "";
38 | System.out.println(sessionId);
39 | }
40 |
41 | @AfterReturning(returning = "result", pointcut = "webAspect()")
42 | public void doAfterReturning(Object result) {
43 | }
44 |
45 | @Around("webAspect()")
46 | public Object around(ProceedingJoinPoint pjp) throws Throwable {
47 | System.out.println("方法环绕start.....");
48 | Object o = null;
49 | System.out.println("before---------------------");
50 | o = pjp.proceed();
51 | System.out.println("after---------------------");
52 | return o;
53 | }
54 |
55 | /**
56 | * 获取登录用户远程主机ip地址
57 | *
58 | * @param request
59 | * @return
60 | */
61 | private String getIpAddr(HttpServletRequest request) {
62 | String ip = request.getHeader("x-forwarded-for");
63 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
64 | ip = request.getHeader("Proxy-Client-IP");
65 | }
66 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
67 | ip = request.getHeader("WL-Proxy-Client-IP");
68 | }
69 | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
70 | ip = request.getRemoteAddr();
71 | }
72 | return ip;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/spring-boot-with-druid/src/main/java/cn/kiiwii/framework/controller/TestController.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.controller;
2 |
3 | import cn.kiiwii.framework.service.ITestService;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Controller;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | /**
12 | * Created by zhong on 2016/11/10.
13 | */
14 | @RestController
15 | @Controller
16 | public class TestController {
17 | @Autowired
18 | private ITestService testService;
19 | private Logger logger = LoggerFactory.getLogger(this.getClass());
20 |
21 | @RequestMapping("/test")
22 | public String greeting() {
23 | testService.test();
24 | return "hello";
25 | }
26 |
27 | @RequestMapping("/testTrans")
28 | public String testTrans() {
29 | try {
30 | testService.testTransaction();
31 | } catch (Exception e) {
32 | e.printStackTrace();
33 | }
34 | return "testTransaction";
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/spring-boot-with-druid/src/main/java/cn/kiiwii/framework/dao/ITestDAO.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.dao;
2 |
3 | /**
4 | * Created by zhong on 2016/9/5.
5 | */
6 | public interface ITestDAO {
7 | void test();
8 |
9 | void save();
10 | }
11 |
--------------------------------------------------------------------------------
/spring-boot-with-druid/src/main/java/cn/kiiwii/framework/dao/impl/TestDAOImpl.java:
--------------------------------------------------------------------------------
1 | package cn.kiiwii.framework.dao.impl;
2 |
3 | import cn.kiiwii.framework.dao.ITestDAO;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.jdbc.core.JdbcTemplate;
6 | import org.springframework.stereotype.Repository;
7 | import org.springframework.transaction.annotation.Propagation;
8 | import org.springframework.transaction.annotation.Transactional;
9 |
10 | import java.util.List;
11 | import java.util.Map;
12 |
13 | /**
14 | * Created by zhong on 2016/9/5.
15 | */
16 | @Repository("testDAO")
17 | public class TestDAOImpl implements ITestDAO {
18 |
19 | @Autowired
20 | private JdbcTemplate jdbcTemplate;
21 | @Override
22 | public void test() {
23 | List