本文共 7437 字,大约阅读时间需要 24 分钟。
1.使用 Spring Boot 实现微服务
业务非常简单:
图示:
说明:1.1.实现商品微服务
1.1.1.创建空工程
1.1.2.导入依赖
org.springframework.boot spring-boot-starter-web org.projectlombok lombok
1.1.3.在service-product创建实体Item
package com.ls.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class Item { private Long id; private String title; private String pic; private String desc; private Long price;}
1.1.4.编写ItemService
package com.ls.service.impl;
import com.ls.entity.Item;
import com.ls.service.ItemService; import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;@Service
public class ItemServiceImpl implements ItemService { private static final Map<Long, Item> ITEM_MAP = new HashMap<Long, Item>();static {// 准备一些静态数据,模拟数据库 ITEM_MAP.put(1L, new Item(1L, "商品1", "http://图片1", "商品描述1", 1000L)); ITEM_MAP.put(2L, new Item(2L, "商品2", "http://图片2", "商品描述2", 2000L)); ITEM_MAP.put(3L, new Item(3L, "商品3", "http://图片3", "商品描述3", 3000L)); ITEM_MAP.put(4L, new Item(4L, "商品4", "http://图片4", "商品描述4", 4000L)); ITEM_MAP.put(5L, new Item(5L, "商品5", "http://图片5", "商品描述5", 5000L)); ITEM_MAP.put(6L, new Item(6L, "商品6", "http://图片6", "商品描述6", 6000L)); ITEM_MAP.put(7L, new Item(7L, "商品7", "http://图片7", "商品描述7", 7000L)); ITEM_MAP.put(8L, new Item(8L, "商品8", "http://图片8", "商品描述8", 8000L)); ITEM_MAP.put(8L, new Item(9L, "商品9", "http://图片9", "商品描述9", 9000L)); ITEM_MAP.put(8L, new Item(10L, "商品10", "http://图片10", "商品描述10", 10000L));}/** * 模拟实现商品查询 * * @param id * @return */@Overridepublic Item queryItemById(long id) { return ITEM_MAP.get(id); }}
1.1.5.编写ItemController
package com.ls.controller;import com.ls.entity.Item;import com.ls.service.ItemService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ItemController { @Value("${server.port}") private String port; @Autowired private ItemService itemService; @GetMapping(value = "item/{id}") public Item queryItemById(@PathVariable("id")long id){ System.out.println("service port:"+port); return this.itemService.queryItemById(id); }}
1.1.6.创建application.yml配置文件
###服务端口号server: port: 8092
1.1.7.启动程序测试
可以看到已经通过微服务查询到数据。1.2.实现订单微服务
1.2.1.在service-order导入依赖
org.springframework.boot spring-boot-starter-web
1.2.2.创建订单Order实体
package com.ls.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.util.Date;import java.util.List;@Data@AllArgsConstructor@NoArgsConstructorpublic class Order { private String orderId; private Long userId; private Date createDate; private Date updateDate; private ListorderDetails;}
1.2.3.创建订单详情OrderDetail实体
package com.ls.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class OrderDetail { private String OrderId; private Item item = new Item();}
1.2.4.将商品微服务项目中的Item类拷贝到当前工程
1.2.5.编写OrderServicepackage com.ls.service;import com.ls.entity.Item;import com.ls.entity.Order;import com.ls.entity.OrderDetail;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.*;@Servicepublic class OrderService { private static final MapORDER_DATA = new HashMap (); static { // 模拟数据库,构造测试数据 Order order = new Order(); order.setOrderId("123"); order.setCreateDate(new Date()); order.setUpdateDate(order.getCreateDate()); order.setUserId(1L); List orderDetails = new ArrayList (); Item item = new Item();// 此处并没有商品的数据,只是保存了商品ID,需要调用商品微服务获取 item.setId(1L); orderDetails.add(new OrderDetail(order.getOrderId(), item)); item = new Item(); // 构造第二个商品数据 item.setId(2L); orderDetails.add(new OrderDetail(order.getOrderId(), item)); order.setOrderDetails(orderDetails); ORDER_DATA.put(order.getOrderId(), order); } @Autowired private ItemService itemService; /** * 根据订单id查询订单数据 * * @param orderId * @return */ public Order queryOrderById(String orderId) { Order order = ORDER_DATA.get(orderId); if (order == null) { return null; } List orderDetailList = order.getOrderDetails(); for (OrderDetail orderDetail : orderDetailList) { // 通过商品微服务查询商品详细数据 Item item = this.itemService.queryItemById(orderDetail.getItem().getId()); if (item != null) { continue; } orderDetail.setItem(item); } return order; }}
1.2.6.实现ItemService
@Servicepublic class ItemService { // Spring框架对RESTful方式的http请求做了封装,来简化操作 @Autowired private RestTemplate restTemplate; public Item queryItemById(Long id) { return this.restTemplate.getForObject("http://127.0.0.1:8081/item/" + id, Item.class); } }
1.2.7.编写OrderController
package com.ls.controller;import com.ls.entity.Order;import com.ls.service.OrderService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class OrderConterllor { @Autowired private OrderService orderService; @GetMapping(value = "order/{orderId}") private Order queryOrderById(@PathVariable("orderId") String orderId){ return this.orderService.queryOrderById(orderId); }}
1.2.8.编写程序入口
package com.ls;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication//@ComponentScan({"com.ls.controller", "com.ls.service","com.ls.properties"})//手动指定bean扫描范围public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } /** * 向Spring容器中定义RestTemplate对象 * @return */ @Bean public RestTemplate restTemplate(){ return new RestTemplate(); }}
1.2.9.编写application.yml配置文件
###服务端口号server: port: 8081
1.2.10.启动测试
测试结果可见,查询订单时,同时也将商品数据查询到。转载地址:http://wqmwb.baihongyu.com/