【黑马程序员】spring cloud初学者--简单的web
项目
spring cloud是这几年新兴的微服务框架,虽然现在的微服务框架很多,但是spring cloud还是被认为最好的微服务框架,尽管我也不知道为什么这么说,但是接下来我准备一点一点的探究,今天就是开始的第一步,大家可以一点一点的通过一个小小的例子,来看看为什么他被称为最好的微服务框架!
Web项目
具体的虽然我不是很明白,但是我们先通过一个简单的例子来看看spring cloud 是什么样子的吧。
创建maven项目
pom文件
[AppleScript] 纯文本查看复制代码
?
0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1
xmlns:xsi="https://www.sodocs.net/doc/4d1726823.html,/2001/XMLSchema-instance" xsi:schemaLocation="https://www.sodocs.net/doc/4d1726823.html,/POM/4.0.0 [url]https://www.sodocs.net/doc/4d1726823.html,/xsd/maven-4.0.0.xsd[/url]"> 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5se[/url]
4
5
5
5
6
5
7
5
8
5
9
添加必要的jar包,一切和spring有关的包都不需要版本号,springcloud会给你选择他最稳定的版本。而spring-boot-maven-plugin更是非常的强大,他提供了很多方便的功能。
它收集类路径上的所有jar,并构建一个可运行的“über-jar”,这样可以更方便地执行和传输您的服务。
他会定位public static void main()方法来标记为可运行类。
他会为你默认选择jar包的版本号。
创建一个get请求
创建一个get请求,并且有一个可选的参数name,请求的结果中会返回一个json数据,就像这个样子
[AppleScript] 纯文本查看复制代码
?
1 2 3 4{
"id": 1,
"content": "Hello, World!" }
创建pojo类
id是唯一标识符,content是返回的内容,首先创建一个简单的类,有id和content两个字段,有一个构造函数
src/main/java/hello/Greeting.java
[AppleScript] 纯文本查看复制代码
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20package hello;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
springboot自动使用jackson,他会吧Greeting转换成json格式的字符串。创建controller
在spring中构建restful的web服务,http请求由controller处理,而springcloud可以轻松的通过@RestController来识别,下面的GreetingController通过返回Greeting类的新实例来处理GET请求/ greeting [AppleScript] 纯文本查看复制代码
?
1
package hello;
0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.form at(template, name));
}
}
这个控制器看似非常的简单,但是我们慢慢来变化他。
@RequestMapping来确保访问的方法正确的映射到greeting方法,但是细心的你发现为什么没有post,get请求,其实@RequestMapping默认允许所有的http请求,我们只需要@RequestMapping(method=GET)来制定需要的请求。
@@RequestParam绑定了参数的name,这样我们在参数中输入name的参数的时候就可以制定该方法的name,此查询字符串参数被明确标记为可选(默认情况下为required = true):如果请求中不存在,则使用“World”的defaultValue。而返回值很简单,id就是一个计数器的下一个值,并且返回格式化的content,
传统的mvc和springcloud的不同就是相应主体的区别,我们不管学习springmvc,还是struts,都有一个view这么个概念,他会是主体,然后然会给html,但是在这里,
这个RESTful Web服务简单的填充以后就可以返回给html的json对象
代码用到了spring4.*的@RestController的注解,这个注解其实就是
@Controller和@ResponseBody 的结合体.并且spring会自动的利用MappingJackson2HttpMessageConverter来转化Greeting实体到json
运行你的程序
非常简单的方法创建一个独立的应用程序。并且我们默认发布到springboot支持的tomcat上边。
[AppleScript] 纯文本查看复制代码
?
01 02 03 04 05 06 07 08 09 10 11package hello;
import org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
12}
}
@SpringBootApplication这个注释其实也是又好几个注释组合而成的。
1. @Configuration将该类标记为应用程序上下文的bean定义的源
2. @EnableAutoConfiguration指示Spring Boot根据类路径设置,其他bean 和各种属性设置开始添加bean
3. @ComponentScan告诉Spring在hello包中查找其他组件,配置和服务,让它找到控制器
而且你也注意到了,我们再也不需要所谓的web.xml文件来保证项目的启动了SpringApplication.run方法能让我们的项目完美的启动,我们也不会去考虑404这些情况了,所谓的配置错误,滚蛋吧。运行main方法,我们就能看到我们的项目正确执行了,接下来方位
[AppleScript] 纯文本查看复制代码
?
1h ttp://localhost:8080/greeting
结果如下:
[AppleScript] 纯文本查看复制代码
?
1{"id":1,"content":"Hello, World!"}
如果是get的请求加参数
[AppleScript] 纯文本查看复制代码
?
1h ttp://localhost:8080/greeting?name=User
结果是
[AppleScript] 纯文本查看复制代码
?
1{"id":2,"content":"Hello, User!"}
小结
如果你发现id从1变成了2,说明我们的项目一直是很好的运行的。通过这一章的认识,我们了解了@RestController,@RequestMapping,
@RequestParam,
@SpringBootApplication,并且知道了如何运用springcloud启动一个项目,
而不用配置web.xml文