关于Spring Cloud:
Spring cloud并不是一个项目,它是一套项目的组合,Spring cloud中存在很多的子项目,每一个子项目都是针对微服务系统架构存在的问题给出的一种解决方案,Springcloud是基于Springboot实现的,Springcloud并没有重复的造轮子,它只是将目前各家公司开发的比较成熟,经得起实际考验的服务组合起来
使用Spring cloud搭建微服务系统(订单微服务+商品微服务):
1.商品微服务的搭建:
创建一个maven项目,项目名称item:
在pom.xml文件中添加依赖:定义版本变量,添加父级依赖,添加web支持
< properties>
< java.version> 1.8</ java.version>
</ properties>
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 1.5.6.RELEASE</ version>
</ parent>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
</ dependencies>
创建实体类Item,添加get Set方法;无参构造;带参构造;toString方法:
商品微服务实体类
public class Item {
private Integer id;
private String title;
private String pic;
private String desc;
private Integer price;
get Set方法;无参构造;带参构造;toString方法
}
编写service层,添加@service注解,初始化数据,编写查询方法:
@Service
public class ItemService {
private static final Map< Integer, Item> map = new HashMap < Integer, Item> ( ) ;
static {
map. put ( 1 , new Item ( 1 , "商品标题1" , "http://图片1" , "商品描述1" , 100 ) ) ;
map. put ( 2 , new Item ( 1 , "商品标题2" , "http://图片2" , "商品描述2" , 200 ) ) ;
map. put ( 3 , new Item ( 1 , "商品标题3" , "http://图片3" , "商品描述3" , 300 ) ) ;
map. put ( 4 , new Item ( 1 , "商品标题4" , "http://图片4" , "商品描述4" , 400 ) ) ;
map. put ( 5 , new Item ( 1 , "商品标题5" , "http://图片5" , "商品描述5" , 500 ) ) ;
}
public Item queryItemById ( Integer id) {
return map. get ( id) ;
}
}
编写controller层:
@RestController
public class ItemController {
@Autowired
private ItemService itemservice;
@RequestMapping ( value= "/item/{id}" , method