├── .gitignore
├── README.md
├── method-interceptor
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── example
│ │ └── methodinterceptor
│ │ ├── MethodInterceptorApplication.java
│ │ ├── annotation
│ │ └── InterceptorAnnotation.java
│ │ ├── config
│ │ └── InterceptorConfig.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── interceptor
│ │ └── MyInterceptor.java
│ │ └── service
│ │ ├── MyInterceptorInterface.java
│ │ └── impl
│ │ └── MyInterceptorInterfaceImpl.java
│ └── resources
│ └── application.yml
├── netty-test
├── README.md
├── pom.xml
└── src
│ └── main
│ └── java
│ └── one
│ ├── client
│ ├── TimeClient.java
│ └── TimeClientHandler.java
│ └── server
│ ├── ChildChannelHandler.java
│ ├── DiscardServer.java
│ └── DiscardServerHandler.java
├── springboot-actuator
├── springboot-actuator-admin
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── actuatoradmin
│ │ │ │ └── ActuatorAdminApplication.java
│ │ └── resources
│ │ │ └── application.yml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── actuatoradmin
│ │ └── ActuatorAdminApplicationTests.java
└── springboot-actuator-client
│ ├── pom.xml
│ └── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── client
│ │ │ ├── ClientApplication.java
│ │ │ ├── endpoint
│ │ │ └── FeaturesEndpoint.java
│ │ │ └── timetask
│ │ │ └── TimeTaskTest.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── client
│ └── ClientApplicationTests.java
├── springboot-async
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── example
│ │ └── async
│ │ ├── AsyncApplication.java
│ │ ├── async
│ │ └── AsyncTest.java
│ │ ├── config
│ │ ├── AsyncConfig.java
│ │ └── AsyncTwoConfig.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── handler
│ │ └── AsyncExceptionHandler.java
│ │ ├── service
│ │ ├── AsyncService.java
│ │ └── impl
│ │ │ └── AsyncServiceImpl.java
│ │ └── timetask
│ │ └── TestScheduling.java
│ └── resources
│ └── application.yml
├── springboot-bean
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── gyc
│ │ │ ├── GycApplication.java
│ │ │ ├── bean
│ │ │ ├── AnnotationBean.java
│ │ │ ├── CustomBean.java
│ │ │ ├── LifeCycleBean.java
│ │ │ ├── LifeCycleBeanAware.java
│ │ │ └── LifeCycleProcessor.java
│ │ │ └── config
│ │ │ └── LifeCycleConfig.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── gyc
│ └── GycApplicationTests.java
├── springboot-mybatis
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ ├── config
│ │ │ └── MybatisPageConfig.java
│ │ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ │ └── model
│ │ │ └── User.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── demo
│ └── DemoApplicationTests.java
├── springboot-netty-mybatis
├── .gitignore
├── db
│ └── user.sql
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── gyc
│ │ │ ├── GycApplication.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ │ ├── netty
│ │ │ ├── NettyStart.java
│ │ │ └── ServerHandler.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ └── resources
│ │ ├── application.yml
│ │ └── mapper
│ │ └── UserMapper.xml
│ └── test
│ └── java
│ └── com
│ └── gyc
│ ├── GycApplicationTests.java
│ └── mapper
│ └── UserMapperTest.java
├── springboot-netty-socket
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── nettysocket
│ │ │ ├── NettySocketApplication.java
│ │ │ ├── PushServer.java
│ │ │ ├── cache
│ │ │ └── ClientCache.java
│ │ │ ├── controller
│ │ │ └── PushController.java
│ │ │ └── listenner
│ │ │ └── EventListenner.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── nettysocket
│ └── TestOne.java
├── springboot-netty
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── gyc
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ ├── nettysocket
│ │ │ ├── ChildChannelHandler.java
│ │ │ ├── DiscardServer.java
│ │ │ └── DiscardServerHandler.java
│ │ │ └── service
│ │ │ ├── BaseService.java
│ │ │ └── impl
│ │ │ └── BaseServiceImpl.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── gyc
│ └── demo
│ └── DemoApplicationTests.java
├── springboot-redis
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── redistest
│ │ │ ├── RedisTestApplication.java
│ │ │ ├── TestController.java
│ │ │ └── model
│ │ │ └── User.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── redistest
│ └── RedisTestApplicationTests.java
├── springboot-swagger
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── swagger
│ │ │ └── SwaggerApplication.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── swagger
│ └── SwaggerApplicationTests.java
└── springboot_date
├── .gitignore
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── example
│ │ └── springboot_date
│ │ ├── SpringbootDateApplication.java
│ │ ├── controller
│ │ └── DateVerifyController.java
│ │ ├── converter
│ │ ├── CourseControllerHandler.java
│ │ └── CourseDateConverter.java
│ │ └── model
│ │ └── DateModelNoAnnotation.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── example
└── springboot_date
└── SpringbootDateApplicationTests.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 关于springboot以及netty的学习
2 |
3 | ## springboot学习
4 | 1. spring bean生命周期
5 | - [项目地址](https://github.com/guodayede/springboot-study/tree/master/springboot-bean)
6 | - [文章地址:](https://www.guoyuchuan.com/java/2018/11/17/springbean%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/)
7 | 2. springboot处理date参数
8 | - [项目地址](https://github.com/guodayede/springboot-study/tree/master/springboot_date)
9 | - [文章地址](https://www.guoyuchuan.com/springboot/2019/09/02/springboot%E5%A4%84%E7%90%86date%E5%8F%82%E6%95%B0)
10 | 3. springboot使用mybatis-plus(一)
11 | - [项目地址](https://github.com/guodayede/springboot-study/tree/master/springboot-mybatis)
12 | - [文章地址](https://www.guoyuchuan.com/java/springboot/2019/03/08/springboot%E4%BD%BF%E7%94%A8mybatis-plus-%E4%B8%80)
13 | 4. springboot异步线程(一)
14 | - [项目地址](https://github.com/guodayede/springboot-study/tree/master/springboot-async)
15 | - [文章地址](https://www.guoyuchuan.com/springboot/%E5%BC%82%E6%AD%A5/java/2019/11/13/springboot%E5%BC%82%E6%AD%A5%E7%BA%BF%E7%A8%8B(%E4%B8%80)/)
16 | 5. springboot异步线程(二)
17 | - [项目地址](https://github.com/guodayede/springboot-study/tree/master/springboot-async)
18 | - [文章地址](https://www.guoyuchuan.com/springboot/async/java/2019/12/16/springboot%E5%BC%82%E6%AD%A5%E7%BA%BF%E7%A8%8B(%E4%BA%8C)/)
19 | 6. springboot定时器(一)
20 | -[项目地址](https://github.com/guodayede/springboot-study)
21 | -[文章地址](https://www.guoyuchuan.com/java/springboot/2020/05/23/springboot%E5%AE%9A%E6%97%B6%E5%99%A8(%E4%B8%80)/)
22 | 7. MethodInterceptor 的几种用法
23 | - [项目地址](https://github.com/guodayede/springboot-study/tree/master/method-interceptor)
24 | - [文章地址](https://www.guoyuchuan.com/java/springboot/spring/2020/06/06/MethodInterceptor-%E7%9A%84%E5%87%A0%E7%A7%8D%E7%94%A8%E6%B3%95/)
25 |
26 |
27 | ## netty学习
28 | 1. netty学习(一)netty入门demo
29 | - [netty-test](https://github.com/guodayede/springboot-study/tree/master/netty-test)
30 | - [文章地址:](https://www.guoyuchuan.com/netty/springboot/2018/08/28/netty%E5%85%A5%E9%97%A8demo(%E4%B8%80))
31 | 2. netty学习(二)springboot整合netty
32 | - [springboot-netty:](https://github.com/guodayede/springboot-study/tree/master/springboot-netty)
33 | - [文章地址:](https://www.guoyuchuan.com/netty/springboot/2019/03/31/netty%E5%AD%A6%E4%B9%A0-%E4%BA%8C-springboot%E6%95%B4%E5%90%88netty)
34 | 3. netty学习(三)将netty接收数据直接存入数据库
35 | - [springboot-netty-mybatis](https://github.com/guodayede/springboot-study/tree/master/springboot-netty-mybatis)
36 | - [文章地址:](https://www.guoyuchuan.com/netty/springboot/2019/04/07/netty%E5%AD%A6%E4%B9%A0-%E4%B8%89-springboot+netty+mybatis)
37 |
--------------------------------------------------------------------------------
/method-interceptor/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.3.0.RELEASE
9 |
10 |
11 | com.example
12 | method-interceptor
13 | 0.0.1-SNAPSHOT
14 | method-interceptor
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-aop
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-test
34 | test
35 |
36 |
37 | org.junit.vintage
38 | junit-vintage-engine
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | org.apache.maven.plugins
48 | maven-compiler-plugin
49 |
50 | 1.8
51 | 1.8
52 | UTF-8
53 |
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-maven-plugin
58 |
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/java/com/example/methodinterceptor/MethodInterceptorApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.methodinterceptor;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class MethodInterceptorApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(MethodInterceptorApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/java/com/example/methodinterceptor/annotation/InterceptorAnnotation.java:
--------------------------------------------------------------------------------
1 | package com.example.methodinterceptor.annotation;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Target(ElementType.METHOD)
6 | @Retention(RetentionPolicy.RUNTIME)
7 | @Documented
8 | public @interface InterceptorAnnotation {
9 | }
10 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/java/com/example/methodinterceptor/config/InterceptorConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.methodinterceptor.config;
2 |
3 | import com.example.methodinterceptor.annotation.InterceptorAnnotation;
4 | import com.example.methodinterceptor.interceptor.MyInterceptor;
5 | import org.springframework.aop.Advisor;
6 | import org.springframework.aop.support.DefaultPointcutAdvisor;
7 | import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 |
11 | @Configuration
12 | public class InterceptorConfig {
13 | //注意该地址为项目具体包地址
14 | public static final String traceExecution = "execution(* com.example.methodinterceptor..*.*(..))";
15 | // public static final String traceExecution = "annotation(com.example.methodinterceptor.annotation.InterceptorAnnotation)";
16 |
17 |
18 | /* @Bean
19 | public DefaultPointcutAdvisor defaultPointcutAdvisor2() {
20 | MyInterceptor interceptor = new MyInterceptor();
21 | AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
22 | pointcut.setExpression(traceExecution);
23 |
24 | // 配置增强类advisor
25 | DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
26 | advisor.setPointcut(pointcut);
27 | advisor.setAdvice(interceptor);
28 | return advisor;
29 | }*/
30 | /* @Bean
31 | public DefaultPointcutAdvisor defaultPointcutAdvisor() {
32 | JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
33 | pointcut.setPattern("com.example.methodinterceptor.*");
34 | // 配置增强类advisor
35 | DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
36 | advisor.setPointcut(pointcut);
37 | advisor.setAdvice(new MyInterceptor());
38 | System.out.println(advisor.toString());
39 | return advisor;
40 | }*/
41 |
42 | @Bean
43 | public Advisor pointcutAdvisor() {
44 | MyInterceptor interceptor = new MyInterceptor();
45 |
46 | AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null,InterceptorAnnotation.class);
47 | // 配置增强类advisor
48 | DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
49 | advisor.setPointcut(pointcut);
50 | advisor.setAdvice(interceptor);
51 | System.out.println(advisor.toString());
52 | return advisor;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/java/com/example/methodinterceptor/controller/TestController.java:
--------------------------------------------------------------------------------
1 | package com.example.methodinterceptor.controller;
2 |
3 | import com.example.methodinterceptor.annotation.InterceptorAnnotation;
4 | import com.example.methodinterceptor.service.MyInterceptorInterface;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RestController;
8 |
9 | @RestController
10 | @RequestMapping("/test")
11 | public class TestController {
12 | private final MyInterceptorInterface myInterceptorInterface;
13 |
14 | @Autowired
15 | public TestController(MyInterceptorInterface myInterceptorInterface) {
16 | this.myInterceptorInterface = myInterceptorInterface;
17 | }
18 |
19 | @RequestMapping("/one")
20 | @InterceptorAnnotation
21 | public String getOne(){
22 | // MapUtil.get("1");
23 | myInterceptorInterface.getMyName();
24 | return "SUCCESS";
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/java/com/example/methodinterceptor/interceptor/MyInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.example.methodinterceptor.interceptor;
2 |
3 | import org.aopalliance.intercept.MethodInterceptor;
4 | import org.aopalliance.intercept.MethodInvocation;
5 |
6 | public class MyInterceptor implements MethodInterceptor {
7 | @Override
8 | public Object invoke(MethodInvocation methodInvocation) throws Throwable {
9 | System.out.println("=======");
10 | System.out.println(methodInvocation.getMethod().getName());
11 | System.out.println(methodInvocation.getMethod().getDeclaringClass().getName());
12 | return methodInvocation.proceed();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/java/com/example/methodinterceptor/service/MyInterceptorInterface.java:
--------------------------------------------------------------------------------
1 | package com.example.methodinterceptor.service;
2 |
3 | import com.example.methodinterceptor.annotation.InterceptorAnnotation;
4 |
5 | public interface MyInterceptorInterface {
6 |
7 | String getMyName();
8 | }
9 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/java/com/example/methodinterceptor/service/impl/MyInterceptorInterfaceImpl.java:
--------------------------------------------------------------------------------
1 | package com.example.methodinterceptor.service.impl;
2 |
3 | import com.example.methodinterceptor.annotation.InterceptorAnnotation;
4 | import com.example.methodinterceptor.service.MyInterceptorInterface;
5 | import org.springframework.stereotype.Service;
6 |
7 | @Service
8 | public class MyInterceptorInterfaceImpl implements MyInterceptorInterface {
9 | @Override
10 | @InterceptorAnnotation
11 | public String getMyName() {
12 | // System.out.println("getMyName");
13 | return "null";
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/method-interceptor/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/netty-test/README.md:
--------------------------------------------------------------------------------
1 | ## netty学习
2 | 1. [netty入门](https://github.com/guodayede/springboot-study/tree/master/netty-test)
--------------------------------------------------------------------------------
/netty-test/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com
8 | gyc
9 | 1.0
10 |
11 |
12 |
13 |
14 |
15 | io.netty
16 | netty-all
17 | 4.1.42.Final
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/netty-test/src/main/java/one/client/TimeClient.java:
--------------------------------------------------------------------------------
1 | package one.client;
2 |
3 | import io.netty.bootstrap.Bootstrap;
4 | import io.netty.buffer.ByteBuf;
5 | import io.netty.buffer.Unpooled;
6 | import io.netty.channel.ChannelFuture;
7 | import io.netty.channel.ChannelInitializer;
8 | import io.netty.channel.ChannelOption;
9 | import io.netty.channel.EventLoopGroup;
10 | import io.netty.channel.nio.NioEventLoopGroup;
11 | import io.netty.channel.socket.SocketChannel;
12 | import io.netty.channel.socket.nio.NioSocketChannel;
13 | import io.netty.handler.codec.DelimiterBasedFrameDecoder;
14 |
15 | public class TimeClient {
16 | public void connect(int port,String host)throws Exception{
17 | //配置客户端
18 | System.out.println(port+"--"+host);
19 | EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
20 | try {
21 | Bootstrap b=new Bootstrap();
22 | b.group(eventLoopGroup).channel(NioSocketChannel.class)
23 | .option(ChannelOption.TCP_NODELAY,true)
24 | .handler(new ChannelInitializer() {
25 | protected void initChannel(SocketChannel socketChannel) throws Exception {
26 | ByteBuf byteBuf= Unpooled.copiedBuffer("$".getBytes());
27 | socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
28 | socketChannel.pipeline().addLast(new TimeClientHandler());
29 | }
30 | });
31 | //绑定端口,同步等待成功
32 | ChannelFuture f = b.connect(host,port).sync();
33 | //等待服务监听端口关闭
34 | f.channel().closeFuture().sync();
35 | }finally {
36 | //优雅退出,释放线程资源
37 | eventLoopGroup.shutdownGracefully();
38 | }
39 | }
40 | public static void main(String[] args) throws Exception {
41 | new TimeClient().connect(8080,"localhost");
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/netty-test/src/main/java/one/client/TimeClientHandler.java:
--------------------------------------------------------------------------------
1 | package one.client;
2 |
3 | import io.netty.buffer.ByteBuf;
4 | import io.netty.buffer.Unpooled;
5 | import io.netty.channel.ChannelHandlerAdapter;
6 | import io.netty.channel.ChannelHandlerContext;
7 | import io.netty.util.CharsetUtil;
8 | import io.netty.util.ReferenceCountUtil;
9 |
10 | public class TimeClientHandler extends ChannelHandlerAdapter {
11 | private byte[] req;
12 | public TimeClientHandler(){
13 | req="$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$".getBytes();
14 | }
15 | @Override
16 | public void channelActive(ChannelHandlerContext ctx) throws Exception {
17 | ByteBuf message=null;
18 | for(int i=0;i<10;i++){
19 | message=Unpooled.buffer(req.length);
20 | message.writeBytes(req);
21 | ctx.writeAndFlush(message);
22 | }
23 | }
24 | @Override
25 | public void channelRead(ChannelHandlerContext ctx, Object msg) {
26 | try {
27 | ByteBuf in = (ByteBuf) msg;
28 | System.out.println(in.toString(CharsetUtil.UTF_8));
29 | } finally {
30 | ReferenceCountUtil.release(msg);
31 | }
32 | }
33 | @Override
34 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
35 | // 出现异常就关闭
36 | cause.printStackTrace();
37 | ctx.close();
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/netty-test/src/main/java/one/server/ChildChannelHandler.java:
--------------------------------------------------------------------------------
1 | package one.server;
2 |
3 |
4 | import io.netty.buffer.ByteBuf;
5 | import io.netty.buffer.Unpooled;
6 | import io.netty.channel.ChannelInitializer;
7 | import io.netty.channel.socket.SocketChannel;
8 | import io.netty.handler.codec.DelimiterBasedFrameDecoder;
9 |
10 | public class ChildChannelHandler extends ChannelInitializer {
11 |
12 | protected void initChannel(SocketChannel socketChannel) throws Exception {
13 | ByteBuf byteBuf= Unpooled.copiedBuffer("$".getBytes());
14 | socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
15 | socketChannel.pipeline().addLast(new DiscardServerHandler());
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/netty-test/src/main/java/one/server/DiscardServer.java:
--------------------------------------------------------------------------------
1 | package one.server;
2 |
3 | import io.netty.bootstrap.ServerBootstrap;
4 | import io.netty.channel.ChannelFuture;
5 | import io.netty.channel.ChannelOption;
6 | import io.netty.channel.EventLoopGroup;
7 | import io.netty.channel.nio.NioEventLoopGroup;
8 | import io.netty.channel.socket.nio.NioServerSocketChannel;
9 |
10 | /**
11 | * 丢弃任何进入的数据 启动服务端的DiscardServerHandler
12 | */
13 | public class DiscardServer {
14 | public void run(int port) throws Exception {
15 | EventLoopGroup bossGroup = new NioEventLoopGroup();
16 | EventLoopGroup workerGroup = new NioEventLoopGroup();
17 | System.out.println("准备运行端口:" + port);
18 | try {
19 | ServerBootstrap b = new ServerBootstrap();
20 | b = b.group(bossGroup, workerGroup)
21 | .channel(NioServerSocketChannel.class)
22 | .option(ChannelOption.SO_BACKLOG, 128)
23 | .childHandler(new ChildChannelHandler());
24 | //绑定端口,同步等待成功
25 | ChannelFuture f = b.bind(port).sync();
26 | //等待服务监听端口关闭
27 | f.channel().closeFuture().sync();
28 | } finally {
29 | //优雅退出,释放线程资源
30 | workerGroup.shutdownGracefully();
31 | bossGroup.shutdownGracefully();
32 | }
33 | }
34 | public static void main(String[] args) throws Exception {
35 | new DiscardServer().run(8080);
36 | }
37 | }
--------------------------------------------------------------------------------
/netty-test/src/main/java/one/server/DiscardServerHandler.java:
--------------------------------------------------------------------------------
1 | package one.server;
2 |
3 | import io.netty.buffer.ByteBuf;
4 | import io.netty.buffer.Unpooled;
5 | import io.netty.channel.ChannelHandlerAdapter;
6 | import io.netty.channel.ChannelHandlerContext;
7 | import io.netty.util.CharsetUtil;
8 | import io.netty.util.ReferenceCountUtil;
9 |
10 | public class DiscardServerHandler extends ChannelHandlerAdapter {
11 | @Override
12 | public void channelRead(ChannelHandlerContext ctx, Object msg) {
13 |
14 | try {
15 | ByteBuf in = (ByteBuf) msg;
16 | if(in!=null&& in.readableBytes()>0){
17 | System.out.println("传输内容是");
18 | System.out.println(in.toString(CharsetUtil.UTF_8));
19 | ByteBuf resp= Unpooled.copiedBuffer("收到信息$".getBytes());
20 | ctx.writeAndFlush(resp);
21 | }
22 | } finally {
23 | ReferenceCountUtil.release(msg);
24 | }
25 | }
26 | @Override
27 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
28 | // 出现异常就关闭
29 | cause.printStackTrace();
30 | ctx.close();
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-admin/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.0.5.RELEASE
9 |
10 |
11 | com.example
12 | actuator-admin
13 | 1.0
14 | actuator-admin
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-test
33 | test
34 |
35 |
36 | de.codecentric
37 | spring-boot-admin-starter-server
38 | 2.0.1
39 |
40 |
41 | de.codecentric
42 | spring-boot-admin-server-ui
43 | 2.0.1
44 |
45 |
46 |
47 |
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-maven-plugin
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-admin/src/main/java/com/example/actuatoradmin/ActuatorAdminApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.actuatoradmin;
2 |
3 | import de.codecentric.boot.admin.server.config.EnableAdminServer;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | @EnableAdminServer
9 | public class ActuatorAdminApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(ActuatorAdminApplication.class, args);
13 | }
14 |
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-admin/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8088
3 |
4 | management:
5 | endpoints:
6 | web:
7 | exposure:
8 | include: "*"
9 | endpoint:
10 | health:
11 | show-details: always
12 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-admin/src/test/java/com/example/actuatoradmin/ActuatorAdminApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.actuatoradmin;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ActuatorAdminApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-client/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.0.5.RELEASE
9 |
10 |
11 | com.example
12 | client
13 | 1.0
14 | client
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
36 |
37 | de.codecentric
38 | spring-boot-admin-starter-client
39 | 2.0.0
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-web
44 |
45 |
46 |
47 |
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-maven-plugin
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-client/src/main/java/com/example/client/ClientApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.client;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.scheduling.annotation.EnableScheduling;
6 |
7 | @SpringBootApplication
8 | @EnableScheduling
9 | public class ClientApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(ClientApplication.class, args);
13 | }
14 |
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-client/src/main/java/com/example/client/endpoint/FeaturesEndpoint.java:
--------------------------------------------------------------------------------
1 | package com.example.client.endpoint;
2 |
3 | import org.springframework.boot.actuate.endpoint.annotation.*;
4 | import org.springframework.stereotype.Component;
5 |
6 | import java.util.Map;
7 | import java.util.concurrent.ConcurrentHashMap;
8 |
9 | /**
10 | * @author gyc
11 | * @Title: FeaturesEndpoint
12 | * @ProjectName springboot-study
13 | * @Description:
14 | * @date 2018/12/18 16:29
15 | */
16 | /*
17 | @Component
18 | @Endpoint(id = "featuresEndpoint")
19 | public class FeaturesEndpoint {
20 | private Map features = new ConcurrentHashMap<>();
21 |
22 | @ReadOperation
23 | public Map features() {
24 | return features;
25 | }
26 |
27 | @ReadOperation
28 | public Feature feature(@Selector String name) {
29 | return features.get(name);
30 | }
31 |
32 | @WriteOperation
33 | public void configureFeature(@Selector String name) {
34 | features.put(name, new Feature());
35 | }
36 |
37 | @DeleteOperation
38 | public void deleteFeature(@Selector String name) {
39 | features.remove(name);
40 | }
41 | public static class Feature {
42 | private Boolean enabled;
43 |
44 | public Boolean getEnabled() {
45 | return enabled;
46 | }
47 |
48 | public void setEnabled(Boolean enabled) {
49 | this.enabled = enabled;
50 | }
51 | }
52 | }
53 | */
54 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-client/src/main/java/com/example/client/timetask/TimeTaskTest.java:
--------------------------------------------------------------------------------
1 | package com.example.client.timetask;
2 |
3 | import org.springframework.scheduling.annotation.Scheduled;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * @author gyc
8 | * @Title: TimeTaskTest
9 | * @ProjectName springboot-study
10 | * @Description:
11 | * @date 2018/12/18 10:52
12 | */
13 | @Component
14 | public class TimeTaskTest {
15 |
16 | @Scheduled(cron = "0/10 * * * * ? ")
17 | public void test(){
18 | System.out.println("corn定时任务");
19 | }
20 |
21 | @Scheduled(initialDelay = 1000*15 ,fixedDelay = 1000*5)
22 | public void testone(){
23 | System.out.println("aaaaaa");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-client/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | management:
2 | endpoints:
3 | web:
4 | exposure:
5 | include: "*"
6 | server:
7 | port: 8080
8 |
9 | spring:
10 | boot:
11 | admin:
12 | client:
13 | url: "http://localhost:8088"
--------------------------------------------------------------------------------
/springboot-actuator/springboot-actuator-client/src/test/java/com/example/client/ClientApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.client;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ClientApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/springboot-async/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.2.1.RELEASE
9 |
10 |
11 | com.example
12 | async
13 | 1.0
14 | springboot-async
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-test
33 | test
34 |
35 |
36 | org.junit.vintage
37 | junit-vintage-engine
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-maven-plugin
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/AsyncApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.async;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.scheduling.annotation.EnableAsync;
6 | import org.springframework.scheduling.annotation.EnableScheduling;
7 |
8 | /**
9 | * @author gyc
10 | */
11 | @SpringBootApplication
12 | @EnableAsync
13 | public class AsyncApplication {
14 | public static void main(String[] args) {
15 | SpringApplication.run(AsyncApplication.class, args);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/async/AsyncTest.java:
--------------------------------------------------------------------------------
1 | package com.example.async.async;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.scheduling.annotation.Async;
6 | import org.springframework.stereotype.Component;
7 |
8 | /**
9 | * @author gyc
10 | * @title: AsyncTest
11 | * @projectName test
12 | * @date 2019/11/1314:06
13 | * @description:
14 | */
15 | @Component
16 | public class AsyncTest {
17 | private static final Logger log= LoggerFactory.getLogger(AsyncTest.class);
18 | @Async
19 | public void testOne(){
20 | log.info("ThreadName:===one===="+Thread.currentThread().getName());
21 | try {
22 | Thread.sleep(1000*60);
23 | } catch (InterruptedException e) {
24 | e.printStackTrace();
25 | }
26 | }
27 | @Async
28 | public void testTwo(){
29 | log.info("ThreadName:====two===="+Thread.currentThread().getName());
30 | try {
31 | Thread.sleep(1000*60);
32 | } catch (InterruptedException e) {
33 | e.printStackTrace();
34 | }
35 | }
36 | @Async
37 | public void testThree(){
38 | log.info("ThreadName:====three===="+Thread.currentThread().getName());
39 | try {
40 | Thread.sleep(1000*60);
41 | } catch (InterruptedException e) {
42 | e.printStackTrace();
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/config/AsyncConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.async.config;
2 |
3 | import com.example.async.handler.AsyncExceptionHandler;
4 | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.core.task.AsyncTaskExecutor;
8 | import org.springframework.core.task.SimpleAsyncTaskExecutor;
9 | import org.springframework.core.task.TaskExecutor;
10 | import org.springframework.scheduling.annotation.AsyncConfigurer;
11 | import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
12 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
13 |
14 | import java.util.concurrent.Executor;
15 | import java.util.concurrent.Executors;
16 |
17 | /**
18 | * @author gyc
19 | * @title: CoursesAsyncConfig
20 | * @projectName app
21 | * @date 2019/11/715:19
22 | * @description:
23 | */
24 | @Configuration
25 | public class AsyncConfig implements AsyncConfigurer {
26 | @Override
27 | public Executor getAsyncExecutor() {
28 | ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
29 | //线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
30 | executor.setThreadNamePrefix("courses-schedule-");
31 | //最大线程数10:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
32 | executor.setMaxPoolSize(10);
33 | //核心线程数3:线程池创建时候初始化的线程数
34 | executor.setCorePoolSize(3);
35 | //缓冲队列0:用来缓冲执行任务的队列
36 | executor.setQueueCapacity(5);
37 | //允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
38 | executor.setKeepAliveSeconds(60);
39 | // 当线程池已满,且等待队列也满了的时候,直接抛弃当前线程(不会抛出异常)
40 | // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
41 | // executor.initialize();
42 | return executor;
43 | }
44 | @Override
45 | public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
46 | return new AsyncExceptionHandler();
47 | }
48 | }
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/config/AsyncTwoConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.async.config;
2 |
3 | import com.example.async.handler.AsyncExceptionHandler;
4 | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.core.task.TaskExecutor;
8 | import org.springframework.scheduling.annotation.AsyncConfigurer;
9 | import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
10 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
11 |
12 | import java.util.concurrent.Executor;
13 | import java.util.concurrent.Executors;
14 |
15 | /**
16 | * @author gyc
17 | * @title: AsyncTwoConfig
18 | * @projectName springboot-study
19 | * @date 2019/12/2611:37
20 | * @description:
21 | */
22 | @Configuration
23 | public class AsyncTwoConfig {
24 |
25 | @Bean(name = "threadPoolTaskExecutor")
26 | public ThreadPoolTaskExecutor testTaskExecutor() {
27 | ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
28 | //线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
29 | executor.setThreadNamePrefix("courses-schedule-");
30 | //最大线程数10:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
31 | executor.setMaxPoolSize(10);
32 | //核心线程数3:线程池创建时候初始化的线程数
33 | executor.setCorePoolSize(3);
34 | //缓冲队列0:用来缓冲执行任务的队列
35 | executor.setQueueCapacity(5);
36 | //允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
37 | executor.setKeepAliveSeconds(60);
38 | // 当线程池已满,且等待队列也满了的时候,直接抛弃当前线程(不会抛出异常)
39 | // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
40 | // executor.initialize();
41 | return executor;
42 | }
43 | @Bean(name = "concurrentTaskExecutor")
44 | public TaskExecutor concurrentTaskExecutor () {
45 | return new ConcurrentTaskExecutor(
46 | Executors.newFixedThreadPool(3));
47 | }
48 | }
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/controller/TestController.java:
--------------------------------------------------------------------------------
1 | package com.example.async.controller;
2 |
3 | import com.example.async.service.AsyncService;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | /**
12 | * @author gyc
13 | * @title: TestController
14 | * @projectName test
15 | * @date 2019/11/1311:49
16 | * @description:
17 | */
18 | @RestController
19 | @RequestMapping("/test")
20 | public class TestController {
21 | private final AsyncService asyncService;
22 | @Autowired
23 | public TestController(AsyncService asyncService) {
24 | this.asyncService = asyncService;
25 | }
26 |
27 | @GetMapping
28 | public String test(){
29 | asyncService.testAsync();
30 | asyncService.testOne();
31 | return "SUCCESS";
32 | }
33 | }
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/handler/AsyncExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.example.async.handler;
2 |
3 | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
4 |
5 | import java.lang.reflect.Method;
6 |
7 | /**
8 | * @author gyc
9 | * @title: AsyncExceptionHandler
10 | * @projectName springboot-study
11 | * @date 2019/12/2611:38
12 | * @description:
13 | */
14 | public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
15 | @Override
16 | public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
17 | System.out.println("Exception Cause - " + throwable.getMessage());
18 | System.out.println("Method name - " + method.getName());
19 | for (Object param : obj) {
20 | System.out.println("Parameter value - " + param);
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/service/AsyncService.java:
--------------------------------------------------------------------------------
1 | package com.example.async.service;
2 |
3 |
4 | import org.springframework.scheduling.annotation.Async;
5 |
6 | /**
7 | * @author gyc
8 | * @title: AsyncService
9 | * @projectName test
10 | * @date 2019/11/1312:31
11 | * @description:
12 | */
13 | public interface AsyncService {
14 | /**
15 | * 测试
16 | */
17 | @Async("threadPoolTaskExecutor")
18 | void testAsync();
19 |
20 | /**
21 | *测试一
22 | */
23 | @Async("concurrentTaskExecutor")
24 | void testOne();
25 | }
26 |
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/service/impl/AsyncServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.example.async.service.impl;
2 |
3 | import com.example.async.async.AsyncTest;
4 | import com.example.async.service.AsyncService;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.scheduling.annotation.Async;
9 | import org.springframework.stereotype.Service;
10 |
11 | /**
12 | * @author gyc
13 | * @title: AsyncServiceImpl
14 | * @projectName test
15 | * @date 2019/11/1312:31
16 | * @description:
17 | */
18 | @Service
19 | public class AsyncServiceImpl implements AsyncService {
20 | private static final Logger log= LoggerFactory.getLogger(AsyncServiceImpl.class);
21 | @Override
22 | public void testAsync(){
23 | log.info("========testAsync运行=========="+Thread.currentThread().getName());
24 | }
25 | @Override
26 | public void testOne(){
27 | log.info("ThreadName:===one===="+Thread.currentThread().getName());
28 | }
29 | }
--------------------------------------------------------------------------------
/springboot-async/src/main/java/com/example/async/timetask/TestScheduling.java:
--------------------------------------------------------------------------------
1 | package com.example.async.timetask;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.scheduling.annotation.Async;
6 | import org.springframework.scheduling.annotation.Scheduled;
7 |
8 | /**
9 | * @author gyc
10 | * @title: TestScheduling
11 | * @projectName test
12 | * @date 2019/11/1311:54
13 | * @description:
14 | */
15 | //@Component
16 | public class TestScheduling {
17 | private static final Logger log = LoggerFactory.getLogger(TestScheduling.class);
18 | @Async("asyncTaskExecutor")
19 | @Scheduled(initialDelay = 1000 * 60, fixedDelay = 1000 * 30)
20 | public void testOne() {
21 | //打印线程名字
22 | log.info("ThreadName:====one====" + Thread.currentThread().getName());
23 | }
24 | @Async("asyncTaskExecutor")
25 | @Scheduled(initialDelay = 1000 * 60, fixedDelay = 1000 * 30)
26 | public void testTwo() {
27 | //打印线程名字
28 | log.info("ThreadName:====two====" + Thread.currentThread().getName());
29 | }
30 | }
--------------------------------------------------------------------------------
/springboot-async/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9090
--------------------------------------------------------------------------------
/springboot-bean/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com
7 | gyc
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | gyc
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.4.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-test
36 | test
37 |
38 |
39 |
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-maven-plugin
45 |
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/java/com/gyc/GycApplication.java:
--------------------------------------------------------------------------------
1 | package com.gyc;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class GycApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(GycApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/java/com/gyc/bean/AnnotationBean.java:
--------------------------------------------------------------------------------
1 | package com.gyc.bean;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.stereotype.Component;
6 | import org.springframework.stereotype.Service;
7 |
8 | import javax.annotation.PostConstruct;
9 | import javax.annotation.PreDestroy;
10 |
11 | /**
12 | * @author gyc
13 | * @date 2018/9/6
14 | */
15 | @Component
16 | public class AnnotationBean {
17 | private static final Logger logger= LoggerFactory.getLogger(AnnotationBean.class);
18 | @PostConstruct
19 | public void init(){
20 | logger.info("AnnotationBean-init");
21 | }
22 |
23 | @PreDestroy
24 | public void destroy(){
25 | logger.info("AnnotationBean-destroy");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/java/com/gyc/bean/CustomBean.java:
--------------------------------------------------------------------------------
1 | package com.gyc.bean;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | /**
7 | * @author gyc
8 | * @date 2018/9/5
9 | * 用于自定义配置bean
10 | */
11 | public class CustomBean {
12 | private final static Logger logger = LoggerFactory.getLogger(CustomBean.class);
13 | public void init(){
14 | logger.info("CustomBean-init");
15 | }
16 |
17 | public void destroy(){
18 | logger.info("CustomBean-destroy");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/java/com/gyc/bean/LifeCycleBean.java:
--------------------------------------------------------------------------------
1 | package com.gyc.bean;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.DisposableBean;
6 | import org.springframework.beans.factory.InitializingBean;
7 | import org.springframework.stereotype.Component;
8 |
9 | /**
10 | * @author gyc
11 | * @date 2018/9/8
12 | */
13 | @Component
14 | public class LifeCycleBean implements InitializingBean,DisposableBean {
15 | private static final Logger logger= LoggerFactory.getLogger(LifeCycleBean.class);
16 | @Override
17 | public void destroy() throws Exception {
18 | logger.info("LifeCycleBean-destroy");
19 | }
20 |
21 | @Override
22 | public void afterPropertiesSet() throws Exception {
23 | logger.info("LifeCycleBean-afterPropertiesSet");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/java/com/gyc/bean/LifeCycleBeanAware.java:
--------------------------------------------------------------------------------
1 | package com.gyc.bean;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.BeansException;
6 | import org.springframework.beans.factory.BeanFactory;
7 | import org.springframework.beans.factory.BeanFactoryAware;
8 | import org.springframework.beans.factory.BeanNameAware;
9 | import org.springframework.context.ApplicationContext;
10 | import org.springframework.context.ApplicationContextAware;
11 | import org.springframework.stereotype.Component;
12 |
13 | /**
14 | * @author gyc
15 | * @date 2018/9/8
16 | */
17 | @Component
18 | public class LifeCycleBeanAware implements BeanNameAware,BeanFactoryAware,ApplicationContextAware {
19 | private static final Logger logger= LoggerFactory.getLogger(LifeCycleBeanAware.class);
20 | @Override
21 | public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
22 | logger.info("bean工厂");
23 | }
24 |
25 | @Override
26 | public void setBeanName(String s) {
27 | logger.info("bean的默认的名字:"+s);
28 | }
29 |
30 | @Override
31 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
32 | logger.info("bean上下文");
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/java/com/gyc/bean/LifeCycleProcessor.java:
--------------------------------------------------------------------------------
1 | package com.gyc.bean;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.BeansException;
6 | import org.springframework.beans.factory.config.BeanPostProcessor;
7 | import org.springframework.stereotype.Component;
8 |
9 | /**
10 | * @author gyc
11 | * @date 2018/9/8
12 | */
13 | @Component
14 | public class LifeCycleProcessor implements BeanPostProcessor {
15 | private static final Logger logger= LoggerFactory.getLogger(LifeCycleProcessor.class);
16 | private static final String DEFAULT_BEAN_NAME="annotationBean";
17 | /**
18 | *在bean初始化之前执行
19 | * @param bean
20 | * @param beanName
21 | * @return
22 | * @throws BeansException
23 | */
24 | @Override
25 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
26 | if(DEFAULT_BEAN_NAME.equals(beanName)){
27 | logger.info("bean初始化之前执行:"+beanName);
28 | }
29 | return bean;
30 | }
31 | /**
32 | *bean初始化之后执行
33 | * @param bean
34 | * @param beanName
35 | * @return
36 | * @throws BeansException
37 | */
38 | @Override
39 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
40 | if(DEFAULT_BEAN_NAME.equals(beanName)){
41 | logger.info("bean初始化之后执行:"+beanName);
42 | }
43 | return bean;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/java/com/gyc/config/LifeCycleConfig.java:
--------------------------------------------------------------------------------
1 | package com.gyc.config;
2 |
3 | import com.gyc.bean.CustomBean;
4 | import com.gyc.bean.AnnotationBean;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 |
8 | /**
9 | * @author gyc
10 | * @date 2018/9/8
11 | */
12 | @Configuration
13 | public class LifeCycleConfig {
14 | /**
15 | * 自定义bean的initMethod和destroyMethod
16 | * @return
17 | */
18 | @Bean(initMethod = "init",destroyMethod = "destroy")
19 | CustomBean customBean(){
20 | return new CustomBean();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/springboot-bean/src/main/resources/application.yml:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guodayede/springboot-study/6c21b47ee9e30672fe70410502f3ee37454a0b85/springboot-bean/src/main/resources/application.yml
--------------------------------------------------------------------------------
/springboot-bean/src/test/java/com/gyc/GycApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.gyc;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class GycApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/springboot-mybatis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.example
7 | demo
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | demo
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.1.0.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-test
36 | test
37 |
38 |
39 |
40 |
45 |
46 |
47 | mysql
48 | mysql-connector-java
49 |
50 |
51 |
52 | com.baomidou
53 | mybatis-plus-boot-starter
54 | 3.1.0
55 |
56 |
57 |
58 | com.alibaba
59 | druid-spring-boot-starter
60 | 1.1.10
61 |
62 |
63 |
64 |
65 |
66 |
67 | org.springframework.boot
68 | spring-boot-maven-plugin
69 |
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/com/example/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | /**
8 | * @author gyc
9 | */
10 | @SpringBootApplication
11 | public class DemoApplication {
12 |
13 | public static void main(String[] args) {
14 | SpringApplication.run(DemoApplication.class, args);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/com/example/demo/config/MybatisPageConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.config;
2 |
3 | import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
4 | import org.mybatis.spring.annotation.MapperScan;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 |
8 | /**
9 | * @author: gyc
10 | * @date: 2019/3/7 17:34
11 | * @description:
12 | */
13 | @Configuration
14 | @MapperScan("com.example.demo.mapper")
15 | public class MybatisPageConfig {
16 | /**
17 | * mybatis-plus分页插件
18 | * 文档:http://mp.baomidou.com
19 | */
20 | @Bean
21 | public PaginationInterceptor paginationInterceptor() {
22 | PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
23 | return paginationInterceptor;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/com/example/demo/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.mapper;
2 |
3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 | import com.example.demo.model.User;
5 |
6 | /**
7 | * @Author gyc
8 | * @Date: 2019/3/7 14:41
9 | * @Description:
10 | */
11 | public interface UserMapper extends BaseMapper {
12 | }
13 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/java/com/example/demo/model/User.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.model;
2 |
3 | import com.baomidou.mybatisplus.annotation.TableField;
4 | import com.baomidou.mybatisplus.annotation.TableId;
5 | import com.baomidou.mybatisplus.annotation.TableName;
6 |
7 | import java.io.Serializable;
8 |
9 | /**
10 | * @author gyc
11 | * @Date: 2019/3/7 14:36
12 | * @Description:
13 | */
14 | @TableName("user")
15 | public class User implements Serializable {
16 | private static final long serialVersionUID = 8268426372054408603L;
17 |
18 | @TableId("id")
19 | private Long id;
20 | @TableField("name")
21 | private String name;
22 | @TableField("age")
23 | private Integer age;
24 | @TableField("email")
25 | private String email;
26 |
27 | public Long getId() {
28 | return id;
29 | }
30 |
31 | public void setId(Long id) {
32 | this.id = id;
33 | }
34 |
35 | public String getName() {
36 | return name;
37 | }
38 |
39 | public void setName(String name) {
40 | this.name = name;
41 | }
42 |
43 | public Integer getAge() {
44 | return age;
45 | }
46 |
47 | public void setAge(Integer age) {
48 | this.age = age;
49 | }
50 |
51 | public String getEmail() {
52 | return email;
53 | }
54 |
55 | public void setEmail(String email) {
56 | this.email = email;
57 | }
58 | @Override
59 | public String toString() {
60 | return "User{" +
61 | "id=" + id +
62 | ", name='" + name + '\'' +
63 | ", age=" + age +
64 | ", email='" + email + '\'' +
65 | '}';
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | druid:
4 | url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
5 | username: root
6 | password: 123
7 | driver-class-name: com.mysql.jdbc.Driver
8 | initial-size: 5
9 | max-active: 30
10 | min-idle: 2
11 | max-wait: 1234
12 | pool-prepared-statements: true
13 | max-pool-prepared-statement-per-connection-size: 5
14 | # driver-class-name: com.mysql.jdbc.Driver
15 | mybatis-plus:
16 | # config-location: classpath:/mapper/*.xml
17 | type-aliases-package: com.example.demo.model
18 | configuration:
19 | # 驼峰下划线转换
20 | map-underscore-to-camel-case: true
21 | cache-enabled: false
22 | call-setters-on-nulls: true
23 | global-config:
24 | # 刷新mapper 调试神器
25 | refresh: true
26 | banner: false
27 | #数据库大写下划线转换
28 | #capital-mode: true
29 | #序列接口实现类配置
30 | #key-generator: com.baomidou.springboot.xxx
31 | # 数据库相关配置
32 | db-config:
33 | db-type: mysql
34 | # 主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
35 | id-type: auto
36 | # 字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
37 | field-strategy: not_empty
38 | capital-mode: true
39 | #逻辑删除配置
40 | logic-delete-value: 1
41 | logic-not-delete-value: 0
42 |
--------------------------------------------------------------------------------
/springboot-mybatis/src/test/java/com/example/demo/DemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 | import com.baomidou.mybatisplus.core.metadata.IPage;
5 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6 | import com.example.demo.mapper.UserMapper;
7 | import com.example.demo.model.User;
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 | import org.springframework.boot.test.context.SpringBootTest;
11 | import org.springframework.test.context.junit4.SpringRunner;
12 |
13 | import javax.annotation.Resource;
14 | import java.util.List;
15 |
16 | @RunWith(SpringRunner.class)
17 | @SpringBootTest
18 | public class DemoApplicationTests {
19 | @Resource
20 | private UserMapper userMapper;
21 |
22 | @Test
23 | public void contextLoads() {
24 | User user=new User();
25 | user.setName("jack");
26 | // List userList = userMapper.selectList(new QueryWrapper<>(user));
27 | // userList.forEach(one-> System.out.println(one.toString()));
28 | Page page=new Page(3,2);
29 | IPage iPage = userMapper.selectPage(page, null);
30 | List records = iPage.getRecords();
31 | records.forEach(one-> System.out.println(one.toString()));
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/springboot-netty-mybatis/db/user.sql:
--------------------------------------------------------------------------------
1 | /*
2 | Navicat MySQL Data Transfer
3 |
4 | Source Server : 本地
5 | Source Server Version : 50717
6 | Source Host : localhost:3306
7 | Source Database : authority
8 |
9 | Target Server Type : MYSQL
10 | Target Server Version : 50717
11 | File Encoding : 65001
12 |
13 | Date: 2018-10-07 21:10:08
14 | */
15 |
16 | SET FOREIGN_KEY_CHECKS=0;
17 |
18 | -- ----------------------------
19 | -- Table structure for user
20 | -- ----------------------------
21 | DROP TABLE IF EXISTS `user`;
22 | CREATE TABLE `user` (
23 | `id` int(10) NOT NULL AUTO_INCREMENT,
24 | `name` char(20) DEFAULT NULL,
25 | `password` char(20) DEFAULT NULL,
26 | `age` int(10) DEFAULT NULL,
27 | `birthday` date DEFAULT NULL,
28 | PRIMARY KEY (`id`)
29 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
30 |
31 | -- ----------------------------
32 | -- Records of user
33 | -- ----------------------------
34 | INSERT INTO `user` VALUES ('1', 'admin', 'admin', '10', '2017-05-30');
35 | INSERT INTO `user` VALUES ('2', 'teacher', 'teacher', '30', null);
36 | INSERT INTO `user` VALUES ('3', 'student', 'student', '20', null);
37 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com
7 | gyc
8 | 1.0
9 | jar
10 |
11 | gyc
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.5.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-test
35 | test
36 |
37 |
38 |
39 | io.netty
40 | netty-all
41 | 4.1.42.Final
42 |
43 |
44 |
45 | org.mybatis.spring.boot
46 | mybatis-spring-boot-starter
47 | 1.3.2
48 |
49 |
50 |
51 | mysql
52 | mysql-connector-java
53 |
54 |
55 |
56 | com.alibaba
57 | druid-spring-boot-starter
58 | 1.1.9
59 |
60 |
61 |
62 |
63 |
64 |
65 | org.springframework.boot
66 | spring-boot-maven-plugin
67 |
68 |
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/java/com/gyc/GycApplication.java:
--------------------------------------------------------------------------------
1 | package com.gyc;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | @MapperScan(basePackages = "com.gyc.mapper")
9 | public class GycApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(GycApplication.class, args);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/java/com/gyc/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.gyc.entity;
2 |
3 | import java.io.Serializable;
4 | import java.util.Date;
5 |
6 |
7 | public class User implements Serializable {
8 | private static final long serialVersionUID = -7776017450107481817L;
9 | private int id;
10 | private String name;
11 | private String password;
12 | private int age;
13 | private Date birthday;
14 |
15 | public int getId() {
16 | return id;
17 | }
18 |
19 | public void setId(int id) {
20 | this.id = id;
21 | }
22 |
23 | public String getName() {
24 | return name;
25 | }
26 |
27 | public void setName(String name) {
28 | this.name = name;
29 | }
30 |
31 | public String getPassword() {
32 | return password;
33 | }
34 |
35 | public void setPassword(String password) {
36 | this.password = password;
37 | }
38 |
39 | public int getAge() {
40 | return age;
41 | }
42 |
43 | public void setAge(int age) {
44 | this.age = age;
45 | }
46 |
47 | public Date getBirthday() {
48 | return birthday;
49 | }
50 |
51 | public void setBirthday(Date birthday) {
52 | this.birthday = birthday;
53 | }
54 |
55 | @Override
56 | public String toString() {
57 | return "User{" +
58 | "id=" + id +
59 | ", name='" + name + '\'' +
60 | ", password='" + password + '\'' +
61 | ", age=" + age +
62 | ", birthday=" + birthday +
63 | '}';
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/java/com/gyc/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | package com.gyc.mapper;
2 |
3 | import com.gyc.entity.User;
4 | import org.springframework.stereotype.Repository;
5 |
6 | import java.util.List;
7 |
8 | @Repository
9 | public interface UserMapper {
10 |
11 | List selectAll();
12 | }
13 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/java/com/gyc/netty/NettyStart.java:
--------------------------------------------------------------------------------
1 | package com.gyc.netty;
2 |
3 | import io.netty.bootstrap.ServerBootstrap;
4 | import io.netty.buffer.ByteBuf;
5 | import io.netty.buffer.Unpooled;
6 | import io.netty.channel.ChannelFuture;
7 | import io.netty.channel.ChannelInitializer;
8 | import io.netty.channel.ChannelOption;
9 | import io.netty.channel.EventLoopGroup;
10 | import io.netty.channel.nio.NioEventLoopGroup;
11 | import io.netty.channel.socket.SocketChannel;
12 | import io.netty.channel.socket.nio.NioServerSocketChannel;
13 | import io.netty.handler.codec.DelimiterBasedFrameDecoder;
14 | import org.springframework.stereotype.Component;
15 |
16 | import javax.annotation.PostConstruct;
17 | import javax.annotation.PreDestroy;
18 | import javax.annotation.Resource;
19 |
20 | @Component
21 | public class NettyStart {
22 | @Resource
23 | private ServerHandler serverHandler;
24 |
25 | private EventLoopGroup bossGroup = new NioEventLoopGroup();
26 | private EventLoopGroup workGroup = new NioEventLoopGroup();
27 | /**
28 | * 启动netty服务
29 | * @throws InterruptedException
30 | */
31 | @PostConstruct
32 | public void start() throws InterruptedException {
33 | ServerBootstrap b=new ServerBootstrap();
34 | b.group(bossGroup,workGroup)
35 | .channel(NioServerSocketChannel.class)
36 | .option(ChannelOption.SO_BACKLOG,128)
37 | .childHandler(new ChannelInitializer() {
38 | @Override
39 | protected void initChannel(SocketChannel socketChannel) throws Exception {
40 | socketChannel.pipeline().addLast(serverHandler);
41 | }
42 | });
43 | ChannelFuture future = b.bind(8080).sync();
44 | if (future.isSuccess()) {
45 | System.out.println("启动 Netty 成功");
46 | }
47 | }
48 | /**
49 | * 销毁
50 | */
51 | @PreDestroy
52 | public void destroy() {
53 | bossGroup.shutdownGracefully().syncUninterruptibly();
54 | workGroup.shutdownGracefully().syncUninterruptibly();
55 | System.out.println("关闭 Netty 成功");
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/java/com/gyc/netty/ServerHandler.java:
--------------------------------------------------------------------------------
1 | package com.gyc.netty;
2 |
3 | import com.gyc.entity.User;
4 | import com.gyc.service.UserService;
5 | import io.netty.buffer.ByteBuf;
6 | import io.netty.channel.ChannelHandler.Sharable;
7 | import io.netty.channel.ChannelHandlerContext;
8 | import io.netty.channel.ChannelInboundHandlerAdapter;
9 | import io.netty.util.CharsetUtil;
10 | import org.springframework.stereotype.Component;
11 |
12 | import javax.annotation.Resource;
13 | import java.util.List;
14 |
15 | @Component
16 | @Sharable
17 | public class ServerHandler extends ChannelInboundHandlerAdapter {
18 | @Resource
19 | private UserService userService;
20 |
21 | /**
22 | * 获取数据
23 | * @param ctx 上下文
24 | * @param msg 获取的数据
25 | */
26 | @Override
27 | public void channelRead(ChannelHandlerContext ctx, Object msg){
28 | ByteBuf readMessage= (ByteBuf) msg;
29 | System.out.println(readMessage.toString(CharsetUtil.UTF_8));
30 | List users = userService.selectAll();
31 | users.forEach(user-> System.out.println(user.toString()));
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/java/com/gyc/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.gyc.service;
2 |
3 | import com.gyc.entity.User;
4 |
5 | import java.util.List;
6 |
7 | public interface UserService {
8 | /**
9 | * 获取所有数据
10 | * @return
11 | */
12 | List selectAll();
13 | }
14 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/java/com/gyc/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.gyc.service.impl;
2 |
3 | import com.gyc.entity.User;
4 | import com.gyc.mapper.UserMapper;
5 | import com.gyc.service.UserService;
6 | import org.springframework.stereotype.Service;
7 |
8 | import javax.annotation.Resource;
9 | import java.util.List;
10 |
11 | @Service
12 | public class UserServiceImpl implements UserService {
13 | @Resource
14 | private UserMapper userMapper;
15 | @Override
16 | public List selectAll() {
17 | return userMapper.selectAll();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | druid:
4 | url: jdbc:mysql://localhost:3306/authority?useUnicode=true&characterEncoding=utf-8&useSSL=false
5 | username: root
6 | password: 123456
7 | driver-class-name: com.mysql.jdbc.Driver
8 | initial-size: 2
9 | max-active: 30
10 | min-idle: 2
11 | max-wait: 1234
12 | pool-prepared-statements: true
13 | max-pool-prepared-statement-per-connection-size: 5
14 | #mybatis\u914D\u7F6E
15 | mybatis:
16 | mapper-locations: classpath:mapper/*.xml
17 | type-aliases-package: com.example.demo.entity
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/main/resources/mapper/UserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
17 |
18 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/test/java/com/gyc/GycApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.gyc;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class GycApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/springboot-netty-mybatis/src/test/java/com/gyc/mapper/UserMapperTest.java:
--------------------------------------------------------------------------------
1 | package com.gyc.mapper;
2 |
3 | import com.gyc.entity.User;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 | import org.springframework.test.context.junit4.SpringRunner;
8 |
9 | import javax.annotation.Resource;
10 |
11 | import java.util.List;
12 |
13 | import static org.junit.Assert.*;
14 |
15 | @RunWith(SpringRunner.class)
16 | @SpringBootTest
17 | public class UserMapperTest {
18 | @Resource
19 | private UserMapper userMapper;
20 |
21 | @Test
22 | public void selectAll() {
23 | List users = userMapper.selectAll();
24 | System.out.println(users.size());
25 | }
26 | }
--------------------------------------------------------------------------------
/springboot-netty-socket/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.0.5.RELEASE
9 |
10 |
11 | com.example
12 | netty-socket
13 | 1.0
14 | netty-socket
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 | com.corundumstudio.socketio
34 | netty-socketio
35 | 1.7.11
36 |
37 |
38 |
39 |
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-maven-plugin
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/springboot-netty-socket/src/main/java/com/example/nettysocket/NettySocketApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.nettysocket;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class NettySocketApplication {
8 | public static void main(String[] args) {
9 | SpringApplication.run(NettySocketApplication.class, args);
10 | }
11 | }
12 |
13 |
--------------------------------------------------------------------------------
/springboot-netty-socket/src/main/java/com/example/nettysocket/PushServer.java:
--------------------------------------------------------------------------------
1 | package com.example.nettysocket;
2 |
3 | import com.corundumstudio.socketio.Configuration;
4 | import com.corundumstudio.socketio.SocketConfig;
5 | import com.corundumstudio.socketio.SocketIOServer;
6 | import com.example.nettysocket.listenner.EventListenner;
7 | import org.springframework.beans.factory.InitializingBean;
8 | import org.springframework.beans.factory.annotation.Value;
9 | import org.springframework.stereotype.Component;
10 |
11 | import javax.annotation.Resource;
12 |
13 | /**
14 | * @author gyc
15 | * @Title: PushServer
16 | * @ProjectName 项目
17 | * @Description: 消息推送服务端启动
18 | * @date 2019/1/2 15:17
19 | */
20 | @Component
21 | public class PushServer implements InitializingBean {
22 | @Resource
23 | private EventListenner eventListenner;
24 |
25 | @Value("${push.server.port}")
26 | private int serverPort;
27 | @Override
28 | public void afterPropertiesSet() throws Exception {
29 | Configuration config = new Configuration();
30 | config.setPort(serverPort);
31 |
32 | SocketConfig socketConfig = new SocketConfig();
33 | socketConfig.setReuseAddress(true);
34 | socketConfig.setTcpNoDelay(true);
35 | socketConfig.setSoLinger(0);
36 | config.setSocketConfig(socketConfig);
37 | config.setHostname("localhost");
38 |
39 | SocketIOServer server = new SocketIOServer(config);
40 | server.addListeners(eventListenner);
41 | server.start();
42 | System.out.println("启动正常");
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/springboot-netty-socket/src/main/java/com/example/nettysocket/cache/ClientCache.java:
--------------------------------------------------------------------------------
1 | package com.example.nettysocket.cache;
2 |
3 | import com.corundumstudio.socketio.SocketIOClient;
4 | import org.springframework.stereotype.Component;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 | import java.util.UUID;
9 | import java.util.concurrent.ConcurrentHashMap;
10 |
11 | /**
12 | * @author gyc
13 | * @Title: ClientCache
14 | * @ProjectName 项目
15 | * @Description: 本地缓存,用于保存客户端连接会话
16 | * @date 2019/1/9 15:13
17 | */
18 | @Component
19 | public class ClientCache {
20 |
21 | //本地缓存
22 | private static Map> concurrentHashMap=new ConcurrentHashMap<>();
23 |
24 | /**
25 | * 存入本地缓存
26 | * @param userId 用户ID
27 | * @param sessionId 页面sessionID
28 | * @param socketIOClient 页面对应的通道连接信息
29 | */
30 | public void saveClient(String userId, UUID sessionId,SocketIOClient socketIOClient){
31 | HashMap sessionIdClientCache=concurrentHashMap.get(userId);
32 | if(sessionIdClientCache==null){
33 | sessionIdClientCache = new HashMap<>();
34 | }
35 | sessionIdClientCache.put(sessionId,socketIOClient);
36 | concurrentHashMap.put(userId,sessionIdClientCache);
37 | }
38 |
39 | /**
40 | * 根据用户ID获取所有通道信息
41 | * @param userId
42 | * @return
43 | */
44 | public HashMap getUserClient(String userId){
45 | return concurrentHashMap.get(userId);
46 | }
47 |
48 | /**
49 | * 根据用户ID及页面sessionID删除页面链接信息
50 | * @param userId
51 | * @param sessionId
52 | */
53 | public void deleteSessionClient(String userId,UUID sessionId){
54 | concurrentHashMap.get(userId).remove(sessionId);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/springboot-netty-socket/src/main/java/com/example/nettysocket/controller/PushController.java:
--------------------------------------------------------------------------------
1 | package com.example.nettysocket.controller;
2 |
3 | import com.corundumstudio.socketio.SocketIOClient;
4 | import com.example.nettysocket.cache.ClientCache;
5 | import org.springframework.web.bind.annotation.GetMapping;
6 | import org.springframework.web.bind.annotation.PathVariable;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | import javax.annotation.Resource;
11 | import java.util.HashMap;
12 | import java.util.UUID;
13 |
14 | /**
15 | * @author gyc
16 | * @Title: PushController
17 | * @ProjectName springboot-study
18 | * @Description:
19 | * @date 2019/1/10 16:39
20 | */
21 | @RestController
22 | @RequestMapping("/push")
23 | public class PushController {
24 | @Resource
25 | private ClientCache clientCache;
26 |
27 | @GetMapping("/user/{userId}")
28 | public String pushTuUser(@PathVariable("userId") String userId){
29 | HashMap userClient = clientCache.getUserClient(userId);
30 | userClient.forEach((uuid, socketIOClient) -> {
31 | //向客户端推送消息
32 | socketIOClient.sendEvent("chatevent","服务端推送消息");
33 | });
34 | return "success";
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/springboot-netty-socket/src/main/java/com/example/nettysocket/listenner/EventListenner.java:
--------------------------------------------------------------------------------
1 | package com.example.nettysocket.listenner;
2 |
3 | import com.corundumstudio.socketio.AckRequest;
4 | import com.corundumstudio.socketio.SocketIOClient;
5 | import com.corundumstudio.socketio.annotation.OnConnect;
6 | import com.corundumstudio.socketio.annotation.OnDisconnect;
7 | import com.corundumstudio.socketio.annotation.OnEvent;
8 | import com.example.nettysocket.cache.ClientCache;
9 | import org.springframework.stereotype.Component;
10 |
11 | import javax.annotation.Resource;
12 | import java.util.UUID;
13 |
14 | /**
15 | * @author gyc
16 | * @Title: EventListenner
17 | * @ProjectName 项目
18 | * @Description: 推送服务端连接处理
19 | * @date 2019/1/2 15:19
20 | */
21 | @Component
22 | public class EventListenner {
23 | @Resource
24 | private ClientCache clientCache;
25 |
26 | /**
27 | * 客户端连接
28 | * @param client
29 | */
30 | @OnConnect
31 | public void onConnect(SocketIOClient client) {
32 | String userId = client.getHandshakeData().getSingleUrlParam("userId");
33 | UUID sessionId = client.getSessionId();
34 | clientCache.saveClient(userId,sessionId,client);
35 | System.out.println("建立连接");
36 | }
37 |
38 | /**
39 | * 客户端断开
40 | * @param client
41 | */
42 | @OnDisconnect
43 | public void onDisconnect(SocketIOClient client) {
44 | String userId = client.getHandshakeData().getSingleUrlParam("userId");
45 | clientCache.deleteSessionClient(userId,client.getSessionId());
46 | System.out.println("关闭连接");
47 | }
48 |
49 |
50 | //消息接收入口,当接收到消息后,查找发送目标客户端,并且向该客户端发送消息,且给自己发送消息
51 | // 暂未使用
52 | @OnEvent("messageevent")
53 | public void onEvent(SocketIOClient client, AckRequest request) {
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/springboot-netty-socket/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | push:
2 | server:
3 | port: 9092
--------------------------------------------------------------------------------
/springboot-netty-socket/src/test/java/com/example/nettysocket/TestOne.java:
--------------------------------------------------------------------------------
1 | package com.example.nettysocket;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.IOException;
6 | import java.io.OutputStream;
7 | import java.io.PrintWriter;
8 | import java.net.Socket;
9 |
10 | /**
11 | * @author gyc
12 | * @Title: TestOne
13 | * @ProjectName springboot-study
14 | * @Description:
15 | * @date 2018/12/25 10:30
16 | */
17 | public class TestOne {
18 |
19 | @Test
20 | public void testa(){
21 | try {
22 | Socket socket = new Socket("localhost", 9098);
23 | OutputStream outputStream = socket.getOutputStream();
24 | PrintWriter printWriter = new PrintWriter(outputStream);
25 | printWriter.write("test");
26 | printWriter.flush();
27 | socket.shutdownOutput();
28 | socket.close();
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/springboot-netty/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.gyc
7 | demo
8 | 1.0
9 | jar
10 |
11 | demo
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.4.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-test
36 | test
37 |
38 |
39 |
40 | io.netty
41 | netty-all
42 | 4.1.42.Final
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-maven-plugin
51 |
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/springboot-netty/src/main/java/com/gyc/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.gyc.demo;
2 |
3 | import com.gyc.demo.nettysocket.DiscardServer;
4 | import org.springframework.boot.CommandLineRunner;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 |
8 | import javax.annotation.Resource;
9 |
10 | @SpringBootApplication
11 | public class DemoApplication implements CommandLineRunner {
12 | @Resource
13 | private DiscardServer discardServer;
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(DemoApplication.class, args);
17 | }
18 |
19 | @Override
20 | public void run(String... args) throws Exception {
21 | discardServer.run(8080);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/springboot-netty/src/main/java/com/gyc/demo/nettysocket/ChildChannelHandler.java:
--------------------------------------------------------------------------------
1 | package com.gyc.demo.nettysocket;
2 |
3 |
4 | import io.netty.channel.ChannelInitializer;
5 | import io.netty.channel.socket.SocketChannel;
6 | import org.springframework.stereotype.Component;
7 |
8 | import javax.annotation.Resource;
9 |
10 | @Component
11 | public class ChildChannelHandler extends ChannelInitializer {
12 | @Resource
13 | private DiscardServerHandler discardServerHandler;
14 |
15 | public void initChannel(SocketChannel socketChannel) throws Exception {
16 | socketChannel.pipeline().addLast(discardServerHandler);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/springboot-netty/src/main/java/com/gyc/demo/nettysocket/DiscardServer.java:
--------------------------------------------------------------------------------
1 | package com.gyc.demo.nettysocket;
2 |
3 | import io.netty.bootstrap.ServerBootstrap;
4 | import io.netty.channel.ChannelFuture;
5 | import io.netty.channel.ChannelOption;
6 | import io.netty.channel.EventLoopGroup;
7 | import io.netty.channel.nio.NioEventLoopGroup;
8 | import io.netty.channel.socket.nio.NioServerSocketChannel;
9 | import org.springframework.stereotype.Component;
10 |
11 | import javax.annotation.Resource;
12 |
13 | /**
14 | * 丢弃任何进入的数据 启动服务端的DiscardServerHandler
15 | */
16 | @Component
17 | public class DiscardServer {
18 | @Resource
19 | private ChildChannelHandler childChannelHandler;
20 | public void run(int port) throws Exception {
21 | EventLoopGroup bossGroup = new NioEventLoopGroup();
22 | EventLoopGroup workerGroup = new NioEventLoopGroup();
23 | System.out.println("准备运行端口:" + port);
24 | try {
25 | ServerBootstrap bootstrap = new ServerBootstrap();
26 | bootstrap.group(bossGroup, workerGroup)
27 | .channel(NioServerSocketChannel.class)
28 | .option(ChannelOption.SO_BACKLOG, 128)
29 | .childHandler(childChannelHandler);
30 | //绑定端口,同步等待成功
31 | ChannelFuture f = bootstrap.bind(port).sync();
32 | //等待服务监听端口关闭
33 | f.channel().closeFuture().sync();
34 | } finally {
35 | //退出,释放线程资源
36 | workerGroup.shutdownGracefully();
37 | bossGroup.shutdownGracefully();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/springboot-netty/src/main/java/com/gyc/demo/nettysocket/DiscardServerHandler.java:
--------------------------------------------------------------------------------
1 | package com.gyc.demo.nettysocket;
2 |
3 | import com.example.demo.service.BaseService;
4 | import io.netty.buffer.ByteBuf;
5 | import io.netty.channel.ChannelHandler.Sharable;
6 | import io.netty.channel.ChannelHandlerAdapter;
7 | import io.netty.channel.ChannelHandlerContext;
8 | import io.netty.util.CharsetUtil;
9 | import io.netty.util.ReferenceCountUtil;
10 | import org.springframework.stereotype.Component;
11 |
12 | import javax.annotation.Resource;
13 |
14 | @Component
15 | @Sharable
16 | public class DiscardServerHandler extends ChannelHandlerAdapter {
17 | @Resource
18 | private BaseService baseService;
19 | @Override
20 | public void channelRead(ChannelHandlerContext ctx, Object msg) {
21 |
22 | try {
23 | ByteBuf in = (ByteBuf) msg;
24 | System.out.println("传输内容是");
25 | System.out.println(in.toString(CharsetUtil.UTF_8));
26 | //这里调用service服务
27 | baseService.test();
28 | } finally {
29 | ReferenceCountUtil.release(msg);
30 | }
31 | }
32 | @Override
33 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
34 | // 出现异常就关闭
35 | cause.printStackTrace();
36 | ctx.close();
37 | }
38 | }
39 |
40 |
--------------------------------------------------------------------------------
/springboot-netty/src/main/java/com/gyc/demo/service/BaseService.java:
--------------------------------------------------------------------------------
1 | package com.gyc.demo.service;
2 |
3 | public interface BaseService {
4 | /**
5 | * 测试接口
6 | */
7 | void test();
8 | }
9 |
--------------------------------------------------------------------------------
/springboot-netty/src/main/java/com/gyc/demo/service/impl/BaseServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.gyc.demo.service.impl;
2 |
3 | import com.gyc.demo.service.BaseService;
4 | import org.springframework.stereotype.Service;
5 |
6 | @Service
7 | public class BaseServiceImpl implements BaseService {
8 | @Override
9 | public void test() {
10 | System.out.println("aaaaa");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/springboot-netty/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guodayede/springboot-study/6c21b47ee9e30672fe70410502f3ee37454a0b85/springboot-netty/src/main/resources/application.properties
--------------------------------------------------------------------------------
/springboot-netty/src/test/java/com/gyc/demo/DemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.gyc.demo;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class DemoApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/springboot-redis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.0.RELEASE
9 |
10 |
11 | com.example
12 | redis-test
13 | 1.0
14 | redis-test
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-data-redis
25 |
26 |
27 |
28 | org.apache.commons
29 | commons-pool2
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-test
34 | test
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-starter-web
39 |
40 |
41 |
42 | org.databene
43 | contiperf
44 | RELEASE
45 | compile
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.springframework.boot
53 | spring-boot-maven-plugin
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/springboot-redis/src/main/java/com/example/redistest/RedisTestApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.redistest;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class RedisTestApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(RedisTestApplication.class, args);
11 | }
12 |
13 | }
14 |
15 |
--------------------------------------------------------------------------------
/springboot-redis/src/main/java/com/example/redistest/TestController.java:
--------------------------------------------------------------------------------
1 | package com.example.redistest;
2 |
3 | /**
4 | * @author gyc
5 | * @Title: TestController
6 | * @ProjectName springboot-study
7 | * @Description:
8 | * @date 2018/12/19 17:20
9 | */
10 |
11 | public class TestController {
12 | }
13 |
--------------------------------------------------------------------------------
/springboot-redis/src/main/java/com/example/redistest/model/User.java:
--------------------------------------------------------------------------------
1 | package com.example.redistest.model;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author gyc
7 | */
8 | public class User implements Serializable {
9 | private static final long serialVersionUID = 1592671233576716938L;
10 | private String userName;
11 | private Integer age;
12 |
13 | public String getUserName() {
14 | return userName;
15 | }
16 |
17 | public void setUserName(String userName) {
18 | this.userName = userName;
19 | }
20 |
21 | public Integer getAge() {
22 | return age;
23 | }
24 |
25 | public void setAge(Integer age) {
26 | this.age = age;
27 | }
28 |
29 | public User(String userName, Integer age) {
30 | this.userName = userName;
31 | this.age = age;
32 | }
33 |
34 | @Override
35 | public String toString() {
36 | return "User{" +
37 | "userName='" + userName + '\'' +
38 | ", age=" + age +
39 | '}';
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/springboot-redis/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | redis:
3 | host: localhost
4 | database: 0
5 | password: zzzz
6 | lettuce:
7 | pool:
8 | max-active: 8
9 | min-idle: 0
10 | max-wait: 10s
11 | max-idle: 8
12 | server:
13 | port: 8081
14 |
--------------------------------------------------------------------------------
/springboot-redis/src/test/java/com/example/redistest/RedisTestApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.redistest;
2 |
3 | import com.example.redistest.model.User;
4 | import org.databene.contiperf.PerfTest;
5 | import org.databene.contiperf.junit.ContiPerfRule;
6 | import org.junit.Rule;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.springframework.boot.test.context.SpringBootTest;
10 | import org.springframework.data.redis.core.RedisTemplate;
11 | import org.springframework.data.redis.core.StringRedisTemplate;
12 | import org.springframework.test.context.junit4.SpringRunner;
13 |
14 | import javax.annotation.Resource;
15 |
16 | @RunWith(SpringRunner.class)
17 | @SpringBootTest
18 | public class RedisTestApplicationTests {
19 | @Resource
20 | private StringRedisTemplate stringRedisTemplate;
21 | @Resource
22 | private RedisTemplate redisTemplate;
23 | @Rule
24 | public ContiPerfRule i = new ContiPerfRule();
25 |
26 | @Test
27 | @PerfTest(invocations = 1, threads = 1)
28 | public void test() {
29 |
30 | redisTemplate.opsForValue().set("aa",new User("测试",23));
31 | Object aa = redisTemplate.opsForValue().get("aa");
32 | System.out.println(aa.toString());
33 | }
34 |
35 | @Test
36 | public void contextLoads() {
37 | stringRedisTemplate.opsForValue().increment("aa");
38 | System.out.println(Integer.valueOf(stringRedisTemplate.opsForValue().get("aa")));
39 | }
40 |
41 | }
42 |
43 |
--------------------------------------------------------------------------------
/springboot-swagger/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.0.RELEASE
9 |
10 |
11 | com.example
12 | swagger
13 | 0.0.1-SNAPSHOT
14 | swagger
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-maven-plugin
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/springboot-swagger/src/main/java/com/example/swagger/SwaggerApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.swagger;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SwaggerApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SwaggerApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/springboot-swagger/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/springboot-swagger/src/test/java/com/example/swagger/SwaggerApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.swagger;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class SwaggerApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/springboot_date/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/springboot_date/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.7.RELEASE
9 |
10 |
11 | com.example
12 | springboot_date
13 | 0.0.1-SNAPSHOT
14 | springbootDate
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-test
34 | test
35 |
36 |
37 |
38 |
39 |
40 |
41 | org.springframework.boot
42 | spring-boot-maven-plugin
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/springboot_date/src/main/java/com/example/springboot_date/SpringbootDateApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot_date;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringbootDateApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringbootDateApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/springboot_date/src/main/java/com/example/springboot_date/controller/DateVerifyController.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot_date.controller;
2 |
3 |
4 | import com.example.springboot_date.model.DateModelNoAnnotation;
5 | import org.springframework.web.bind.annotation.*;
6 |
7 | import java.util.Date;
8 |
9 | /**
10 | * @author gyc
11 | * @title: DateVerifyController
12 | * @projectName test
13 | * @date 2019/8/2811:07
14 | * @description: TODO
15 | */
16 |
17 | @RestController
18 | @RequestMapping("/date")
19 | public class DateVerifyController {
20 | // 方式一
21 | @PostMapping("/no")
22 | public String dateUnNoAnnotation(DateModelNoAnnotation dateModelNoAnnotation){
23 | System.out.println(dateModelNoAnnotation.toString());
24 | return "SUCCESS";
25 | }
26 |
27 | // 方式二
28 | @PostMapping("/has")
29 | public String dateHasAnnotation(@RequestBody DateModelNoAnnotation dateModelNoAnnotation){
30 | System.out.println(dateModelNoAnnotation.toString());
31 | return "SUCCESS";
32 | }
33 | // 方式三
34 | @GetMapping("/param")
35 | public String dateParams(@RequestParam("id")Integer id, @RequestParam("receiveDate")Date receiveDate){
36 | System.out.println("id====="+id);
37 | System.out.println("receiveDate====="+receiveDate);
38 | System.out.println("receiveDate====="+receiveDate.getTime());
39 | return "SUCCESS";
40 | }
41 | // 方式四
42 | @GetMapping("/no/param")
43 | public String dateNoParams(Integer id,Date receiveDate){
44 | System.out.println("id====="+id);
45 | System.out.println("receiveDate====="+receiveDate);
46 | System.out.println("receiveDate====="+receiveDate.getTime());
47 | return "SUCCESS";
48 | }
49 | }
--------------------------------------------------------------------------------
/springboot_date/src/main/java/com/example/springboot_date/converter/CourseControllerHandler.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot_date.converter;
2 |
3 | import org.springframework.core.convert.support.GenericConversionService;
4 | import org.springframework.web.bind.WebDataBinder;
5 | import org.springframework.web.bind.annotation.ControllerAdvice;
6 | import org.springframework.web.bind.annotation.InitBinder;
7 |
8 | /**
9 | * @author gyc
10 | * @title: ControllerHandler
11 | * @projectName app
12 | * @date 2019/8/1914:37
13 | * @description: 将时间转换类应用到接口上
14 | */
15 | @ControllerAdvice
16 | public class CourseControllerHandler {
17 | @InitBinder
18 | public void initBinder(WebDataBinder binder) {
19 | GenericConversionService genericConversionService = (GenericConversionService) binder.getConversionService();
20 | if (genericConversionService != null) {
21 | genericConversionService.addConverter(new CourseDateConverter());
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/springboot_date/src/main/java/com/example/springboot_date/converter/CourseDateConverter.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot_date.converter;
2 |
3 | import org.springframework.core.convert.converter.Converter;
4 | import org.springframework.util.StringUtils;
5 |
6 | import java.text.SimpleDateFormat;
7 | import java.util.Date;
8 |
9 | /**
10 | * @author gyc
11 | * @title: DateConverter
12 | * @projectName app
13 | * @date 2019/8/1914:36
14 | * @description: 时间转换类
15 | */
16 | public class CourseDateConverter implements Converter {
17 | private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
18 | private static final String dateFormata = "yyyy-MM-dd HH:mm:ss";
19 | private static final String shortDateFormat = "yyyy-MM-dd";
20 | private static final String shortDateFormata = "yyyy/MM/dd";
21 | private static final String timeStampFormat = "^\\d+$";
22 |
23 | @Override
24 | public Date convert(String value) {
25 | if (StringUtils.isEmpty(value)) {
26 | return null;
27 | }
28 | value = value.trim();
29 | try {
30 | if (value.contains("-")) {
31 | SimpleDateFormat formatter;
32 | if (value.contains(":")) {
33 | //yyyy-MM-dd HH:mm:ss 格式
34 | formatter = new SimpleDateFormat(dateFormat);
35 | } else {
36 | //yyyy-MM-dd 格式
37 | formatter = new SimpleDateFormat(shortDateFormat);
38 | }
39 | return formatter.parse(value);
40 | } else if (value.matches(timeStampFormat)) {
41 | //时间戳
42 | Long lDate = new Long(value);
43 | return new Date(lDate);
44 | } else if (value.contains("/")) {
45 | SimpleDateFormat formatter;
46 | if (value.contains(":")) {
47 | // yyyy/MM/dd HH:mm:ss 格式
48 | formatter = new SimpleDateFormat(dateFormata);
49 | } else {
50 | // yyyy/MM/dd 格式
51 | formatter = new SimpleDateFormat(shortDateFormata);
52 | }
53 | return formatter.parse(value);
54 | }
55 | } catch (Exception e) {
56 | throw new RuntimeException(String.format("parser %s to Date fail", value));
57 | }
58 | throw new RuntimeException(String.format("parser %s to Date fail", value));
59 | }
60 | }
--------------------------------------------------------------------------------
/springboot_date/src/main/java/com/example/springboot_date/model/DateModelNoAnnotation.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot_date.model;
2 |
3 | import java.util.Date;
4 |
5 | /**
6 | * @author gyc
7 | * @title: DateModel
8 | * @projectName test
9 | * @date 2019/8/2811:08
10 | * @description: TODO
11 | */
12 | public class DateModelNoAnnotation {
13 | private Integer id;
14 |
15 | // @JsonFormat(timezone = "GMT+8",pattern = "yyyy/MM/dd HH:mm:ss")
16 | private Date receiveDate;
17 |
18 | public Integer getId() {
19 | return id;
20 | }
21 |
22 | public void setId(Integer id) {
23 | this.id = id;
24 | }
25 |
26 | public Date getReceiveDate() {
27 | return receiveDate;
28 | }
29 |
30 | public void setReceiveDate(Date receiveDate) {
31 | this.receiveDate = receiveDate;
32 | }
33 |
34 | @Override
35 | public String toString() {
36 | return "DateModel{" +
37 | "id=" + id +
38 | ", receiveDate=" + receiveDate +
39 | '}';
40 | }
41 | }
--------------------------------------------------------------------------------
/springboot_date/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/springboot_date/src/test/java/com/example/springboot_date/SpringbootDateApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot_date;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class SpringbootDateApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------