一起玩转微服务(11)——一切从简单开始
介绍
使用Spring Bboot是快乐并且简单的,不需要繁琐的配置就能够完成一套非常强大的应用。
spring boot 2.3.1
Spring Boot 2.3.1 发布于:2020/06/12,现在已经提交到 Spring 仓库和 Maven 中央仓库了。
这个版本包括 127 个 bug 修复、Spring Boot 文档改进增强、依赖升级等,另外还新增了一些新特性:
•提供基于新的 Maven 坐标 com.oracle.database 对 Oracle JDBC driver 的依赖管理;
•优化 Spring Cloud 的 CachedRandomPropertySource 不能正确适配的问题;•限制使用定制的 YAML 类型;
•增强对 NoSuchMethodErrors 异常失败分析,能显示基类从哪被加载的;•提供更佳的错误消息,如果 Docker 停止运行了;
•优化 SystemEnvironmentPropertyMapper 类;
•提供更佳的诊断信息,当构建 OCI 镜像失败时 Docker 响应的 500 错误;
•支持通过 alwaysUseFullPath=true 参数来配置 UrlPathHelper;•支持在 Elasticsearch URIs 中使用用户信息;
•支持在 Spring WebFlux 框架中使用欢迎页面;
这个小版本还增加了蛮多东西的,大家也没有必要跟着版本走,可以根据需要进行升级。疫情也挡不住外国友人更新的热情。
实现
使用STS,可以去官方网站下载最新版。网站地址 https://Spring.io/tools/sts/ Spring Tool Suite™是基于eclipse开发的专门为Spring开发使用的工具包。
新建工程
选择Spring Starter Project,
输入工程名 对应的Name 打包方式 对应的Packaging,可以选择jar或者war的方式。
输入组织名 对应的Group 输入描述 对应的Description
输入包名 对应的Package 点击next,然后选择web和mysql
这里的版本用的是2.3.1 如果没有本地maven库或者私库会下载很长时间。
添加默认请求
进入 Chapter0301Application 添加
@RestController @SpringBootApplication public class Chapter0301Application { @RequestMapping("/") String home() { return "欢迎使用Spring Boot!"; } public static void main(String[] args) { SpringApplication.run(Chapter0301Application.class, args); } }
使用@RestController 相当于@Controller 和 @RequestBody。是Sspring Bboot 基于Sspring MVC的基础上进行了改进, 将@Controller 与@ResponseBody 进行了合并形成的一个新的注解。 @EnableAutoConfiguration 作用 从classpath中搜索所有META-INF/spring.factories配置文件然后,将其中org.springframework.boot.autoconfigure.EnableAutoConfiguration key对应的配置项加载到spring容器 只有spring.boot.enableautoconfiguration为true(默认为true)的时候,才启用自动配置 @EnableAutoConfiguration还可以进行排除,排除方式有2种,一是根据class来排除(exclude),二是根据class name(excludeName)来排除 其内部实现的关键点有
1.ImportSelector 该接口的方法的返回值都会被纳入到spring容器管理中
2.SpringFactoriesLoader 该类可以从classpath中搜索所有META-INF/spring.factories配置文件,并读取配置
启动spring boot
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.1.RELEASE) 2020-06-23 13:30:11.611 INFO 9916 --- [ main] com.cloud.sky.Chapter0301Application : Starting Chapter0301Application on DADI-PC with PID 9916 (D:\java\microservice\chapter0301\target\classes started by Administrator in D:\java\microservice\chapter0301) 2020-06-23 13:30:11.614 INFO 9916 --- [ main] com.cloud.sky.Chapter0301Application : No active profile set, falling back to default profiles: default 2020-06-23 13:30:12.415 INFO 9916 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-06-23 13:30:12.423 INFO 9916 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-06-23 13:30:12.424 INFO 9916 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36] 2020-06-23 13:30:12.512 INFO 9916 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-06-23 13:30:12.512 INFO 9916 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 830 ms 2020-06-23 13:30:12.665 INFO 9916 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-06-23 13:30:12.809 INFO 9916 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-06-23 13:30:12.818 INFO 9916 --- [ main] com.cloud.sky.Chapter0301Application : Started Chapter0301Application in 1.492 seconds (JVM running for 3.109) 2020-06-23 13:30:20.675 INFO 9916 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-06-23 13:30:20.676 INFO 9916 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2020-06-23 13:30:20.680 INFO 9916 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
打开浏览器访问 http://localhost:8080/ 可以得到如下页面
遇到问题
构建的过程中遇到问题
[INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while processing the POMs: [FATAL] Non-parseable POM D:\java\apache-maven-3.1.1\repo\org\jetbrains\kotlin\kotlin-bom\1.3.72\kotlin-bom-1.3.72.pom: entity reference names can not start with character ')' (position: START_TAG seen ...ost,s="";function qs(n){var u=D.URL;var t=u.match(eval(\'/(\\?|#|&)... @1:243) @ D:\java\apache-maven-3.1.1\repo\org\jetbrains\kotlin\kotlin-bom\1.3.72\kotlin-bom-1.3.72.pom, line 1, column 243 @ [ERROR] The build could not read 1 project -> [Help 1] [ERROR] [ERROR] The project com.cloudskyme:chapter0301:0.0.1 (D:\java\microservice\chapter0301\pom.xml) has 1 error [ERROR] Non-parseable POM D:\java\apache-maven-3.1.1\repo\org\jetbrains\kotlin\kotlin-bom\1.3.72\kotlin-bom-1.3.72.pom: entity reference names can not start with character ')' (position: START_TAG seen ...ost,s="";function qs(n){var u=D.URL;var t=u.match(eval(\'/(\\?|#|&)... @1:243) @ D:\java\apache-maven-3.1.1\repo\org\jetbrains\kotlin\kotlin-bom\1.3.72\kotlin-bom-1.3.72.pom, line 1, column 243 -> [Help 2] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException
1. 解决
修改maven默认源配置
我使用的是阿里的maven仓库,国外的东西没个代理还真麻烦。
<repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> <repository> <id>sonatype-nexus-snapshots</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
然后执行 mvn help:system
成功可以看到如下界面:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2013-06-23 vmware安装ubuntu12.04嵌套安装xen server(实现嵌套虚拟化)