From 5fa0df71d63068a8cea46f520f36dd6be427b610 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Tue, 24 Sep 2019 20:10:44 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Springboot=E6=95=B4=E5=90=88dubbo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 4 -- README.md | 1 + SpringBoot-Dubbo/README.md | 4 ++ .../SpringBoot-Dubbo-Interface/pom.xml | 25 ++++++++ .../com/boot/dubbo/inter/UserService.java | 7 +++ .../java/com/boot/dubbo/model/UserModel.java | 32 ++++++++++ .../SpringBoot-Dubbo-Consumer/README.MD | 1 + .../SpringBoot-Dubbo-Consumer/pom.xml | 18 ++++++ .../SpringBootDubboConsumerApplication.java | 14 +++++ .../consumer/controller/UserController.java | 22 +++++++ .../src/main/resources/application.yml | 11 ++++ .../SpringBoot-Dubbo-Provider/pom.xml | 9 +++ .../SpringBootDubboProviderApplication.java | 16 +++++ .../service/impl/UserServiceImpl.java | 21 +++++++ .../src/main/resources/application.yml | 19 ++++++ .../SpringBoot-Dubbo-Parent/pom.xml | 62 +++++++++++++++++++ 16 files changed, 262 insertions(+), 4 deletions(-) delete mode 100644 README.en.md create mode 100644 SpringBoot-Dubbo/README.md create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/pom.xml create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/inter/UserService.java create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/model/UserModel.java create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/README.MD create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/pom.xml create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/SpringBootDubboConsumerApplication.java create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/controller/UserController.java create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/resources/application.yml create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/pom.xml create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/SpringBootDubboProviderApplication.java create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/service/impl/UserServiceImpl.java create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/resources/application.yml create mode 100644 SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/pom.xml diff --git a/README.en.md b/README.en.md deleted file mode 100644 index 2d05399..0000000 --- a/README.en.md +++ /dev/null @@ -1,4 +0,0 @@ -# SpringBoot-Components - -#### Description -SpringBoot应用集合 diff --git a/README.md b/README.md index d6af277..337e8fb 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,4 @@ SpringBoot应用集合 - [SpringBoot整合Listener](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Listener) - [SpringBoot对多线程的支持](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-MultiThread) - [SpringBoot整合Swagger](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Swagger) +- [SpringBoot整合Dubbo](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Dubbo) \ No newline at end of file diff --git a/SpringBoot-Dubbo/README.md b/SpringBoot-Dubbo/README.md new file mode 100644 index 0000000..b5a1bb2 --- /dev/null +++ b/SpringBoot-Dubbo/README.md @@ -0,0 +1,4 @@ +# SpringBoot-Dubbo + +#### 介绍 +SpringBoot整合Dubbo \ No newline at end of file diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/pom.xml b/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/pom.xml new file mode 100644 index 0000000..e179a0c --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + com.button + SpringBoot-Dubbo-Interface + 0.0.1-SNAPSHOT + jar + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + UTF-8 + + + + + \ No newline at end of file diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/inter/UserService.java b/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/inter/UserService.java new file mode 100644 index 0000000..59d5643 --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/inter/UserService.java @@ -0,0 +1,7 @@ +package com.boot.dubbo.inter; + +import com.boot.dubbo.model.UserModel; + +public interface UserService { + UserModel getUser(); +} diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/model/UserModel.java b/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/model/UserModel.java new file mode 100644 index 0000000..1770b36 --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Interface/src/main/java/com/boot/dubbo/model/UserModel.java @@ -0,0 +1,32 @@ +package com.boot.dubbo.model; + +import java.io.Serializable; + +public class UserModel implements Serializable { + private static final long serialVersionUID = 1L; + + private String name; + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return "UserModel [name=" + name + ", age=" + age + "]"; + } + +} diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/README.MD b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/README.MD new file mode 100644 index 0000000..267ae70 --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/README.MD @@ -0,0 +1 @@ +http://localhost:8081/getUser \ No newline at end of file diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/pom.xml b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/pom.xml new file mode 100644 index 0000000..36d970b --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/pom.xml @@ -0,0 +1,18 @@ + + 4.0.0 + + com.button + SpringBoot-Dubbo-Parent + 0.0.1-SNAPSHOT + + SpringBoot-Dubbo-Consumer + + + + org.springframework.boot + spring-boot-starter-web + + + \ No newline at end of file diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/SpringBootDubboConsumerApplication.java b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/SpringBootDubboConsumerApplication.java new file mode 100644 index 0000000..5e3d39b --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/SpringBootDubboConsumerApplication.java @@ -0,0 +1,14 @@ +package com.boot.dubbo.consumer; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; + +@EnableDubbo +@SpringBootApplication +public class SpringBootDubboConsumerApplication { + public static void main(String[] args) { + SpringApplication.run(SpringBootDubboConsumerApplication.class, args); + } +} diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/controller/UserController.java b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/controller/UserController.java new file mode 100644 index 0000000..82f801d --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/java/com/boot/dubbo/consumer/controller/UserController.java @@ -0,0 +1,22 @@ +package com.boot.dubbo.consumer.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.alibaba.dubbo.config.annotation.Reference; +import com.boot.dubbo.inter.UserService; +import com.boot.dubbo.model.UserModel; + +@RestController +public class UserController { + + @Reference + UserService userService; + + @ResponseBody // 以json格式返回 + @RequestMapping("/getUser") + public UserModel initOrder() { + return userService.getUser(); + } +} diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/resources/application.yml b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/resources/application.yml new file mode 100644 index 0000000..9bdee76 --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Consumer/src/main/resources/application.yml @@ -0,0 +1,11 @@ +#避免和监控中心端口冲突,设为8081端口访问 +server: + port: 8081 + +dubbo: + application: + name: order-service-consumer + registry: + address: zookeeper://94.191.97.200:2181 + monitor: + protocol: registry \ No newline at end of file diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/pom.xml b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/pom.xml new file mode 100644 index 0000000..01c869f --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/pom.xml @@ -0,0 +1,9 @@ + + 4.0.0 + + com.button + SpringBoot-Dubbo-Parent + 0.0.1-SNAPSHOT + + SpringBoot-Dubbo-Provider + \ No newline at end of file diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/SpringBootDubboProviderApplication.java b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/SpringBootDubboProviderApplication.java new file mode 100644 index 0000000..8a5749f --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/SpringBootDubboProviderApplication.java @@ -0,0 +1,16 @@ +package com.boot.dubbo.provider; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; + +//开启基于注解的dubbo功能(主要是包扫描@DubboComponentScan) +//也可以在配置文件中使用dubbo.scan.base-package来替代 @EnableDubbo +@EnableDubbo +@SpringBootApplication +public class SpringBootDubboProviderApplication { + public static void main(String[] args) { + SpringApplication.run(SpringBootDubboProviderApplication.class, args); + } +} diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/service/impl/UserServiceImpl.java b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..613863b --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/java/com/boot/dubbo/provider/service/impl/UserServiceImpl.java @@ -0,0 +1,21 @@ +package com.boot.dubbo.provider.service.impl; + +import org.springframework.stereotype.Component; + +import com.alibaba.dubbo.config.annotation.Service; +import com.boot.dubbo.inter.UserService; +import com.boot.dubbo.model.UserModel; + +@Service(version = "1.0.0",timeout = 3000) //属于Dubbo的@Service注解,非Spring 作用:暴露服务,配上版本号1.0.0说明向zookeeper注册的是版本为1.0.0的TestService接口,超时时长为3000ms等信息 +@Component +public class UserServiceImpl implements UserService { + + @Override + public UserModel getUser() { + UserModel user = new UserModel(); + user.setAge(25); + user.setName("张三"); + return user; + } + +} diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/resources/application.yml b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/resources/application.yml new file mode 100644 index 0000000..56c07e7 --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/SpringBoot-Dubbo-Provider/src/main/resources/application.yml @@ -0,0 +1,19 @@ +server: + port: 8080 +#当前服务/应用的名字 +dubbo: + application: + name: user-service-provider +#注册中心的协议和地址 + registry: + protocol: zookeeper + address: 94.191.97.200:2181 + +#通信规则(通信协议和接口) + protocol: + name: dubbo + port: 20880 + +#连接监控中心 + monitor: + protocol: registry \ No newline at end of file diff --git a/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/pom.xml b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/pom.xml new file mode 100644 index 0000000..68026e7 --- /dev/null +++ b/SpringBoot-Dubbo/SpringBoot-Dubbo-Parent/pom.xml @@ -0,0 +1,62 @@ + + 4.0.0 + com.button + SpringBoot-Dubbo-Parent + 0.0.1-SNAPSHOT + pom + + + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + + + + + + + com.alibaba.boot + dubbo-spring-boot-starter + 0.2.0 + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + com.button + SpringBoot-Dubbo-Interface + 0.0.1-SNAPSHOT + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + UTF-8 + + + + + + SpringBoot-Dubbo-Provider + SpringBoot-Dubbo-Consumer + + \ No newline at end of file -- Gitee From 3bcb39643ace3d46336d712bb40ca3870b8ea0e8 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Tue, 24 Sep 2019 20:11:59 +0800 Subject: [PATCH 2/7] =?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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 337e8fb..e57b27c 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ SpringBoot应用集合 - [SpringBoot整合Listener](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Listener) - [SpringBoot对多线程的支持](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-MultiThread) - [SpringBoot整合Swagger](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Swagger) -- [SpringBoot整合Dubbo](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Dubbo) \ No newline at end of file +- [SpringBoot整合Dubbo](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/SpringBoot-Dubbo) \ No newline at end of file -- Gitee From 17c724375069bb21ae8a31f6cce688704aa5b5b2 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Thu, 26 Sep 2019 19:37:39 +0800 Subject: [PATCH 3/7] =?UTF-8?q?SpringBoot=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-Exception/README.MD | 4 ++ Springboot-Exception/pom.xml | 50 +++++++++++++++ .../SpringBootExceptionApplication.java | 11 ++++ .../exception/config/ExceptionHandle.java | 61 +++++++++++++++++++ .../controller/ExceptionController.java | 21 +++++++ .../exception/model/ExceptionHtmlModel.java | 30 +++++++++ .../boot/exception/model/ExceptionModel.java | 30 +++++++++ .../src/main/resources/application.yml | 9 +++ .../src/main/resources/logback.xml | 30 +++++++++ 9 files changed, 246 insertions(+) create mode 100644 Springboot-Exception/README.MD create mode 100644 Springboot-Exception/pom.xml create mode 100644 Springboot-Exception/src/main/java/com/button/boot/exception/SpringBootExceptionApplication.java create mode 100644 Springboot-Exception/src/main/java/com/button/boot/exception/config/ExceptionHandle.java create mode 100644 Springboot-Exception/src/main/java/com/button/boot/exception/controller/ExceptionController.java create mode 100644 Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionHtmlModel.java create mode 100644 Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionModel.java create mode 100644 Springboot-Exception/src/main/resources/application.yml create mode 100644 Springboot-Exception/src/main/resources/logback.xml diff --git a/Springboot-Exception/README.MD b/Springboot-Exception/README.MD new file mode 100644 index 0000000..7a42adb --- /dev/null +++ b/Springboot-Exception/README.MD @@ -0,0 +1,4 @@ +# 本案例实现Springboot配置全局异常 +接口访问地址如下: +http://localhost:8080/exception/e1 +http://localhost:8080/exception/e2 \ No newline at end of file diff --git a/Springboot-Exception/pom.xml b/Springboot-Exception/pom.xml new file mode 100644 index 0000000..8c8e4d0 --- /dev/null +++ b/Springboot-Exception/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + com.button + Springboot-Exception + 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-starter-test + test + + + 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-Exception/src/main/java/com/button/boot/exception/SpringBootExceptionApplication.java b/Springboot-Exception/src/main/java/com/button/boot/exception/SpringBootExceptionApplication.java new file mode 100644 index 0000000..b6e2857 --- /dev/null +++ b/Springboot-Exception/src/main/java/com/button/boot/exception/SpringBootExceptionApplication.java @@ -0,0 +1,11 @@ +package com.button.boot.exception; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootExceptionApplication { + public static void main(String[] args) { + SpringApplication.run(SpringBootExceptionApplication.class, args); + } +} diff --git a/Springboot-Exception/src/main/java/com/button/boot/exception/config/ExceptionHandle.java b/Springboot-Exception/src/main/java/com/button/boot/exception/config/ExceptionHandle.java new file mode 100644 index 0000000..193336c --- /dev/null +++ b/Springboot-Exception/src/main/java/com/button/boot/exception/config/ExceptionHandle.java @@ -0,0 +1,61 @@ +package com.button.boot.exception.config; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.ModelAndView; + +import com.button.boot.exception.model.ExceptionHtmlModel; +import com.button.boot.exception.model.ExceptionModel; + +@RestControllerAdvice +public class ExceptionHandle { + private static final Logger LOG = LoggerFactory.getLogger(ExceptionHandle.class); + + // 捕获全局异常 + @ExceptionHandler(value = Exception.class) + Object handleException(Exception e, HttpServletRequest request) { + LOG.error("url {}, msg {}", request.getRequestURL(), e.getMessage()); + Map map = new HashMap<>(); + map.put("code", 999); + map.put("msg", e.getMessage()); + map.put("url", request.getRequestURL()); + return map; + } + + /** + * 功能描述: 处理自定义异常类 + * + * @return + * + */ + @ExceptionHandler(value = ExceptionModel.class) + Object handleCustomerException(ExceptionModel e, HttpServletRequest request) { + Map map = new HashMap<>(); + map.put("code", e.getCode()); + map.put("msg", e.getMessage()); + map.put("url", request.getRequestURL()); + return map; + } + + /** + * 功能描述: 处理自定义异常类 + * + * @return + * + */ + @ExceptionHandler(value = ExceptionHtmlModel.class) + Object handleCustomerExceptionHtml(ExceptionModel e, HttpServletRequest request) { + // 进行页面跳转 + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("error.html"); + modelAndView.addObject("msg", e.getMessage()); + return modelAndView; + } +} diff --git a/Springboot-Exception/src/main/java/com/button/boot/exception/controller/ExceptionController.java b/Springboot-Exception/src/main/java/com/button/boot/exception/controller/ExceptionController.java new file mode 100644 index 0000000..fd104d9 --- /dev/null +++ b/Springboot-Exception/src/main/java/com/button/boot/exception/controller/ExceptionController.java @@ -0,0 +1,21 @@ +package com.button.boot.exception.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.button.boot.exception.model.ExceptionModel; + +@RestController +public class ExceptionController { + + @GetMapping(value = "e1") + public Object e1() { + throw new ExceptionModel("500", "接口异常了."); + } + + @GetMapping(value = "e2") + public Object e2() { + int a = 1/0; + return a; + } +} diff --git a/Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionHtmlModel.java b/Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionHtmlModel.java new file mode 100644 index 0000000..dd07217 --- /dev/null +++ b/Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionHtmlModel.java @@ -0,0 +1,30 @@ +package com.button.boot.exception.model; + +public class ExceptionHtmlModel extends RuntimeException { + private static final long serialVersionUID = 1L; + + private String code; + private String msg; + + public ExceptionHtmlModel(String code, String msg) { + this.code = code; + this.msg = msg; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + +} diff --git a/Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionModel.java b/Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionModel.java new file mode 100644 index 0000000..a024a8a --- /dev/null +++ b/Springboot-Exception/src/main/java/com/button/boot/exception/model/ExceptionModel.java @@ -0,0 +1,30 @@ +package com.button.boot.exception.model; + +public class ExceptionModel extends RuntimeException { + private static final long serialVersionUID = 1L; + + private String code; + private String msg; + + public ExceptionModel(String code, String msg) { + this.code = code; + this.msg = msg; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + +} diff --git a/Springboot-Exception/src/main/resources/application.yml b/Springboot-Exception/src/main/resources/application.yml new file mode 100644 index 0000000..4864bea --- /dev/null +++ b/Springboot-Exception/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + servlet: + context-path: /exception + port: 8080 + uri-encoding: utf-8 + +logging: + file: logback.xml + diff --git a/Springboot-Exception/src/main/resources/logback.xml b/Springboot-Exception/src/main/resources/logback.xml new file mode 100644 index 0000000..175edf0 --- /dev/null +++ b/Springboot-Exception/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_exception.log + + + ${LOG_HOME}/springboot_exception.log.%d{yyyy-MM-dd}.%i.log + + + 100MB + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + \ No newline at end of file -- Gitee From 742feb9afba04d852a38027282b84bb723e08ffe Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Thu, 26 Sep 2019 19:38:39 +0800 Subject: [PATCH 4/7] =?UTF-8?q?SpringBoot=E4=BF=AE=E6=94=B9banner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-Banner/README.MD | 2 + Springboot-Banner/pom.xml | 53 +++++++++++++++++++ .../banner/SpringBootBannerApplication.java | 16 ++++++ .../src/main/resources/application.yml | 9 ++++ .../src/main/resources/banner.txt | 36 +++++++++++++ .../src/main/resources/logback.xml | 31 +++++++++++ 6 files changed, 147 insertions(+) create mode 100644 Springboot-Banner/README.MD create mode 100644 Springboot-Banner/pom.xml create mode 100644 Springboot-Banner/src/main/java/com/button/boot/banner/SpringBootBannerApplication.java create mode 100644 Springboot-Banner/src/main/resources/application.yml create mode 100644 Springboot-Banner/src/main/resources/banner.txt create mode 100644 Springboot-Banner/src/main/resources/logback.xml diff --git a/Springboot-Banner/README.MD b/Springboot-Banner/README.MD new file mode 100644 index 0000000..36c6c13 --- /dev/null +++ b/Springboot-Banner/README.MD @@ -0,0 +1,2 @@ +# 本案例实现Springboot设置banner +banner制作地址:http://patorjk.com/software/taag/#p=display&f=Graffiti&t=SpringBoot \ No newline at end of file diff --git a/Springboot-Banner/pom.xml b/Springboot-Banner/pom.xml new file mode 100644 index 0000000..dbbc947 --- /dev/null +++ b/Springboot-Banner/pom.xml @@ -0,0 +1,53 @@ + + 4.0.0 + com.button + Springboot-Banner + 0.0.1-SNAPSHOT + jar + Springboot设置banner + + + 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-starter-test + test + + + 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-Banner/src/main/java/com/button/boot/banner/SpringBootBannerApplication.java b/Springboot-Banner/src/main/java/com/button/boot/banner/SpringBootBannerApplication.java new file mode 100644 index 0000000..6955024 --- /dev/null +++ b/Springboot-Banner/src/main/java/com/button/boot/banner/SpringBootBannerApplication.java @@ -0,0 +1,16 @@ +package com.button.boot.banner; + +import org.springframework.boot.Banner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootBannerApplication { + public static void main(String[] args) { + SpringApplication springApplication = new SpringApplication(SpringBootBannerApplication.class); +// springApplication.setBannerMode(Banner.Mode.OFF); //关闭banner打印 + springApplication.setBannerMode(Banner.Mode.CONSOLE); //控制台打印 +// springApplication.setBannerMode(Banner.Mode.LOG); //log打印 + springApplication.run(args); + } +} diff --git a/Springboot-Banner/src/main/resources/application.yml b/Springboot-Banner/src/main/resources/application.yml new file mode 100644 index 0000000..088f9ad --- /dev/null +++ b/Springboot-Banner/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + servlet: + context-path: /banner + port: 8080 + uri-encoding: utf-8 + +logging: + file: logback.xml + diff --git a/Springboot-Banner/src/main/resources/banner.txt b/Springboot-Banner/src/main/resources/banner.txt new file mode 100644 index 0000000..6c6e069 --- /dev/null +++ b/Springboot-Banner/src/main/resources/banner.txt @@ -0,0 +1,36 @@ +//////////////////////////////////////////////////////////////////// +// _ooOoo_ // +// o8888888o // +// 88" . "88 // +// (| ^_^ |) // +// O\ = /O // +// ____/`---'\____ // +// .' \\| |// `. // +// / \\||| : |||// \ // +// / _||||| -:- |||||- \ // +// | | \\\ - /// | | // +// | \_| ''\---/'' | | // +// \ .-\__ `-` ___/-. / // +// ___`. .' /--.--\ `. . ___ // +// ."" '< `.___\_<|>_/___.' >'"". // +// | | : `- \`.;`\ _ /`;.`/ - ` : | | // +// \ \ `-. \_ __\ /__ _/ .-` / / // +// ========`-.____`-.___\_____/___.-`____.-'======== // +// `=---=' // +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // +// 佛祖保佑 永不宕机 永无BUG // +//////////////////////////////////////////////////////////////////// + + ______ __ _______ __ + / \ / | / \ / | +/$$$$$$ | ______ ______ $$/ _______ ______ $$$$$$$ | ______ ______ _$$ |_ +$$ \__$$/ / \ / \ / |/ \ / \ $$ |__$$ | / \ / \ / $$ | +$$ \ /$$$$$$ |/$$$$$$ |$$ |$$$$$$$ |/$$$$$$ |$$ $$< /$$$$$$ |/$$$$$$ |$$$$$$/ + $$$$$$ |$$ | $$ |$$ | $$/ $$ |$$ | $$ |$$ | $$ |$$$$$$$ |$$ | $$ |$$ | $$ | $$ | __ +/ \__$$ |$$ |__$$ |$$ | $$ |$$ | $$ |$$ \__$$ |$$ |__$$ |$$ \__$$ |$$ \__$$ | $$ |/ | +$$ $$/ $$ $$/ $$ | $$ |$$ | $$ |$$ $$ |$$ $$/ $$ $$/ $$ $$/ $$ $$/ + $$$$$$/ $$$$$$$/ $$/ $$/ $$/ $$/ $$$$$$$ |$$$$$$$/ $$$$$$/ $$$$$$/ $$$$/ + $$ | / \__$$ | + $$ | $$ $$/ + $$/ $$$$$$/ + diff --git a/Springboot-Banner/src/main/resources/logback.xml b/Springboot-Banner/src/main/resources/logback.xml new file mode 100644 index 0000000..1b95d4e --- /dev/null +++ b/Springboot-Banner/src/main/resources/logback.xml @@ -0,0 +1,31 @@ + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + ${LOG_HOME}/springboot_banner.log + + + ${LOG_HOME}/springboot_banner.log.%d{yyyy-MM-dd}.%i.log + + + + 100MB + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + \ No newline at end of file -- Gitee From 2260c005d73358496e2378746007d83d5912af1a Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Thu, 26 Sep 2019 19:39:19 +0800 Subject: [PATCH 5/7] =?UTF-8?q?SpringBoot=E6=95=B4=E5=90=88httpclient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-Httpclient/README.MD | 1 + Springboot-Httpclient/pom.xml | 58 ++++++ .../SpringBootHttpclientApplication.java | 11 + .../config/HttpclientConfigure.java | 76 +++++++ .../service/HttpClientToolService.java | 13 ++ .../impl/HttpClientToolServiceImpl.java | 193 ++++++++++++++++++ .../src/main/resources/application.yml | 16 ++ .../src/main/resources/logback.xml | 30 +++ .../boot/httpclient/test/HttpClientTest.java | 25 +++ 9 files changed, 423 insertions(+) create mode 100644 Springboot-Httpclient/README.MD create mode 100644 Springboot-Httpclient/pom.xml create mode 100644 Springboot-Httpclient/src/main/java/com/button/boot/httpclient/SpringBootHttpclientApplication.java create mode 100644 Springboot-Httpclient/src/main/java/com/button/boot/httpclient/config/HttpclientConfigure.java create mode 100644 Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/HttpClientToolService.java create mode 100644 Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/impl/HttpClientToolServiceImpl.java create mode 100644 Springboot-Httpclient/src/main/resources/application.yml create mode 100644 Springboot-Httpclient/src/main/resources/logback.xml create mode 100644 Springboot-Httpclient/src/test/java/com/button/boot/httpclient/test/HttpClientTest.java diff --git a/Springboot-Httpclient/README.MD b/Springboot-Httpclient/README.MD new file mode 100644 index 0000000..2edefcf --- /dev/null +++ b/Springboot-Httpclient/README.MD @@ -0,0 +1 @@ +# 本案例实现SpringBoot整合Httpclient \ No newline at end of file diff --git a/Springboot-Httpclient/pom.xml b/Springboot-Httpclient/pom.xml new file mode 100644 index 0000000..b521c9f --- /dev/null +++ b/Springboot-Httpclient/pom.xml @@ -0,0 +1,58 @@ + + 4.0.0 + com.button + Springboot-Httpclient + 0.0.1-SNAPSHOT + jar + + SpringBoot整合Httpclient + + + 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.apache.httpcomponents + httpclient + + + + org.springframework.boot + spring-boot-starter-test + test + + + 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-Httpclient/src/main/java/com/button/boot/httpclient/SpringBootHttpclientApplication.java b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/SpringBootHttpclientApplication.java new file mode 100644 index 0000000..52c1e62 --- /dev/null +++ b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/SpringBootHttpclientApplication.java @@ -0,0 +1,11 @@ +package com.button.boot.httpclient; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootHttpclientApplication { + public static void main(String[] args) { + SpringApplication.run(SpringBootHttpclientApplication.class, args); + } +} diff --git a/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/config/HttpclientConfigure.java b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/config/HttpclientConfigure.java new file mode 100644 index 0000000..73b89f4 --- /dev/null +++ b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/config/HttpclientConfigure.java @@ -0,0 +1,76 @@ +package com.button.boot.httpclient.config; + +import org.apache.http.client.config.RequestConfig; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class HttpclientConfigure { + @Value("${http.maxTotal}") + private Integer httpMaxTotal; + @Value("${http.defaultMaxPerRoute}") + private Integer httpDefaultMaxPerRoute; + @Value("${http.connectTimeout}") + private Integer httpConnectTimeout; + @Value("${http.connectionRequestTimeout}") + private Integer httpConnectionRequestTimeout; + @Value("${http.socketTimeout}") + private Integer httpSocketTimeout; + @Value("${http.staleConnectionCheckEnabled}") + private Boolean httpStaleConnectionCheckEnabled; + + @Bean(name = "httpClientConnectionManager") + public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() { + PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); + // 最大连接数 + poolingHttpClientConnectionManager.setMaxTotal(httpMaxTotal); + // 每个主机的最大并发数 + poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpDefaultMaxPerRoute); + return poolingHttpClientConnectionManager; + } + + /** + * 实例化连接池,设置连接池管理器。 + * @param httpClientConnectionManager + * @return + */ + @Bean(name = "httpClientBuilder") + public HttpClientBuilder getHttpClientBuilder(@Qualifier("httpClientConnectionManager")PoolingHttpClientConnectionManager httpClientConnectionManager){ + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); + httpClientBuilder.setConnectionManager(httpClientConnectionManager); + return httpClientBuilder; + } + + /** + * 注入连接池,用于获取httpClient + * @param httpClientBuilder + * @return + */ + @Bean + public CloseableHttpClient getCloseableHttpClient(@Qualifier("httpClientBuilder") HttpClientBuilder httpClientBuilder){ + return httpClientBuilder.build(); + } + + @SuppressWarnings("deprecation") + @Bean(name = "builder") + public RequestConfig.Builder getBuilder(){ + RequestConfig.Builder builder = RequestConfig.custom(); + return builder.setConnectTimeout(httpConnectTimeout) + .setConnectionRequestTimeout(httpConnectionRequestTimeout) + .setSocketTimeout(httpSocketTimeout) + .setStaleConnectionCheckEnabled(httpStaleConnectionCheckEnabled); + } + + /** + * 使用builder构建一个RequestConfig对象 + */ + @Bean + public RequestConfig getRequestConfig(@Qualifier("builder") RequestConfig.Builder builder){ + return builder.build(); + } +} diff --git a/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/HttpClientToolService.java b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/HttpClientToolService.java new file mode 100644 index 0000000..bb9f54e --- /dev/null +++ b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/HttpClientToolService.java @@ -0,0 +1,13 @@ +package com.button.boot.httpclient.service; + +import java.util.Map; + +public interface HttpClientToolService { + String doGet(String url); + String doGet(String url, Map map); + String doPostForm(String url); + String doPostForm(String url, Map map); + String doPostForm(String url, Map map, Map headers); + String doPostBody(String url, String body); + String doPostBody(String url, String body, Map headers); +} diff --git a/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/impl/HttpClientToolServiceImpl.java b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/impl/HttpClientToolServiceImpl.java new file mode 100644 index 0000000..831f4f8 --- /dev/null +++ b/Springboot-Httpclient/src/main/java/com/button/boot/httpclient/service/impl/HttpClientToolServiceImpl.java @@ -0,0 +1,193 @@ +package com.button.boot.httpclient.service.impl; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.http.NameValuePair; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.button.boot.httpclient.service.HttpClientToolService; + +@Service +public class HttpClientToolServiceImpl implements HttpClientToolService { + private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientToolServiceImpl.class); + private static String UTF_8 = "UTF-8"; + + @Autowired + private CloseableHttpClient httpClient; + @Autowired + private RequestConfig config; + + /** + * 不带参数的get请求,如果状态码为200,则返回response,否则返回null + * + * @param url + * @return + * @throws Exception + */ + @Override + public String doGet(String url) { + CloseableHttpResponse response = null; + try { + // 声明 http get 请求 + HttpGet httpGet = new HttpGet(url); + // 装载配置信息 + httpGet.setConfig(config); + // 发起请求 + response = this.httpClient.execute(httpGet); + // 判断状态码是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + // 返回响应体的内容 + return EntityUtils.toString(response.getEntity(), UTF_8); + } + LOGGER.error("http执行doGet请求返回空.url={},response={}", url, response); + return null; + } catch (Exception ex) { + LOGGER.error("http执行doGet请求时异常. ex={}", ex); + return null; + } finally { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + LOGGER.error("关闭doGet请求response时出现异常. e={}", e); + } + } + } + } + + /** + * 带参数的get请求,如果状态码为200,则返回response,否则返回null + * + * @param url + * @return + * @throws Exception + */ + @Override + public String doGet(String url, Map map) { + try { + URIBuilder uriBuilder = new URIBuilder(url); + if (map != null) { + // 遍历map,拼接请求参数 + for (Map.Entry entry : map.entrySet()) { + uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); + } + } + // 调用不带参数的get请求 + return this.doGet(uriBuilder.build().toString()); + } catch (Exception e) { + LOGGER.error("执行doGet请求时发生异常. e={}", e); + return ""; + } + } + + @Override + public String doPostForm(String url) { + return this.doPostForm(url, null); + } + + @Override + public String doPostForm(String url, Map map) { + return doPostForm(url, map, null); + } + + @Override + public String doPostForm(String url, Map map, Map headers) { + CloseableHttpResponse response = null; + try { + // 声明httpPost请求 + HttpPost httpPost = new HttpPost(url); + // 加入配置信息 + httpPost.setConfig(config); + if (headers != null) { + for (Entry entry : headers.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + } + // 判断map是否为空,不为空则进行遍历,封装from表单对象 + if (map != null) { + List list = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()) { + list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); + } + // 构造from表单对象 + UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, UTF_8); + // 把表单放到post里 + httpPost.setEntity(urlEncodedFormEntity); + } + // 发起请求 + response = this.httpClient.execute(httpPost); + if (response.getStatusLine().getStatusCode() == 200) { + return EntityUtils.toString(response.getEntity(), UTF_8); + } + LOGGER.error("http执行doPostForm请求返回空.url={},response={}", url, response); + return null; + } catch (Exception ex) { + LOGGER.error("http执行doPostForm请求时发生异常. ex={}", ex); + return null; + } finally { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + LOGGER.error("关闭doPostForm中的response时出现异常. e={}", e); + } + } + } + } + + @Override + public String doPostBody(String url, String body) { + return doPostBody(url, body, null); + } + + @Override + public String doPostBody(String url, String body, Map headers) { + CloseableHttpResponse response = null; + try { + HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(config); + // 判断map是否为空,不为空则进行遍历,封装from表单对象 + httpPost.addHeader("content-type", "application/json"); + if (headers != null) { + for (Entry entry : headers.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + } + httpPost.setEntity(new StringEntity(body, "UTF-8")); + response = this.httpClient.execute(httpPost); + if (response.getStatusLine().getStatusCode() == 200) { + return EntityUtils.toString(response.getEntity(), UTF_8); + } + LOGGER.error("http执行doPostBody请求返回结果为空. url={}, response={}", url, response); + return null; + } catch (Exception ex) { + LOGGER.error("http执行doPostBody请求时出现异常. ex={}", ex); + return null; + } finally { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + LOGGER.error("关闭doPostBody中的response时出现异常. e={}", e); + } + } + } + } +} diff --git a/Springboot-Httpclient/src/main/resources/application.yml b/Springboot-Httpclient/src/main/resources/application.yml new file mode 100644 index 0000000..a1cb588 --- /dev/null +++ b/Springboot-Httpclient/src/main/resources/application.yml @@ -0,0 +1,16 @@ +server: + servlet: + context-path: /httpclient + port: 8080 + uri-encoding: utf-8 + +logging: + file: logback.xml + +http: + defaultMaxPerRoute: 100 + maxTotal: 300 + connectTimeout: 1000 + connectionRequestTimeout: 500 + socketTimeout: 10000 + staleConnectionCheckEnabled: true \ No newline at end of file diff --git a/Springboot-Httpclient/src/main/resources/logback.xml b/Springboot-Httpclient/src/main/resources/logback.xml new file mode 100644 index 0000000..c433ac9 --- /dev/null +++ b/Springboot-Httpclient/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_httpclient.log + + + ${LOG_HOME}/springboot_httpclient.log.%d{yyyy-MM-dd}.%i.log + + + 100MB + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + \ No newline at end of file diff --git a/Springboot-Httpclient/src/test/java/com/button/boot/httpclient/test/HttpClientTest.java b/Springboot-Httpclient/src/test/java/com/button/boot/httpclient/test/HttpClientTest.java new file mode 100644 index 0000000..ddd0f52 --- /dev/null +++ b/Springboot-Httpclient/src/test/java/com/button/boot/httpclient/test/HttpClientTest.java @@ -0,0 +1,25 @@ +package com.button.boot.httpclient.test; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.button.boot.httpclient.service.HttpClientToolService; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class HttpClientTest { + private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientTest.class); + + @Autowired + private HttpClientToolService httpClientToolService; + @Test + public void test1() { + String pageContent = httpClientToolService.doGet("https://www.baidu.com"); + LOGGER.info("doget返回结果: pageContent={}", pageContent); + } +} -- Gitee From 32de27c7d565c63a1e10438662b0d3d7f9d7eafb Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Thu, 26 Sep 2019 19:41:29 +0800 Subject: [PATCH 6/7] =?UTF-8?q?SpringBoot=E6=95=B4=E5=90=88druid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-Druid/README.MD | 3 + Springboot-Druid/pom.xml | 81 +++++++++++++++++++ Springboot-Druid/springboot_druid.sql | 16 ++++ .../druid/SpringBootDruidApplication.java | 13 +++ .../druid/config/DruidMonitorConfigure.java | 45 +++++++++++ .../boot/druid/controller/UserController.java | 22 +++++ .../com/button/boot/druid/dao/UserDao.java | 9 +++ .../button/boot/druid/model/UserModel.java | 34 ++++++++ .../boot/druid/service/UserService.java | 9 +++ .../druid/service/impl/UserServiceImpl.java | 23 ++++++ .../src/main/resources/application.yml | 43 ++++++++++ .../src/main/resources/logback.xml | 31 +++++++ .../src/main/resources/mapper/User.xml | 10 +++ 13 files changed, 339 insertions(+) create mode 100644 Springboot-Druid/README.MD create mode 100644 Springboot-Druid/pom.xml create mode 100644 Springboot-Druid/springboot_druid.sql create mode 100644 Springboot-Druid/src/main/java/com/button/boot/druid/SpringBootDruidApplication.java create mode 100644 Springboot-Druid/src/main/java/com/button/boot/druid/config/DruidMonitorConfigure.java create mode 100644 Springboot-Druid/src/main/java/com/button/boot/druid/controller/UserController.java create mode 100644 Springboot-Druid/src/main/java/com/button/boot/druid/dao/UserDao.java create mode 100644 Springboot-Druid/src/main/java/com/button/boot/druid/model/UserModel.java create mode 100644 Springboot-Druid/src/main/java/com/button/boot/druid/service/UserService.java create mode 100644 Springboot-Druid/src/main/java/com/button/boot/druid/service/impl/UserServiceImpl.java create mode 100644 Springboot-Druid/src/main/resources/application.yml create mode 100644 Springboot-Druid/src/main/resources/logback.xml create mode 100644 Springboot-Druid/src/main/resources/mapper/User.xml diff --git a/Springboot-Druid/README.MD b/Springboot-Druid/README.MD new file mode 100644 index 0000000..d2babf1 --- /dev/null +++ b/Springboot-Druid/README.MD @@ -0,0 +1,3 @@ +# 本案例实现SpringBoot集成阿里巴巴Druid监控 +DruidMonitor访问地址:http://localhost:8080/app/druid +接口访问地址:http://localhost:8080/app/getUserList \ No newline at end of file diff --git a/Springboot-Druid/pom.xml b/Springboot-Druid/pom.xml new file mode 100644 index 0000000..ff32c74 --- /dev/null +++ b/Springboot-Druid/pom.xml @@ -0,0 +1,81 @@ + + 4.0.0 + com.button + Springboot-Druid + 0.0.1-SNAPSHOT + jar + SpringBoot集成阿里巴巴Druid监控 + + + 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.projectlombok + lombok + true + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.1.1 + + + + mysql + mysql-connector-java + + + + com.alibaba + druid + 1.1.10 + + + log4j + log4j + 1.2.17 + + + + org.springframework.boot + spring-boot-starter-test + test + + + 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-Druid/springboot_druid.sql b/Springboot-Druid/springboot_druid.sql new file mode 100644 index 0000000..177a0be --- /dev/null +++ b/Springboot-Druid/springboot_druid.sql @@ -0,0 +1,16 @@ +CREATE SCHEMA `springboot_druid` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ; + + +CREATE TABLE `springboot_druid`.`tb_user` ( + `id` INT(11) NOT NULL AUTO_INCREMENT, + `name` VARCHAR(10) NULL DEFAULT NULL, + `age` INT(3) NULL DEFAULT NULL, + `address` VARCHAR(50) NULL DEFAULT NULL, + PRIMARY KEY (`id`)) +ENGINE = InnoDB +DEFAULT CHARACTER SET = utf8 +COLLATE = utf8_bin; + +INSERT INTO `springboot_druid`.`tb_user` (`name`, `age`, `address`) VALUES ('张三', '25', '陕西'); +INSERT INTO `springboot_druid`.`tb_user` ( `name`, `age`, `address`) VALUES ('李四', '26', '浙江'); +INSERT INTO `springboot_druid`.`tb_user` (`name`, `age`, `address`) VALUES ('王五', '28', '江苏'); diff --git a/Springboot-Druid/src/main/java/com/button/boot/druid/SpringBootDruidApplication.java b/Springboot-Druid/src/main/java/com/button/boot/druid/SpringBootDruidApplication.java new file mode 100644 index 0000000..a486233 --- /dev/null +++ b/Springboot-Druid/src/main/java/com/button/boot/druid/SpringBootDruidApplication.java @@ -0,0 +1,13 @@ +package com.button.boot.druid; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@MapperScan("com.button.boot.druid.dao") +public class SpringBootDruidApplication { + public static void main(String[] args) { + SpringApplication.run(SpringBootDruidApplication.class, args); + } +} diff --git a/Springboot-Druid/src/main/java/com/button/boot/druid/config/DruidMonitorConfigure.java b/Springboot-Druid/src/main/java/com/button/boot/druid/config/DruidMonitorConfigure.java new file mode 100644 index 0000000..9f3aecb --- /dev/null +++ b/Springboot-Druid/src/main/java/com/button/boot/druid/config/DruidMonitorConfigure.java @@ -0,0 +1,45 @@ +package com.button.boot.druid.config; + +import javax.sql.DataSource; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.support.http.StatViewServlet; +import com.alibaba.druid.support.http.WebStatFilter; + +@Configuration +public class DruidMonitorConfigure { + @SuppressWarnings({"rawtypes", "unchecked"}) + @Bean + public ServletRegistrationBean druidServlet() { // 主要实现WEB监控的配置处理 + ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // 进行druid监控的配置处理操作 + servletRegistrationBean.addInitParameter("allow", "127.0.0.1,192.168.1.159"); // 白名单 + servletRegistrationBean.addInitParameter("deny", "192.168.1.200"); // 黑名单 + servletRegistrationBean.addInitParameter("loginUsername", "admin"); // 用户名 + servletRegistrationBean.addInitParameter("loginPassword", "111111"); // 密码 + servletRegistrationBean.addInitParameter("resetEnable", "false"); // 是否可以重置数据源 + return servletRegistrationBean; + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + @Bean + public FilterRegistrationBean filterRegistrationBean() { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); + filterRegistrationBean.setFilter(new WebStatFilter()); + + filterRegistrationBean.addUrlPatterns("/*"); // 所有请求进行监控处理 + filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*"); + return filterRegistrationBean; + } + + @Bean + @ConfigurationProperties(prefix = "spring.datasource") + public DataSource druidDataSource() { + return new DruidDataSource(); + } +} diff --git a/Springboot-Druid/src/main/java/com/button/boot/druid/controller/UserController.java b/Springboot-Druid/src/main/java/com/button/boot/druid/controller/UserController.java new file mode 100644 index 0000000..30cecc8 --- /dev/null +++ b/Springboot-Druid/src/main/java/com/button/boot/druid/controller/UserController.java @@ -0,0 +1,22 @@ +package com.button.boot.druid.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.button.boot.druid.model.UserModel; +import com.button.boot.druid.service.UserService; + +@RestController +public class UserController { + + @Autowired + private UserService UserService; + + @GetMapping("/getUserList") + public List testdb() { + return UserService.getUserList(); + } +} diff --git a/Springboot-Druid/src/main/java/com/button/boot/druid/dao/UserDao.java b/Springboot-Druid/src/main/java/com/button/boot/druid/dao/UserDao.java new file mode 100644 index 0000000..c02d2b1 --- /dev/null +++ b/Springboot-Druid/src/main/java/com/button/boot/druid/dao/UserDao.java @@ -0,0 +1,9 @@ +package com.button.boot.druid.dao; + +import java.util.List; + +import com.button.boot.druid.model.UserModel; + +public interface UserDao { + List getUserList(); +} diff --git a/Springboot-Druid/src/main/java/com/button/boot/druid/model/UserModel.java b/Springboot-Druid/src/main/java/com/button/boot/druid/model/UserModel.java new file mode 100644 index 0000000..ecbfef9 --- /dev/null +++ b/Springboot-Druid/src/main/java/com/button/boot/druid/model/UserModel.java @@ -0,0 +1,34 @@ +package com.button.boot.druid.model; + +import java.io.Serializable; + +public class UserModel implements Serializable{ + private static final long serialVersionUID = 1L; + + private String name; + private Integer age; + private String address; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public Integer getAge() { + return age; + } + public void setAge(Integer age) { + this.age = age; + } + public String getAddress() { + return address; + } + public void setAddress(String address) { + this.address = address; + } + @Override + public String toString() { + return "UserModel [name=" + name + ", age=" + age + ", address=" + address + "]"; + } +} diff --git a/Springboot-Druid/src/main/java/com/button/boot/druid/service/UserService.java b/Springboot-Druid/src/main/java/com/button/boot/druid/service/UserService.java new file mode 100644 index 0000000..1eb8f59 --- /dev/null +++ b/Springboot-Druid/src/main/java/com/button/boot/druid/service/UserService.java @@ -0,0 +1,9 @@ +package com.button.boot.druid.service; + +import java.util.List; + +import com.button.boot.druid.model.UserModel; + +public interface UserService { + List getUserList(); +} diff --git a/Springboot-Druid/src/main/java/com/button/boot/druid/service/impl/UserServiceImpl.java b/Springboot-Druid/src/main/java/com/button/boot/druid/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..8b55e4b --- /dev/null +++ b/Springboot-Druid/src/main/java/com/button/boot/druid/service/impl/UserServiceImpl.java @@ -0,0 +1,23 @@ +package com.button.boot.druid.service.impl; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.button.boot.druid.dao.UserDao; +import com.button.boot.druid.model.UserModel; +import com.button.boot.druid.service.UserService; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private UserDao userDao; + + @Override + public List getUserList() { + return userDao.getUserList(); + } + +} diff --git a/Springboot-Druid/src/main/resources/application.yml b/Springboot-Druid/src/main/resources/application.yml new file mode 100644 index 0000000..b229a45 --- /dev/null +++ b/Springboot-Druid/src/main/resources/application.yml @@ -0,0 +1,43 @@ +server: + servlet: + context-path: /app + port: 8080 + uri-encoding: utf-8 + +logging: + file: logback.xml + +##validate 加载hibernate时,验证创建数据库表结构 +##create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 +##create-drop 加载hibernate时创建,退出是删除表结构 +##update 加载hibernate自动更新数据库结构 +##validate 启动时验证表的结构,不会创建表 +##none 启动时不做任何操作 +spring: + jpa: + hibernate: + ddl-auto: create + show-sql: true ##控制台打印sql + datasource: ##数据库配置 + url: jdbc:mysql://localhost:3306/springboot_druid?useUnicode=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&characterEncoding=UTF-8 + username: root + password: 123456 + driver-class-name: com.mysql.jdbc.Driver + type: com.alibaba.druid.pool.DruidDataSource #使用druid的话 需要多配置一个属性spring.datasource.type + initialSize: 5 + minIdle: 5 + maxActive: 20 + maxWait: 60000 # 配置获取连接等待超时的时间 + timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 + minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫秒 + validationQuery: SELECT 1 FROM DUAL + testWhileIdle: true + testOnBorrow: false + testOnReturn: false + poolPreparedStatements: true + maxPoolPreparedStatementPerConnectionSize: 20 # 打开PSCache,并且指定每个连接上PSCache的大小 + filters: stat,wall,log4j # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 +#mybatis的mapper配置文件 +mybatis: + #config-location: classpath:mybatis-config.xml # mybatis配置文件所在路径 + mapper-locations: classpath:mapper/*.xml # 所有的mapper映射文件 diff --git a/Springboot-Druid/src/main/resources/logback.xml b/Springboot-Druid/src/main/resources/logback.xml new file mode 100644 index 0000000..63dff9a --- /dev/null +++ b/Springboot-Druid/src/main/resources/logback.xml @@ -0,0 +1,31 @@ + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + ${LOG_HOME}/springboot_druid.log + + + ${LOG_HOME}/springboot_druid.log.%d{yyyy-MM-dd}.%i.log + + + + 100MB + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + \ No newline at end of file diff --git a/Springboot-Druid/src/main/resources/mapper/User.xml b/Springboot-Druid/src/main/resources/mapper/User.xml new file mode 100644 index 0000000..9eb5cba --- /dev/null +++ b/Springboot-Druid/src/main/resources/mapper/User.xml @@ -0,0 +1,10 @@ + + + + + -- Gitee From e015054e193adddfec3d1b0b20f84e0186342733 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Thu, 26 Sep 2019 19:44:17 +0800 Subject: [PATCH 7/7] =?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 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e57b27c..cdc58dc 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,8 @@ SpringBoot应用集合 - [SpringBoot整合Listener](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Listener) - [SpringBoot对多线程的支持](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-MultiThread) - [SpringBoot整合Swagger](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Swagger) -- [SpringBoot整合Dubbo](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/SpringBoot-Dubbo) \ No newline at end of file +- [SpringBoot整合Dubbo](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/SpringBoot-Dubbo) +- [Springboot设置banner](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Banner) +- [Springboot配置全局异常](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Exception) +- [SpringBoot集成阿里巴巴Druid监控](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Druid) +- [SpringBoot整合Httpclient](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-Httpclient) \ No newline at end of file -- Gitee