From 6317d877a75860a93155413a7349add3a65e6736 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Tue, 22 Oct 2019 19:34:49 +0800 Subject: [PATCH 1/3] =?UTF-8?q?SpringBoot=E6=95=B4=E5=90=88mail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-Mail/README.MD | 8 ++ Springboot-Mail/pom.xml | 56 ++++++++++ Springboot-Mail/pusa.png | Bin 0 -> 16323 bytes .../boot/mail/SpringBootMailApplication.java | 11 ++ .../boot/mail/controller/MailController.java | 83 +++++++++++++++ .../com/button/boot/mail/model/MailModel.java | 60 +++++++++++ .../button/boot/mail/service/MailService.java | 10 ++ .../mail/service/impl/MailServiceImpl.java | 97 ++++++++++++++++++ .../src/main/resources/application.yml | 29 ++++++ .../src/main/resources/logback.xml | 30 ++++++ .../main/resources/templates/resource.html | 11 ++ 11 files changed, 395 insertions(+) create mode 100644 Springboot-Mail/README.MD create mode 100644 Springboot-Mail/pom.xml create mode 100644 Springboot-Mail/pusa.png create mode 100644 Springboot-Mail/src/main/java/com/button/boot/mail/SpringBootMailApplication.java create mode 100644 Springboot-Mail/src/main/java/com/button/boot/mail/controller/MailController.java create mode 100644 Springboot-Mail/src/main/java/com/button/boot/mail/model/MailModel.java create mode 100644 Springboot-Mail/src/main/java/com/button/boot/mail/service/MailService.java create mode 100644 Springboot-Mail/src/main/java/com/button/boot/mail/service/impl/MailServiceImpl.java create mode 100644 Springboot-Mail/src/main/resources/application.yml create mode 100644 Springboot-Mail/src/main/resources/logback.xml create mode 100644 Springboot-Mail/src/main/resources/templates/resource.html diff --git a/Springboot-Mail/README.MD b/Springboot-Mail/README.MD new file mode 100644 index 0000000..707bc09 --- /dev/null +++ b/Springboot-Mail/README.MD @@ -0,0 +1,8 @@ +# 本案例实现Springboot整合mail实现邮件发送 +注:application.yml中的邮箱授权码不是登录邮箱的地址,需要登录邮箱后自行设置 +测试发送普通邮件:http://localhost:8080/mail/sendSimpleMail +测试发送html格式的邮件:http://localhost:8080/mail/sendHTMLMail +测试发送带有附件的邮件:http://localhost:8080/mail/sendAttachmentMail +测试镶嵌静态资源的邮件:http://localhost:8080/mail/sendInlineResourceMail + + diff --git a/Springboot-Mail/pom.xml b/Springboot-Mail/pom.xml new file mode 100644 index 0000000..e8af75a --- /dev/null +++ b/Springboot-Mail/pom.xml @@ -0,0 +1,56 @@ + + 4.0.0 + com.button + Springboot-Mail + 0.0.1-SNAPSHOT + jar + + + UTF-8 + UTF-8 + 1.8 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-devtools + true + true + + + + org.springframework.boot + spring-boot-starter-mail + + + org.springframework.boot + spring-boot-starter-freemarker + + + + + + org.springframework.boot + spring-boot-maven-plugin + + -Dfile.encoding=UTF-8 + true + + + + + + \ No newline at end of file diff --git a/Springboot-Mail/pusa.png b/Springboot-Mail/pusa.png new file mode 100644 index 0000000000000000000000000000000000000000..74c0c458772556c61b7a655ce431decc139b1409 GIT binary patch literal 16323 zcmeIZXH=72&^8J&C`ARNDJn%kniK`3lb}cuX@XQK3P=|a=_LfkMiUT}8Wf~MkS;A) zC@K)?5~M3Vv_K#PlCyc>dEP(Yx7Io9oPXc>!Gg`b?_FkJGjq-Cxt|;AYaL_cW~8B^ zIi{nnag&AyVhDa{=?{bdJds9nfnT&fH?^+Nly;q&0}ZH?x}G`>&4)OqojY`(&ETbN z;X^}n{L9`CZJTGl0}Tz#Tt`FQB)|qgLYt7SP)_snqp59vX>U!+kHtGdF~du5fBzCe zvL^&^T>L=A(#HphPE|&?z%VF_gkkHSp%2zghU;eOgB!wWIiS7i2Lon>Upn&7|3O=w zmd=FbhR;FAaLD&F{|8KzFS3HM#f1GXy-Dq$kOIm66d(?->Tt;X;!y<{EhGv;C6nsC zwzvI>C{OC<`f}KBN*WBvC|xuwT*-LsBLj;$X8`;4s3N_%TLq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O64NAx2Q^TbpBdcgsA7 zeX4?7o?N&;5Qs03{WHs9cr{nf-%yvU-yhQ)xVvsy_oAXG+;X^hZ6iXZUdu&tbIC0# z@Zwe}%kLA2Z>O)_zqg@34nro`_U~@^4l|AKe4|6%&$nq%-yIsP|2+Q}Llq6-!NYd5 z{(7j_to-x{nczFC(HR@|gbS&2;LuVDq-# zk>Qr0eAw{VZ_jU6@<#a}elV1zzomcDDL~W0eDbNUka)YM)%5yaFzc zG`NpN>^J`{BH4Gd%`Cvp^7lBi9C%$l07^ZNnRELN`^jEDI$rb9K1G4L0`BeVs(et> zC;b=@-)FMi2RR!<@FnXljl z-RSpirngBoot测试邮件发送(HTML格式)") + .append("

这里是发送邮件的文本内容!

"); + mailModel.setContent(sb.toString()); + mailService.sendHTMLMail(mailModel); + } catch (Exception e) { + LOGGER.error("sendHTMLMail邮件发送出现异常. ex={}", e); + } + } + + @GetMapping(value = "/sendAttachmentMail") + public void sendAttachmentMail() { + try { + MailModel mailModel = new MailModel(); + mailModel.setFrom(MAIL_FROM); + mailModel.setTo(MAIL_TO); + mailModel.setSubject("sendAttachmentMail"); + mailModel.setContent("这是一封带有附件的邮件!!"); + mailModel.setResourcePath("pusa.png"); + mailService.sendAttachmentMail(mailModel); + } catch (Exception e) { + LOGGER.error("sendAttachmentMail邮件发送出现异常. ex={}", e); + } + } + + @GetMapping(value = "/sendInlineResourceMail") + public void sendInlineResourceMail() { + try { + MailModel mailModel = new MailModel(); + mailModel.setFrom(MAIL_FROM); + String[] mailTo = {MAIL_TO, MAIL_TO2}; + mailModel.setTo(mailTo); + mailModel.setSubject("sendInlineResourceMail"); + mailModel.setContent("这是一封镶嵌静态资源的邮件!!"); + mailModel.setResourcePath("src/main/resources/templates/resource.html"); + mailModel.setResourceId("静态资源"); + mailService.sendInlineResourceMail(mailModel); + } catch (Exception e) { + LOGGER.error("sendAttachmentMail邮件发送出现异常. ex={}", e); + } + } +} diff --git a/Springboot-Mail/src/main/java/com/button/boot/mail/model/MailModel.java b/Springboot-Mail/src/main/java/com/button/boot/mail/model/MailModel.java new file mode 100644 index 0000000..eb933c2 --- /dev/null +++ b/Springboot-Mail/src/main/java/com/button/boot/mail/model/MailModel.java @@ -0,0 +1,60 @@ +package com.button.boot.mail.model; + +import java.io.Serializable; +import java.util.Arrays; + +public class MailModel implements Serializable { + private static final long serialVersionUID = 1L; + + private String from; + private String[] to; + private String subject; + private String content; + private String resourcePath; //附件路径,资源等的路径 + private String resourceId; // + + public String getFrom() { + return from; + } + public void setFrom(String from) { + this.from = from; + } + public String[] getTo() { + return this.to; + } + public void setTo(String to) { + this.to = new String[] {to}; + } + public void setTo(String... to) { + this.to = to; + } + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getContent() { + return content; + } + public void setContent(String content) { + this.content = content; + } + public String getResourcePath() { + return resourcePath; + } + public void setResourcePath(String resourcePath) { + this.resourcePath = resourcePath; + } + public String getResourceId() { + return resourceId; + } + public void setResourceId(String resourceId) { + this.resourceId = resourceId; + } + @Override + public String toString() { + return "MailModel [from=" + from + ", to=" + Arrays.toString(to) + ", subject=" + subject + ", content=" + + content + ", resourcePath=" + resourcePath + ", resourceId=" + resourceId + "]"; + } +} diff --git a/Springboot-Mail/src/main/java/com/button/boot/mail/service/MailService.java b/Springboot-Mail/src/main/java/com/button/boot/mail/service/MailService.java new file mode 100644 index 0000000..1892a44 --- /dev/null +++ b/Springboot-Mail/src/main/java/com/button/boot/mail/service/MailService.java @@ -0,0 +1,10 @@ +package com.button.boot.mail.service; + +import com.button.boot.mail.model.MailModel; + +public interface MailService { + void sendSimpleMail(MailModel mailModel); + void sendHTMLMail(MailModel mailModel); + void sendAttachmentMail(MailModel mailModel); + void sendInlineResourceMail(MailModel mailModel); +} diff --git a/Springboot-Mail/src/main/java/com/button/boot/mail/service/impl/MailServiceImpl.java b/Springboot-Mail/src/main/java/com/button/boot/mail/service/impl/MailServiceImpl.java new file mode 100644 index 0000000..ba62cbb --- /dev/null +++ b/Springboot-Mail/src/main/java/com/button/boot/mail/service/impl/MailServiceImpl.java @@ -0,0 +1,97 @@ +package com.button.boot.mail.service.impl; + +import java.io.File; + +import javax.mail.internet.MimeMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +import com.button.boot.mail.model.MailModel; +import com.button.boot.mail.service.MailService; + +@Service +public class MailServiceImpl implements MailService { + private static final Logger LOGGER = LoggerFactory.getLogger(MailServiceImpl.class); + + @Autowired + private JavaMailSender javaMailSender; + + @Override + public void sendSimpleMail(MailModel mailModel) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(mailModel.getFrom()); + message.setTo(mailModel.getTo()); + message.setSubject(mailModel.getSubject()); + message.setText(mailModel.getContent()); + try { + javaMailSender.send(message); + } catch (Exception e) { + LOGGER.error("sendSimpleMail发送邮件时发生异常. ex={}", e); + } + } + + @Override + public void sendHTMLMail(MailModel mailModel) { + MimeMessage message = javaMailSender.createMimeMessage(); + try { + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(mailModel.getFrom()); + helper.setTo(mailModel.getTo()); + helper.setSubject(mailModel.getSubject()); + helper.setText(mailModel.getContent(), true); + + javaMailSender.send(message); + LOGGER.info("html邮件发送成功"); + } catch (Exception e) { + LOGGER.error("sendHTMLMail发送邮件时发生异. ex={}", e); + } + } + + @Override + public void sendAttachmentMail(MailModel mailModel) { + MimeMessage mimeMailMessage = null; + try { + mimeMailMessage = javaMailSender.createMimeMessage(); + MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); + mimeMessageHelper.setFrom(mailModel.getFrom()); + mimeMessageHelper.setTo(mailModel.getTo()); + mimeMessageHelper.setSubject(mailModel.getSubject()); + mimeMessageHelper.setText(mailModel.getContent()); + //文件路径 + FileSystemResource resource = new FileSystemResource(new File(mailModel.getResourcePath())); + String fileName = new File(mailModel.getResourcePath()).getName(); + mimeMessageHelper.addAttachment(fileName, resource); + + javaMailSender.send(mimeMailMessage); + } catch (Exception e) { + LOGGER.error("sendAttachmentMail发送邮件时出现异常. ex={}", e); + } + } + + @Override + public void sendInlineResourceMail(MailModel mailModel){ + MimeMessage message = javaMailSender.createMimeMessage(); + + try { + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(mailModel.getFrom()); + helper.setTo(mailModel.getTo()); + helper.setSubject(mailModel.getSubject()); + helper.setText(mailModel.getContent(), true); + + FileSystemResource res = new FileSystemResource(new File(mailModel.getResourcePath())); + helper.addInline(mailModel.getResourceId(), res); + + javaMailSender.send(message); + } catch (Exception e) { + LOGGER.error("sendInlineResourceMail发送邮件时出现异常. ex={}", e); + } + } +} diff --git a/Springboot-Mail/src/main/resources/application.yml b/Springboot-Mail/src/main/resources/application.yml new file mode 100644 index 0000000..8c5f0de --- /dev/null +++ b/Springboot-Mail/src/main/resources/application.yml @@ -0,0 +1,29 @@ +server: + servlet: + context-path: /mail + port: 8080 + uri-encoding: utf-8 + +logging: + file: logback.xml + +spring: + mail: + host: smtp.163.com + username: **********@163.com #邮箱地址 + password: ********* #邮箱授权码 + default-encoding: UTF-8 + properties: + mail: + smtp: + auth: true + smtp: + starttls: + enable: true + required: true + freemarker: + content-type: text/html + suffix: .ftl + template-loader-path: classpath:/templates + charset: UTF-8 + check-template-location: true diff --git a/Springboot-Mail/src/main/resources/logback.xml b/Springboot-Mail/src/main/resources/logback.xml new file mode 100644 index 0000000..b7bf852 --- /dev/null +++ b/Springboot-Mail/src/main/resources/logback.xml @@ -0,0 +1,30 @@ + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + ${LOG_HOME}/springboot_mail.log + + + ${LOG_HOME}/springboot_mail.log.%d{yyyy-MM-dd}.%i.log + + + 100MB + + 10 + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + \ No newline at end of file diff --git a/Springboot-Mail/src/main/resources/templates/resource.html b/Springboot-Mail/src/main/resources/templates/resource.html new file mode 100644 index 0000000..48b2624 --- /dev/null +++ b/Springboot-Mail/src/main/resources/templates/resource.html @@ -0,0 +1,11 @@ + + + + + mail + + +

您好,这是验证邮件,请点击下面的链接完成验证,


+激活账号 + + -- Gitee From 00bb6c819ead8a0c0a09b04500911b67bc3e5a0e Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Tue, 22 Oct 2019 19:48:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?SpringBoot=E6=95=B4=E5=90=88Netty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-Netty-Parent/README.MD | 1 + .../Springboot-Netty-Client/README.MD | 1 + .../Springboot-Netty-Client/pom.xml | 32 ++++++++++++ .../SpringbootNettyClientApplication.java | 16 ++++++ .../client/netty/NettyClientApplication.java | 42 ++++++++++++++++ .../channel/HelloWorldChannelInitializer.java | 18 +++++++ .../netty/handler/HelloWorldHandler.java | 27 ++++++++++ .../src/main/resources/application.yml | 9 ++++ .../src/main/resources/logback.xml | 34 +++++++++++++ .../Springboot-Netty-Server/README.MD | 1 + .../Springboot-Netty-Server/pom.xml | 31 ++++++++++++ .../SpringbootNettyServerApplication.java | 18 +++++++ .../server/netty/NettyServerApplication.java | 50 +++++++++++++++++++ .../channel/HelloWorldChannelInitializer.java | 19 +++++++ .../netty/handler/HelloWorldHandler.java | 37 ++++++++++++++ .../src/main/resources/application.yml | 9 ++++ .../src/main/resources/logback.xml | 34 +++++++++++++ Springboot-Netty-Parent/pom.xml | 45 +++++++++++++++++ 18 files changed, 424 insertions(+) create mode 100644 Springboot-Netty-Parent/README.MD create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/README.MD create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/pom.xml create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/SpringbootNettyClientApplication.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/NettyClientApplication.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/channel/HelloWorldChannelInitializer.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/handler/HelloWorldHandler.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/application.yml create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/logback.xml create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/README.MD create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/pom.xml create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/SpringbootNettyServerApplication.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/NettyServerApplication.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/channel/HelloWorldChannelInitializer.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/handler/HelloWorldHandler.java create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/application.yml create mode 100644 Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/logback.xml create mode 100644 Springboot-Netty-Parent/pom.xml diff --git a/Springboot-Netty-Parent/README.MD b/Springboot-Netty-Parent/README.MD new file mode 100644 index 0000000..771a215 --- /dev/null +++ b/Springboot-Netty-Parent/README.MD @@ -0,0 +1 @@ +# 本案例实现Springboot整合Netty \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/README.MD b/Springboot-Netty-Parent/Springboot-Netty-Client/README.MD new file mode 100644 index 0000000..6a0a9e1 --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/README.MD @@ -0,0 +1 @@ +# netty客户端 \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/pom.xml b/Springboot-Netty-Parent/Springboot-Netty-Client/pom.xml new file mode 100644 index 0000000..8df7ddb --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.button + Springboot-Netty-Parent + 0.0.1-SNAPSHOT + + Springboot-Netty-Client + + + + org.springframework.boot + spring-boot-devtools + true + true + + + + + + org.springframework.boot + spring-boot-maven-plugin + + -Dfile.encoding=UTF-8 + true + + + + + \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/SpringbootNettyClientApplication.java b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/SpringbootNettyClientApplication.java new file mode 100644 index 0000000..1d27ec4 --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/SpringbootNettyClientApplication.java @@ -0,0 +1,16 @@ +package com.button.boot.netty.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.button.boot.netty.client.netty.NettyClientApplication; + +@SpringBootApplication +public class SpringbootNettyClientApplication { + public static void main(String[] args) { + SpringApplication.run(SpringbootNettyClientApplication.class, args); + //启动netty客户端 + NettyClientApplication nettyClient = new NettyClientApplication(); + nettyClient.start(); + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/NettyClientApplication.java b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/NettyClientApplication.java new file mode 100644 index 0000000..ce3c771 --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/NettyClientApplication.java @@ -0,0 +1,42 @@ +package com.button.boot.netty.client.netty; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.button.boot.netty.client.netty.channel.HelloWorldChannelInitializer; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; + +@Component +public class NettyClientApplication { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyClientApplication.class); + + public void start() { + EventLoopGroup group = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap() + .group(group) + // 该参数的作用就是禁止使用Nagle算法,使用于小数据即时传输 + .option(ChannelOption.TCP_NODELAY, true) + .channel(NioSocketChannel.class) + .handler(new HelloWorldChannelInitializer()); + + try { + ChannelFuture future = bootstrap.connect("127.0.0.1", 8088).sync(); + LOGGER.info("客户端成功...."); + // 发送消息 + future.channel().writeAndFlush("你好啊"); + // 等待连接被关闭 + future.channel().closeFuture().sync(); + } catch (Exception e) { + LOGGER.error("初始化客户端时出现异常. ex={}", e); + } finally { + group.shutdownGracefully(); + } + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/channel/HelloWorldChannelInitializer.java b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/channel/HelloWorldChannelInitializer.java new file mode 100644 index 0000000..7dc2a4d --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/channel/HelloWorldChannelInitializer.java @@ -0,0 +1,18 @@ +package com.button.boot.netty.client.netty.channel; + +import com.button.boot.netty.client.netty.handler.HelloWorldHandler; + +import io.netty.channel.ChannelInitializer; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; + +public class HelloWorldChannelInitializer extends ChannelInitializer { + + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast("decoder", new StringDecoder()); + socketChannel.pipeline().addLast("encoder", new StringEncoder()); + socketChannel.pipeline().addLast(new HelloWorldHandler()); + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/handler/HelloWorldHandler.java b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/handler/HelloWorldHandler.java new file mode 100644 index 0000000..3c69ecf --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/java/com/button/boot/netty/client/netty/handler/HelloWorldHandler.java @@ -0,0 +1,27 @@ +package com.button.boot.netty.client.netty.handler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +public class HelloWorldHandler extends ChannelInboundHandlerAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(HelloWorldHandler.class); + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("客户端Active ....."); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + LOGGER.info("客户端收到消息: {}", msg.toString()); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + cause.printStackTrace(); + ctx.close(); + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/application.yml b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/application.yml new file mode 100644 index 0000000..b255b6b --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + servlet: + context-path: /nClient + port: 8081 + uri-encoding: utf-8 + +logging: + file: logback.xml + \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/logback.xml b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/logback.xml new file mode 100644 index 0000000..53285fd --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Client/src/main/resources/logback.xml @@ -0,0 +1,34 @@ + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + ${LOG_HOME}/springboot_netty_client.log + + + ${LOG_HOME}/springboot_netty_client.log.%d{yyyy-MM-dd}.%i.log + + + 100MB + + + 10 + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + + \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/README.MD b/Springboot-Netty-Parent/Springboot-Netty-Server/README.MD new file mode 100644 index 0000000..71b7093 --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/README.MD @@ -0,0 +1 @@ +# netty服务端 \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/pom.xml b/Springboot-Netty-Parent/Springboot-Netty-Server/pom.xml new file mode 100644 index 0000000..6bd75e0 --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + + com.button + Springboot-Netty-Parent + 0.0.1-SNAPSHOT + + Springboot-Netty-Server + + + org.springframework.boot + spring-boot-devtools + true + true + + + + + + org.springframework.boot + spring-boot-maven-plugin + + -Dfile.encoding=UTF-8 + true + + + + + \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/SpringbootNettyServerApplication.java b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/SpringbootNettyServerApplication.java new file mode 100644 index 0000000..d2723b8 --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/SpringbootNettyServerApplication.java @@ -0,0 +1,18 @@ +package com.button.boot.netty.server; + +import java.net.InetSocketAddress; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.button.boot.netty.server.netty.NettyServerApplication; + +@SpringBootApplication +public class SpringbootNettyServerApplication { + public static void main(String[] args) { + SpringApplication.run(SpringbootNettyServerApplication.class, args); + //启动服务端 + NettyServerApplication nettyServer = new NettyServerApplication(); + nettyServer.start(new InetSocketAddress("127.0.0.1", 8088)); + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/NettyServerApplication.java b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/NettyServerApplication.java new file mode 100644 index 0000000..c397c9a --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/NettyServerApplication.java @@ -0,0 +1,50 @@ +package com.button.boot.netty.server.netty; + +import java.net.InetSocketAddress; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.button.boot.netty.server.netty.channel.HelloWorldChannelInitializer; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +@Component +public class NettyServerApplication { + private static final Logger LOGGER = LoggerFactory.getLogger(NettyServerApplication.class); + + public void start(InetSocketAddress socketAddress) { + //new 一个主线程组 + EventLoopGroup bossGroup = new NioEventLoopGroup(1); + //new 一个工作线程组 + EventLoopGroup workGroup = new NioEventLoopGroup(200); + ServerBootstrap bootstrap = new ServerBootstrap() + .group(bossGroup, workGroup) + .channel(NioServerSocketChannel.class) + .childHandler(new HelloWorldChannelInitializer()) + .localAddress(socketAddress) + //设置队列大小 + .option(ChannelOption.SO_BACKLOG, 1024) + // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 + .childOption(ChannelOption.SO_KEEPALIVE, true); + //绑定端口,开始接收进来的连接 + try { + ChannelFuture future = bootstrap.bind(socketAddress).sync(); + LOGGER.info("服务器启动开始监听端口: {}", socketAddress.getPort()); + future.channel().closeFuture().sync(); + } catch (Exception e) { + LOGGER.error("初始化netty服务端时出现异常. ex={}", e); + } finally { + //关闭主线程组 + bossGroup.shutdownGracefully(); + //关闭工作线程组 + workGroup.shutdownGracefully(); + } + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/channel/HelloWorldChannelInitializer.java b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/channel/HelloWorldChannelInitializer.java new file mode 100644 index 0000000..cd2e0df --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/channel/HelloWorldChannelInitializer.java @@ -0,0 +1,19 @@ +package com.button.boot.netty.server.netty.channel; + +import com.button.boot.netty.server.netty.handler.HelloWorldHandler; + +import io.netty.channel.ChannelInitializer; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; +import io.netty.util.CharsetUtil; + +public class HelloWorldChannelInitializer extends ChannelInitializer { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + //添加编解码 + socketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); + socketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); + socketChannel.pipeline().addLast(new HelloWorldHandler()); + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/handler/HelloWorldHandler.java b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/handler/HelloWorldHandler.java new file mode 100644 index 0000000..9dce6fd --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/java/com/button/boot/netty/server/netty/handler/HelloWorldHandler.java @@ -0,0 +1,37 @@ +package com.button.boot.netty.server.netty.handler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +public class HelloWorldHandler extends ChannelInboundHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(HelloWorldHandler.class); + /** + * 客户端连接会触发 + */ + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + LOGGER.info("Channel active......"); + } + + /** + * 客户端发消息会触发 + */ + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + LOGGER.info("服务器收到消息: {}", msg.toString()); + ctx.write("你也好哦"); + ctx.flush(); + } + + /** + * 发生异常触发 + */ + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + cause.printStackTrace(); + ctx.close(); + } +} diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/application.yml b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/application.yml new file mode 100644 index 0000000..8dd1dc2 --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + servlet: + context-path: /nServer + port: 8080 + uri-encoding: utf-8 + +logging: + file: logback.xml + \ No newline at end of file diff --git a/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/logback.xml b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/logback.xml new file mode 100644 index 0000000..8bcf8eb --- /dev/null +++ b/Springboot-Netty-Parent/Springboot-Netty-Server/src/main/resources/logback.xml @@ -0,0 +1,34 @@ + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + ${LOG_HOME}/springboot_netty_server.log + + + ${LOG_HOME}/springboot_netty_server.log.%d{yyyy-MM-dd}.%i.log + + + 100MB + + + 10 + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + + \ No newline at end of file diff --git a/Springboot-Netty-Parent/pom.xml b/Springboot-Netty-Parent/pom.xml new file mode 100644 index 0000000..5033631 --- /dev/null +++ b/Springboot-Netty-Parent/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + com.button + Springboot-Netty-Parent + 0.0.1-SNAPSHOT + pom + + Springboot-Netty-Server + Springboot-Netty-Client + + + UTF-8 + UTF-8 + 1.8 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + + + io.netty + netty-all + + + + org.springframework.boot + spring-boot-starter-test + test + + + \ No newline at end of file -- Gitee From 97bcd5c838433a12c92294cdfe49e4d31b1b2835 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Tue, 22 Oct 2019 19:49:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d0a8eb9..8ca7d33 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,6 @@ SpringBoot应用集合 - [SpringBoot整合Httpclient](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Httpclient) - [SpringBoot-Admin实现监控](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/SpringBoot-Admin-Parent) - [SpringBoot不同环境打包配置](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Springsession) -- [SpringBoot利用springsession实现session共享](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Springsession) \ No newline at end of file +- [SpringBoot利用springsession实现session共享](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Springsession) +- [SpringBoot整合mail](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Mail) +- [SpringBoot整合netty](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Netty-Parent) \ No newline at end of file -- Gitee