業(yè)務(wù):
1.商品微服務(wù):通過商品id查詢商品的服務(wù)筋蓖。
2.訂單微服務(wù):創(chuàng)建訂單時,通過調(diào)用商品的微服務(wù)進行查詢商品數(shù)據(jù)稿蹲。
說明:
1.對于商品微服務(wù)而言扭勉,商品微服務(wù)是服務(wù)的提供者,訂單微服務(wù)是服務(wù)的消費者苛聘。
2.對于訂單微服務(wù)而言涂炎,訂單微服務(wù)是服務(wù)的提供者忠聚,人是服務(wù)的消費者。
3.1唱捣、實現(xiàn)商品微服務(wù)
3.1.1两蟀、創(chuàng)建工程
3.1.2、導(dǎo)入依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.1.3震缭、創(chuàng)建實體Item
package cn.zuoqy.springclouddemoitem.model;
/**
* Created by zuoqy on 14:05 2018/10/22.
*/
public class Item {
private Long id;
private String title;
private String pic;
private Long price;
public Item(){};
public Item(Long id, String title, String pic, Long price) {
this.id = id;
this.title = title;
this.pic = pic;
this.price = price;
}
}
3.1.4赂毯、編寫ItemService
編寫itemService用于實現(xiàn)具體的商品查詢邏輯,為了方便拣宰,我們并不真正的連接數(shù)據(jù)庫党涕,而是做模擬實現(xiàn)。
package cn.zuoqy.springclouddemoitem.service;
import cn.zuoqy.springclouddemoitem.model.Item;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* Created by zuoqy on 14:10 2018/10/22.
*/
@Service
public class ItemService {
public Item queryItemById(Long id) {
return MAP.get(id);
}
private static final Map<Long, Item> MAP = new HashMap<>();
static {
MAP.put(1L,new Item(1L, "商品標題1", "http://圖片1", 100L));
MAP.put(2L,new Item(2L, "商品標題2", "http://圖片2", 100L));
MAP.put(3L,new Item(3L, "商品標題3", "http://圖片3", 100L));
MAP.put(4L,new Item(4L, "商品標題4", "http://圖片4", 100L));
MAP.put(5L,new Item(5L, "商品標題5", "http://圖片5", 100L));
MAP.put(6L,new Item(6L, "商品標題6", "http://圖片6", 100L));
}
}
3.1.5巡社、編寫ItemController
package cn.zuoqy.springclouddemoitem.controller;
import cn.zuoqy.springclouddemoitem.model.Item;
import cn.zuoqy.springclouddemoitem.service.ItemService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zuoqy on 14:16 2018/10/22.
*/
@RestController
@RequestMapping(value = "item")
public class ItemController {
@Autowired
private ItemService itemService;
/**
* 對外提供接口服務(wù)膛堤,查詢商品信息
*/
@GetMapping(value = "query/{id}")
public Item queryItemById(@PathVariable("id") Long id) {
return itemService.queryItemById(id);
}
}
@RestController注解說明:
從源碼可以看出,這是一個組合注解晌该,組合了@Controller和Response注解肥荔。相當于我們同時寫了這2個注解。
@GetMapping注解說明:
@GetMapping注解是@RequestMapping(method=RequestMethod.GET)簡寫方式朝群。其功能都是一樣的燕耿。
同理還有其它注解:@DeleteMapping,@PostMapping,@PutMapping...
3.1.6、編寫application.properties文件
spring Boot以及spring Cloud項目支持yml
和properties
格式的配置文件姜胖。
yml格式是YAML(Yet Another Markup Language)編寫的格式誉帅,YAML和properties格式的文件是相互轉(zhuǎn)化的。如:
Server:
prot:18100
等價于
server.port=18100
配置文件的示例:3.1.7谭期、啟動程序測試
3.2堵第、實現(xiàn)訂單微服務(wù)
3.2.1、創(chuàng)建工程springcloud-demo-order (同3.1.1)
3.2.2隧出、導(dǎo)入依賴 (同3.1.2)
3.2.3、創(chuàng)建Order實體
package cn.zuoqy.springclouddemoorder.model;
import java.util.Date;
import java.util.List;
/**
* Created by zuoqy on 14:29 2018/10/22.
*/
public class Order {
private String orderId;
private Long userId;
private Date createDate;
private Date updateDate;
private List<OrderDetail> orderDetailList;
public Order() {}
public Order(String orderId, Long userId, Date createDate, Date updateDate, List<OrderDetail> orderDetailList) {
this.orderId = orderId;
this.userId = userId;
this.createDate = createDate;
this.updateDate = updateDate;
this.orderDetailList = orderDetailList;
}
}
3.2.4阀捅、創(chuàng)建訂單詳情OrderDetail實體
package cn.zuoqy.springclouddemoorder.model;
/**
* Created by zuoqy on 14:29 2018/10/22.
*/
public class OrderDetail {
private String orderId;
private Item item = new Item();
private OrderDetail() {}
public OrderDetail(String orderId, Item item) {
this.orderId = orderId;
this.item = item;
}
}
3.2.5胀瞪、將商品微服務(wù)中的Item類拷貝到當前工程
3.2.6、編寫ItemService
package cn.zuoqy.springclouddemoorder.service;
import cn.zuoqy.springclouddemoorder.model.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* Created by zuoqy on 14:43 2018/10/22.
*/
@Service
public class ItemService {
@Autowired
private RestTemplate restTemplate;
public Item queryItemById(Long id) {
return this.restTemplate.getForObject("http://127.0.0.1:8081/item/"+id,Item.class);
}
}
3.2.7饲鄙、編寫OrderService
該Service實現(xiàn)的根據(jù)訂單Id查詢訂單的服務(wù)凄诞,為了方便測試,我們將構(gòu)造數(shù)據(jù)實現(xiàn)忍级,不采用查詢數(shù)據(jù)庫的方式帆谍。
package cn.zuoqy.springclouddemoorder.service;
import cn.zuoqy.springclouddemoorder.model.Item;
import cn.zuoqy.springclouddemoorder.model.Order;
import cn.zuoqy.springclouddemoorder.model.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* Created by zuoqy on 14:34 2018/10/22.
*/
@Service
public class OrderService {
@Autowired
private ItemService itemService;
/**
* 根據(jù)訂單id查詢訂單數(shù)據(jù)
*/
public Order queryOrderById(String orderId) {
Order order = MAP.get(orderId);
if (null == order) {
return null;
}
List<OrderDetail> orderDetails = order.getOrderDetailList();
for (OrderDetail orderDetail: orderDetails) {
Item item = this.itemService.queryItemById(orderDetail.getItem().getId());
if (null == item) continue;
orderDetail.setItem(item);
}
return order;
}
private static final Map<String, Order> MAP = new HashMap<>();
static {
Order order = new Order();
order.setOrderId("9527order");
order.setCreateDate(new Date());
order.setUpdateDate(new Date());
order.setUserId(9527L);
List<OrderDetail> orderDetails = new ArrayList<>();
Item item = new Item();
item.setId(2L);
orderDetails.add(new OrderDetail(order.getOrderId(),item));
Item item2 = new Item();
item2.setId(3L);
orderDetails.add(new OrderDetail(order.getOrderId(),item2));
order.setOrderDetailList(orderDetails);
MAP.put(order.getOrderId(),order);
}
}
3.2.8、編寫OrderController
package cn.zuoqy.springclouddemoorder.controller;
import cn.zuoqy.springclouddemoorder.model.Order;
import cn.zuoqy.springclouddemoorder.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zuoqy on 14:56 2018/10/22.
*/
@RestController
@RequestMapping(value = "order")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping(value = "/query/{orderId}")
public Order queryOrderById(@PathVariable("orderId") String orderId) {
return orderService.queryOrderById(orderId);
}
}
3.2.9轴咱、向Spring容器中定義RestTemplate對象
package cn.zuoqy.springclouddemoorder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class SpringcloudDemoOrderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudDemoOrderApplication.class, args);
}
@Bean //向Spring容器中定義RestTemplate對象
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3.2.10汛蝙、編寫application.properties
3.2.11烈涮、啟動測試
3.3、添加okHttp的支持
okhttp是一個封裝URL窖剑,比HttpClient更友好易用的工具坚洽。目前似乎okhttp更流行一些。
官網(wǎng):http://square.github.io/okhttp/
RestTemplate底層默認使用的jdk的標準實現(xiàn)西土,如果我們想讓RestTemplate的底層使用okhttp非常簡單:
1.添加okhttp依賴
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
2.設(shè)置requestFactory
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
測試結(jié)果與之前一樣讶舰。
3.4、解決訂單系統(tǒng)中的url硬編碼問題
通過以上的測試我們發(fā)現(xiàn)需了,在訂單系統(tǒng)中要調(diào)用商品微服務(wù)中的查詢接口來獲取數(shù)據(jù)跳昼,在訂單微服務(wù)中將url硬編碼到代碼中,這樣顯然不好肋乍,因為鹅颊,運行環(huán)境一旦發(fā)生變化這個url地址將不可用。
解決方案:將url地址寫入到application.properties配置文件中住拭。
實現(xiàn):修改appcation.properties文件:
appcation.properties.png
修改ItemService中的實現(xiàn):
ItemService.png
Spring Cloud—一挪略、微服務(wù)架構(gòu)
Spring Cloud—二、Spring Cloud簡介
Spring Cloud—三滔岳、使用Spring Cloud實現(xiàn)微服務(wù)
Spring Cloud—四杠娱、Spring Cloud快速入門
Spring Cloud—五、注冊中心Eureka
Spring Cloud—六谱煤、使用Ribbon實現(xiàn)負載均衡
Spring Cloud—七摊求、容錯保護:Hystrix
Spring Cloud—八、使用Feign實現(xiàn)聲明式的Rest調(diào)用
Spring Cloud—九刘离、服務(wù)網(wǎng)關(guān)Spring Cloud Zuul
Spring Cloud—十室叉、使用Spring Cloud Config統(tǒng)一管理微服務(wù)
Spring Cloud—十一、使用Spring Cloud Bus(消息總線)實現(xiàn)自動更新