首页 文章 精选 留言 我的

精选列表

搜索[springboot],共4364篇文章
优秀的个人博客,低调大师

SpringBoot入门系列: Spring Boot的测试

Spring Boot的测试,和普通项目的测试类同,可以继续使用我们熟悉的测试工具。当然,这里的测试,主要还是后台代码的测试。 主要需要注意的地方仅有三点: 1、依赖包的引入:pom.xml中仅依赖spring-boot-starter-test,它把相关的依赖全部引入。 2、在测试类上的注解,常用的注解有三个 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration 这三个注解,只要注意第二个注解括号内的Application.class就行,把它替换为项目的启动类即可。 我们前面spring-boot-sample-mysql工程的启动类为SpringBootSampleMysqlApplication,那么测试类上的注解就是 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringBootSampleMysqlApplication.class) @WebAppConfiguration 3、测试类的文件结构,保持src/test/java和src/main/java结构一直,即:包+文件夹。 如:com.example包service中类的测试,那么在src/test/java也是建立com.example包,再在包下建立文件夹service. 注:由于我们测试的启动类用的是项目的启动类,所以Spring Boot项目的测试配置文件任然用src/main/resources的。 下面贴两个个测试类代码 1、服务类的 [java]view plaincopy packagecom.example.service; importjava.util.List; importorg.junit.Assert; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.test.SpringApplicationConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; importorg.springframework.test.context.web.WebAppConfiguration; importcom.example.Application; importcom.example.domain.TestPOJO; importcom.example.dto.HotelDto; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=Application.class) @WebAppConfiguration publicclassTestServicesTest{ @Autowired TestServicestestServices; @Test publicvoidtestShow(){ StringexpectedResult="helloworld!"; Stringresult=testServices.show(); Assert.assertTrue("数据一致",expectedResult.equals(result)); Assert.assertFalse("数据不一致",!expectedResult.equals(result)); } @Test publicvoidtestShowDao(){ List<TestPOJO>testPOJOList=testServices.showDao(10); Assert.assertTrue("数据集不对",testPOJOList.size()==1); Assert.assertTrue("数据一致",testPOJOList.get(0).getName().equals("nice")); } @Test publicvoidtestFindByCountry(){ List<HotelDto>testPOJOList=testServices.findByCountry("US"); Assert.assertTrue("数据集不对",testPOJOList.size()==1); Assert.assertTrue("数据一致",testPOJOList.get(0).getCityName().equals("SanFrancisco")); } } 2、controller类的 [java]view plaincopy packagecom.example.controller; importjava.util.List; importorg.junit.Assert; importorg.junit.Before; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.test.SpringApplicationConfiguration; importorg.springframework.http.MediaType; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; importorg.springframework.test.context.web.WebAppConfiguration; importorg.springframework.test.web.servlet.MockMvc; importorg.springframework.test.web.servlet.MvcResult; importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders; importorg.springframework.test.web.servlet.setup.MockMvcBuilders; importorg.springframework.web.context.WebApplicationContext; importcom.example.Application; importcom.example.domain.TestPOJO; importcom.example.dto.HotelDto; importcom.example.service.TestServices; importcom.fasterxml.jackson.core.JsonProcessingException; importcom.fasterxml.jackson.databind.ObjectMapper; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=Application.class) @WebAppConfiguration publicclassTestControllerTest{ MockMvcmvc; @Autowired WebApplicationContextwebApplicationConnect; @Autowired TestServicestestServices; StringexpectedJson; @Before publicvoidsetUp()throwsJsonProcessingException{ mvc=MockMvcBuilders.webAppContextSetup(webApplicationConnect).build(); } @Test publicvoidtestShow()throwsException{ StringexpectedResult="helloworld!"; Stringuri="/show"; MvcResultmvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)) .andReturn(); intstatus=mvcResult.getResponse().getStatus(); Stringcontent=mvcResult.getResponse().getContentAsString(); Assert.assertTrue("错误,正确的返回值为200",status==200); Assert.assertFalse("错误,正确的返回值为200",status!=200); Assert.assertTrue("数据一致",expectedResult.equals(content)); Assert.assertFalse("数据不一致",!expectedResult.equals(content)); } protectedStringObj2Json(Objectobj)throwsJsonProcessingException{ ObjectMappermapper=newObjectMapper(); returnmapper.writeValueAsString(obj); } @Test publicvoidtestShowDaoInt()throwsException{ List<TestPOJO>testPOJOList=testServices.showDao(10); expectedJson=Obj2Json(testPOJOList); Stringuri="/showDao?age=10"; MvcResultmvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn(); intstatus=mvcResult.getResponse().getStatus(); Stringcontent=mvcResult.getResponse().getContentAsString(); Assert.assertTrue("错误,正确的返回值为200",status==200); Assert.assertFalse("错误,正确的返回值为200",status!=200); Assert.assertTrue("数据一致",expectedJson.equals(content)); Assert.assertFalse("数据不一致",!expectedJson.equals(content)); } @Test publicvoidtestShowDaoString()throwsException{ List<HotelDto>hotelDtoList=testServices.findByCountry("US"); expectedJson=Obj2Json(hotelDtoList); Stringuri="/country/US"; MvcResultmvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn(); intstatus=mvcResult.getResponse().getStatus(); Stringcontent=mvcResult.getResponse().getContentAsString(); Assert.assertTrue("错误,正确的返回值为200",status==200); Assert.assertFalse("错误,正确的返回值为200",status!=200); Assert.assertTrue("数据一致",expectedJson.equals(content)); Assert.assertFalse("数据不一致",!expectedJson.equals(content)); } } controller类的,为了MockMvc,注入了WebApplicationContext。 原文地址http://www.bieryun.com/833.html

优秀的个人博客,低调大师

SpringBoot+Redis实现Session数据共享

1.需求在传统的Web小规模并发的情况下,单个Tomcat容器往往就能胜任请求的负载与处理,如图1所示的方案一。方案一的好处在于,部署简单,维护方便,在Web服务发生变更之后,能够快速升级;除此之外,在用户Session管理方面也十分简单:将所有Session存储至本地服务器即可。但是该方案存在并发规模小、单点故障等原因。 图1 单节点Tomcat示意图 针对方案一存在的负载小、单点故障等因素,方案二进行了大规模的改进。首先,使用NGINX+keepalived取代原来的单节点Tomcat,同时以NGINX作为反向代理服务器,管理多个Tomcat服务节点。如图2所示。 图2 多节点Tomcat部署示意图 从图2可以看出,单节点部署的用户请求负载,从单个Tomcat服务器转移到了Nginx负载(还有其他负载均衡方式)下的多个Tomcat节点,从而实现了单个Web服务的负载均衡与备份。而各个Tomcat节点与NGINX之间,则是采用反向代理的形式进行通信,具体方法有以下五种:**1、轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 upstream backserver { server 192.168.0.14; server 192.168.0.15; } ** **2、指定权重指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 upstream backserver { server 192.168.0.14 weight=10; server 192.168.0.15 weight=10; } ** **3、IP绑定 ip_hash每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 upstream backserver { ip_hash; server 192.168.0.14:88; server 192.168.0.15:80; } ****4、fair(第三方)按后端服务器的响应时间来分配请求,响应时间短的优先分配。 upstream backserver { server server1; server server2; fair; } ** **5、url_hash(第三方)按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 upstream backserver { server squid1:3128; server squid2:3128; hash $request_uri; hash_method crc32; } ** 具体NGINX+Keepalived+Tomcat的负载均衡搭建请参考我的另一篇博客。2.带来的问题

优秀的个人博客,低调大师

SpringBoot2.4新特性」jar自动瘦身

自动分析瘦身 Spring Boot 项目最终构建处理 JAR 包大小一直是个诟病,需要把所有依赖包内置最终输出可运行的 jar。 当然可以使用其他的插件扩展 实现依赖 JAR 和 可运行 jar 分离可以参考 slot-maven-plugin, 但此种方法治标不治本并不能减少原有依赖的 JAR 的大小。 Spring Boot 2.4 提供对构建输出 JAR 分析自动瘦身的功能,自动在构建输出可运行 JAR 时删除 empty starter dependencies 效果展示 先来分别基于 Spring Boot 2.4.0 和 Spring Boot 2.3.6 来构建一个可运行的 jar ,再来聊什么是 empty starter 使用 start.spring.io 创建一个空的 Spring Boot 项目,注意不需要引入任何依赖 mvn clean install 构建出来相关可运行 jar 分别解压两个 jar 到两个不同的目录 tar -zxvf demo-2.3.6.jar -C demo-2.3.6/ tar -zxvf demo-2.4.0.jar -C demo-2.4.0/ 统计依赖 jar 个数, 2.3.6 共计 19 个 依赖 jar 而 2.4.0 只有 18 个依赖 jar ,缺少了 spring-boot-starter.jar cd demo-2.3.6/BOOT-INF/lib && ll -h | wc -l 19 cd demo-2.4.0/BOOT-INF/lib && ll -h | wc -l 18 什么是 empty starter 如上文所述,我们在基于 start.spring.io 创建项目的时候 已经默认引入了, 但在 Spring Boot 2.4 中会自动删除此类 empty starter dependencies jar <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> 我们来看一下 spring-boot-stater 有什么特殊性? ① 空 jar 不包含任何代码 ② 有引用其他 jar,只为批量导入其他 jar 所以此类型 jar 在构建成可运行 jar 时并未实际意义,因为批量导入的依赖 jar 都可以被引入。目前 spring boot 提供的 redis、amqp等大部分 starter 均是此类 jar,所以在构建后会自动删除。 自定义 jar 实现自动瘦身 创建 MANIFEST.MF jar 包元信息,添加一行 Spring-Boot-Jar-Type: dependencies-starter 即可 resources ├── META-INF └── MANIFEST.MF 项目推荐: Spring Cloud 、Spring Security OAuth2的RBAC权限管理系统 欢迎关注

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册