接下来,我们按照传统的做法搭建商品和订单服务,首先创建web项目常见的三个包,分别为model
、service
、controller
:
然后在
model
里面创建三个类,分别为Goods
、Order
、OrderDetails
其中
Goods
代码如下
package com.felix.model;
/**
* Created by weistekweistek on 2019/1/3.
*/
public class Goods {
private Long goodsid;
private String title;
private String pic;
private String desc;
private Float price;
public Goods() {
}
public Goods(Long goodsid, String title, String pic, String desc, Float price) {
this.goodsid = goodsid;
this.title = title;
this.pic = pic;
this.desc = desc;
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"goodsid=" + goodsid +
", title='" + title + '\'' +
", pic='" + pic + '\'' +
", desc='" + desc + '\'' +
", price=" + price +
'}';
}
public Long getGoodsid() {
return goodsid;
}
public void setGoodsid(Long goodsid) {
this.goodsid = goodsid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
订单详情代码(包含商品信息):
package com.felix.model;
/**
* Created by weistekweistek on 2019/1/3.
*/
public class OrderDetails {
private Long orderDetailsID;
private Long orderID;
private Goods goods;
private Integer goodsCount;
public Long getOrderDetailsID() {
return orderDetailsID;
}
public void setOrderDetailsID(Long orderDetailsID) {
this.orderDetailsID = orderDetailsID;
}
public Long getOrderID() {
return orderID;
}
public void setOrderID(Long orderID) {
this.orderID = orderID;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public Integer getGoodsCount() {
return goodsCount;
}
public void setGoodsCount(Integer goodsCount) {
this.goodsCount = goodsCount;
}
public OrderDetails(Long orderDetailsID, Long orderID, Goods goods, Integer goodsCount) {
this.orderDetailsID = orderDetailsID;
this.orderID = orderID;
this.goods = goods;
this.goodsCount = goodsCount;
}
@Override
public String toString() {
return "OrderDetails{" +
"orderDetailsID=" + orderDetailsID +
", orderID=" + orderID +
", goods=" + goods +
", goodsCount=" + goodsCount +
'}';
}
public OrderDetails() {
}
}
订单代码:
package com.felix.model;
import java.util.Date;
import java.util.List;
/**
* Created by weistekweistek on 2019/1/3.
*/
public class Order {
private Long orderid;
private Long userid;
private Date createDate;
private Date updateDate;
private List orderDetailsList;
public Order(Long orderid, Long userid, Date createDate, Date updateDate, List orderDetailsList) {
this.orderid = orderid;
this.userid = userid;
this.createDate = createDate;
this.updateDate = updateDate;
this.orderDetailsList = orderDetailsList;
}
@Override
public String toString() {
return "Order{" +
"orderid=" + orderid +
", userid=" + userid +
", createDate=" + createDate +
", updateDate=" + updateDate +
", orderDetailsList=" + orderDetailsList +
'}';
}
public Long getOrderid() {
return orderid;
}
public void setOrderid(Long orderid) {
this.orderid = orderid;
}
public Long getUserid() {
return userid;
}
public void setUserid(Long userid) {
this.userid = userid;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public List getOrderDetailsList() {
return orderDetailsList;
}
public void setOrderDetailsList(List orderDetailsList) {
this.orderDetailsList = orderDetailsList;
}
public Order() {
}
}
上面内容比较简单,不做详细解释,然后,我们针对上面的Goods
、Order
创建对应的服务GoodsService
、OrderService
其中GoodsService
中创建一个使用商品id查询商品的函数(商品数据为伪造数据)
package com.felix.service;
import com.felix.model.Goods;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* Created by weistekweistek on 2019/1/3.
*/
@Service
public class GoodsService {
public static final Map goods_map = new HashMap();
static {
goods_map.put(1L,new Goods(1L,"商品01","http://127.0.0.1/01.png","商品01描述",10.0F));
goods_map.put(2L,new Goods(2L,"商品02","http://127.0.0.1/02.png","商品02描述",10.0F));
goods_map.put(3L,new Goods(3L,"商品03","http://127.0.0.1/03.png","商品03描述",10.0F));
goods_map.put(4L,new Goods(4L,"商品04","http://127.0.0.1/04.png","商品04描述",10.0F));
goods_map.put(5L,new Goods(5L,"商品05","http://127.0.0.1/05.png","商品05描述",10.0F));
goods_map.put(6L,new Goods(6L,"商品06","http://127.0.0.1/06.png","商品06描述",10.0F));
goods_map.put(7L,new Goods(7L,"商品07","http://127.0.0.1/07.png","商品07描述",10.0F));
goods_map.put(8L,new Goods(8L,"商品08","http://127.0.0.1/08.png","商品08描述",10.0F));
goods_map.put(9L,new Goods(9L,"商品09","http://127.0.0.1/09.png","商品09描述",10.0F));
goods_map.put(10L,new Goods(10L,"商品10","http://127.0.0.1/10.png","商品10描述",10.0F));
}
public Goods findGoodsById(Long id){
return goods_map.get(id);
}
}
OrderService
中创建一个使用订单id查询订单的函数(订单数据为伪造数据)
package com.felix.service;
import com.felix.model.Order;
import com.felix.model.OrderDetails;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* Created by weistekweistek on 2019/1/3.
*/
@Service
public class OrderService {
private static final Map order_map = new HashMap();
static {
List orderDetailsList = new ArrayList();
for (Long i = 1L;i <= 10L;i++){
orderDetailsList.add(new OrderDetails(i ,i,GoodsService.goods_map.get(i),5));
order_map.put(i,new Order(i,i,new Date(),new Date(),new ArrayList(orderDetailsList)));
System.out.println(order_map);
}
}
public Order findOrderById(Long id){
return order_map.get(id);
}
}
然后,在controller
里分别创建GoodsController
、ServiceController
,同时创建接口返回对应的商品/订单信息
GoodsController
中代码如下
package com.felix.controller;
import com.felix.model.Goods;
import com.felix.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by weistekweistek on 2019/1/3.
*/
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@RequestMapping(value = "/{id}")
public Goods findGoodsById(@PathVariable("id") Long id){
return goodsService.findGoodsById(id);
}
}
OrderController
中代码如下
package com.felix.controller;
import com.felix.model.Order;
import com.felix.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by weistekweistek on 2019/1/3.
*/
@RestController
@RequestMapping(value = "/order")
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping(value = "/{id}")
public Order findOrderById(@PathVariable("id") Long id){
return orderService.findOrderById(id);
}
}
传统的服务器均采用上述形式提供服务,上面的代码提供了两个接口http://127.0.0.1:8080/goods/{id}
、http://127.0.0.1:8080/order/{id}
,其中id可以自行修改,SpringBoot
默认采用了8080端口,我们暂时不做改变,现在启动服务器,等到服务器启动后访问http://127.0.0.1:8080/goods/1
查看效果
可以正常提供商品服务,并返回商品信息的JSON数据(Chrome浏览器安装了Json Formatter插件)
然后访问订单接口查看订单信息
http://127.0.0.1:8080/order/3
我们请求了订单id为3的订单数据,从返回的JSON数据可以看到,订单信息里的订单详情列表正常返回。
以上为传统服务搭建。那么如果现在随着我们业务量的增大,单台服务器已经不能满足我们的业务需求,需要把
商品
和订单
独立到两个不同的服务器,该怎么样进行操作呢?下节,我们将针对现有的服务进行改造,在返回数据不改变的前提下实现商品
和订单
服务的分离。
分布式系统开发---商品服务分离SpringCloud(三)
以上内容转载请注明出处,同时也请大家不吝你的关注和下面的赞赏