三、Spring Boot微服务构建

来源:互联网 发布:centos nfs samba 编辑:程序博客网 时间:2024/06/05 19:04

一、
1、通过官方的Spring Initializr工具来产生基础项目。访问http://s七art. spring. io/, 如下图所示, 该页面提供了以Maven 或
Gradle构建Spring Boot项目的功能
2、选择构建工具Maven Project、 Spring Boot版本选择1. 3. 7, 填写Group和
沁tifact信息, 在Search for dependencies中可以搜索需要的其他依赖包,
这里我们要实现RESTfulAPI, 所以可以添加Web依赖。。
http://start.spring.io/
这里写图片描述
3、解压项目包, 并用IDE以Maven项目导入。
从菜单中选择F旦e–>New–>Project from Exis巨ng Sources …。
二、工程结构

这里写图片描述
• src/main/java: 主程序入口 HelloApp巨ca巨on, 可以通过直接运行该类来
启动Spring Boot应用。

• src/main/resources: 配置目录, 该目录用来存放应用的
一 些配置信息,
比如
应用名、服务端口、数据库链接等。由千我们引入了Web模块,因此产生了static
目录与templates目录, 前者用于存放静态资源, 如图片、 css 、JavaScript等;
后者用千存放Web页面的模板文件, 这里我们主要演示提供RESTful APL所以这
两个目录并不会用到。
• src/tes七/: 单元测试目录, 生成的HelloApplica巨onTests通过JUnit 4实
现, 可以直接用运行Spring Boot应用的测试

Maven配置分析

这里写图片描述
这里所引用的web和test 模块,在SpringBoot 生态中被称为Starter POMs。Starter POMs
是 一 系列轻便的依赖 包

开发者在使用和整
合模块时, 不必再去搜寻样例代码中的依赖配置来复制使用, 只需要引入对应的模块包即
可 。 比如, 开发Web应用的时候, 就引入spring-boot-s 七 arter-web, 希望应用具备
访问数据库能力的时候, 那就再引入 spring-boot-starter-jdbc 或是更好用的
spring-boot-starter-data-jpa。 在使用SpringBoot构建应用的时候, 各项功能模
块的整合不再像传统Spring应用的开发方式那样,需要在 pom.xml中做大量的依赖配置

二、实现RESTful API
这里写图片描述
启动该应用,通过浏览器访问http:/ /localhost: 8080/hello, 我们可以看到
返回了预期结果: Hello World。
1、启动方式:
这里写图片描述
2、启动报错:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at com.example.demo.DemoApplication.main(DemoApplication.java:11) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:189) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
… 8 common frames omitted
这里写图片描述
3、解决方式

报的是 无法启动嵌入容器,工厂类不存在。
在类头加上 @EnableAutoConfiguration。就OK了。这个接口看名字是允许自动配置。

这里写图片描述

拓展:

一、@EnableAutoConfiguration
The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you
will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC,
the auto-configuration will assume that you are developing a web application and setup Spring accordingly.

Starters and Auto-Configuration

Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied.
You are free to pick-and-choose jar dependencies outside of the starters and Spring Boot will still do
its best to auto-configure your application.
这个注释告诉SpringBoot“猜”你将如何想配置Spring,基于你已经添加jar依赖项。如果spring-boot-starter-web已经添加Tomcat和Spring MVC,这个注释自动将假设您正在开发一个web应用程序并添加相应的spring设置。
  自动配置被设计用来和“Starters”一起更好的工作,但这两个概念并不直接相关。您可以自由挑选starter依赖项以外的jar包,springboot仍将尽力自动配置您的应用程序。
  spring通常建议我们将main方法所在的类放到一个root包下,@EnableAutoConfiguration(开启自动配置)注解通常都放到main所在类的上面,下面是一个典型的结构布局:
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
这样@EnableAutoConfiguration可以从逐层的往下搜索各个加注解的类,例如,你正在编写一个JPA程序(如果你的pom里进行了配置的话),spring会自动去搜索加了@Entity注解的类,并进行调用
二、@SpringbootApplication
  使用@SpringbootApplication注解 可以解决根类或者配置类(我自己的说法,就是main所在类)头上注解过多的问题,一个@SpringbootApplication相当于@Configuration,@EnableAutoConfiguration和 @ComponentScan 并具有他们的默认属性值

package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //等同于 @Configuration @EnableAutoConfiguration @ComponentScanpublic
class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}