微服务,现在是一个越来越热的东西,软件架构发展到现在,越来越向轻量级方向演变,微服务的架构,慢慢被很多的中小企业认可。
springboot这个项目,用来构建基于java的微服务应用,它并没有发明什么新的东西,而是让现有的开发配置更好用,更简洁。以前开发spring的项目,至少需要一个applicationContext.xml文件来做配置,但是使用springboot可以做到零配置,减少了很多的配置文件。用springboot的内嵌tomcat可以轻松发布一个web接口服务。
我们来看一个简单的例子,新建一个maven工程,pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hua.spring.boot</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
/** * */ package com.hua.spring.boot.springboot; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } @RequestMapping("/w") @ResponseBody String springBoot() { return "welcome to spring boot"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
启动main程序
写道
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
2016-12-29 09:31:46.383 INFO 7988 --- [ main] c.h.s.boot.springboot.SampleController : Starting SampleController on dell2-PC with PID 7988 (D:\work-space\springboot\target\classes started by dell2 in D:\work-space\springboot)
2016-12-29 09:31:46.386 INFO 7988 --- [ main] c.h.s.boot.springboot.SampleController : No active profile set, falling back to default profiles: default
2016-12-29 09:31:46.446 INFO 7988 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6e4784bc: startup date [Thu Dec 29 09:31:46 CST 2016]; root of context hierarchy
2016-12-29 09:31:49.773 INFO 7988 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-12-29 09:31:49.799 INFO 7988 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-12-29 09:31:49.801 INFO 7988 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6
2016-12-29 09:31:50.175 INFO 7988 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-12-29 09:31:50.176 INFO 7988 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3736 ms
2016-12-29 09:31:50.418 INFO 7988 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-12-29 09:31:50.423 INFO 7988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-12-29 09:31:50.424 INFO 7988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-12-29 09:31:50.424 INFO 7988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-12-29 09:31:50.424 INFO 7988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-12-29 09:31:50.891 INFO 7988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6e4784bc: startup date [Thu Dec 29 09:31:46 CST 2016]; root of context hierarchy
2016-12-29 09:31:51.012 INFO 7988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.hua.spring.boot.springboot.SampleController.home()
2016-12-29 09:31:51.014 INFO 7988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/w]}" onto java.lang.String com.hua.spring.boot.springboot.SampleController.springBoot()
2016-12-29 09:31:51.025 INFO 7988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-12-29 09:31:51.025 INFO 7988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-12-29 09:31:51.073 INFO 7988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-29 09:31:51.074 INFO 7988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-29 09:31:51.154 INFO 7988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-29 09:31:51.407 INFO 7988 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-12-29 09:31:51.513 INFO 7988 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-12-29 09:31:51.524 INFO 7988 --- [ main] c.h.s.boot.springboot.SampleController : Started SampleController in 5.802 seconds (JVM running for 6.349)
2016-12-29 09:32:13.640 INFO 7988 --- [nio-8080-exec-1] o.apache.tomcat.util.http.parser.Cookie : A cookie header was received [1476092382,1478080035; HISTORY="\.\.\.\.\.\.com.mor.server.dubbo.service.DemoServer..../dubboadmin/governance/services/com.mor.server.dubbo.service.DemoServer/providers\.\.\.\.\.\.com.mor.server.dubbo.service.DemoServer..../dubboadmin/governance/applications/hello-world-app/services/com.mor.server.dubbo.service.DemoServer/providers"] that contained an invalid cookie. That cookie will be ignored.Note: further occurrences of this error will be logged at DEBUG level.
2016-12-29 09:32:13.649 INFO 7988 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-12-29 09:32:13.649 INFO 7988 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-12-29 09:32:13.685 INFO 7988 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 36 ms
启动成功后,通过浏览器访问http://localhost:8080/w ,会出现welcome to spring boot的字符,是不是很简单?通过这一点,我们 可以开发resultful的接口,返回json类型。
开发一个服务是简单了,轻量极了,任何事物都有两面性,我们在传统的nginx/tomcat模式下面,各种调优参数都可以配置,但是在这里就没那么好操作了,有一部分参数可以通过java代码来设置,但是需要开发人员知道他的API。
一个对外的服务,一般都是做集群的,为了防止单点故障,那么我们通常可以把springboot的单个服务做成docker镜像,通过启动多个docker镜像的形式来实现服务的集群,当然,docker镜像的部署可以通过jenkins来动态管理。。
相关推荐
狂神SpringBoot笔记+源码 狂神SpringBoot笔记+源码 狂神SpringBoot笔记+源码 狂神SpringBoot笔记+源码 狂神SpringBoot笔记+源码 狂神SpringBoot笔记+源码 狂神SpringBoot笔记+源码 狂神SpringBoot笔记+源码 狂神...
springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合...
spring-boot-中文参考手册 SpringBoot中文文档 springboot 中文 文档 SpringBoot 帮助您创建可以独立运行的、基于 Spring 的生产级应用程序。我们对 Spring 平台和第三方库有自己的看法,所以您可以从最简单的开始。...
Springboot搭建的公司官网门户系统源码 Springboot搭建的公司官网门户系统源码 Springboot搭建的公司官网门户系统源码 Springboot搭建的公司官网门户系统源码 Springboot搭建的公司官网门户系统源码 Springboot...
项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...
廖师兄2小时学会springboot 第1章 SpringBoot介绍 1-1 SpringBoot介绍和课程安排 1-1 SpringBoot介绍 第2章 第一个SpringBoot应用 快速构建第一个SpringBoot应用 2-1 第一个SpringBoot应用 第3章 项目属性...
springboot
SpringBoot快递管理系统源码,包含数据库 SpringBoot快递管理系统源码,包含数据库 SpringBoot快递管理系统源码,包含数据库 SpringBoot快递管理系统源码,包含数据库 SpringBoot快递管理系统源码,包含数据库 ...
期末大作业基于springboot的个人博客项目源码期末大作业基于springboot的个人博客项目源码期末大作业基于springboot的个人博客项目源码期末大作业基于springboot的个人博客项目源码期末大作业基于springboot的个人...
Springboot项目报文加密(采用AES、RSA动态加密策略) Springboot项目报文加密(采用AES、RSA动态加密策略) Springboot项目报文加密(采用AES、RSA动态加密策略) Springboot项目报文加密(采用AES、RSA动态加密...
springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel ...
java期末大作业基于springboot+vue+Mysql仓库管理系统源代码java期末大作业基于springboot+vue+Mysql仓库管理系统源代码java期末大作业基于springboot+vue+Mysql仓库管理系统源代码java期末大作业基于springboot+vue...
基于SpringBoot的学生成绩管理系统,源码和数据库脚本。老师指导通过的项目 基于SpringBoot的学生成绩管理系统,源码和数据库脚本。老师指导通过的项目基于SpringBoot的学生成绩管理系统,源码和数据库脚本。老师...
Java毕业设计:基于SpringBoot的医院挂号系统 Java毕业设计:基于SpringBoot的医院挂号系统 Java毕业设计:基于SpringBoot的医院挂号系统 Java毕业设计:基于SpringBoot的医院挂号系统 Java毕业设计:基于...
基于SpringBoot的个人博客系统设计与实现-论文.docx基于SpringBoot的个人博客系统设计与实现-论文.docx基于SpringBoot的个人博客系统设计与实现-论文.docx基于SpringBoot的个人博客系统设计与实现-论文.docx基于...
基于SpringBoot的个人博客系统设计与实现-论文.pdf基于SpringBoot的个人博客系统设计与实现-论文.pdf基于SpringBoot的个人博客系统设计与实现-论文.pdf基于SpringBoot的个人博客系统设计与实现-论文.pdf基于...
基于SpringBoot+MySQL+Vue的在线考试系统(源码+论文).zip 基于SpringBoot+MySQL+Vue的在线考试系统(源码+论文).zip 基于SpringBoot+MySQL+Vue的在线考试系统(源码+论文).zip 基于SpringBoot+MySQL+Vue的在线...
前言:打包好的springboot项目,可以使用java -jar xxx.jar的方式启动。当出现多个springboot项目需要启动的时候,可以使用脚本启动的方式。这在springcloud项目的开发测试阶段尤为有用。以下展示启动脚本示例,过程...
基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...
SpringBoot(七)SpringBoot整合Druid实现数据库密码加密 SpringBoot(七)SpringBoot整合Druid实现数据库密码加密 SpringBoot(七)SpringBoot整合Druid实现数据库密码加密