## 1.新建我的项目dbinit用于初始化数据库
## 2.搭建eureka注册核心
## 3.新建共工依赖的maven工程:order-parent
## 4.别来源gaodai#ma#com搞*代#码网离创立account、storage、order。账户、库存和订单模块
## 5.复制全局惟一发号器工程到工作目录下:
批改yml配置:增加根本配置和注册
它有两个算法:一个雪花算法,一个数据库算法,咱们这里用数据库算法
将原有的db1.properties复制一份改为seata_order.properties,并批改其中配置
启动测试
http://localhost:9090/segment/ids/next_id?businessType=order_business
## 6.order 近程调用发号器、库存、账户
yml 配置ribbon,敞开重试,减少超时工夫
启动类增加 @EnableFeignClients 申明式客户端接口
- EasyIdClient
- AccountClient
- StorageClient
AccountClient
package cn.tedu.order.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import java.math.BigDecimal; @FeignClient(name="account") public interface AccountClient { @GetMapping("/decrease") String decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money); }
StorageClient
package cn.tedu.order.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name="storage") public interface StorageClient { //缩小商品库存 @GetMapping("/decrease") String decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count); }
EasyIdClient
package cn.tedu.order.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name="easy-id-generator") public interface EasyIdClient { @GetMapping("segment/ids/next_id") String nextId(@RequestParam("businessType") String businessType); }
OrderServiceImpl 应用接口调用近程服务
package cn.tedu.order.service; import cn.tedu.order.entity.Order; import cn.tedu.order.feign.AccountClient; import cn.tedu.order.feign.EasyIdClient; import cn.tedu.order.feign.StorageClient; import cn.tedu.order.mapper.OrderMapper; import cn.tedu.order.mapper.OrderMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Random; @Service public class OrderServiceImpl implements OrderService { @Autowired private OrderMapper orderMapper; @Autowired private EasyIdClient easyIdClient; @Autowired private AccountClient accountClient; @Autowired private StorageClient storageClient; @Override public void create(Order order) { // TODO: 从全局惟一id发号器取得id,这里临时随机产生一个 orderId //Long orderId = Long.valueOf(new Random().nextInt(Integer.MAX_VALUE)); //获取一个随机id order_bussiness是哪里定义的呢????????????? String s = easyIdClient.nextId("order_business"); //将String型的s装换为Long类型 Long orderId = Long.valueOf(s); order.setId(orderId); orderMapper.create(order); // TODO: 调用storage,批改库存 storageClient.decrease(order.getProductId(),order.getCount()); // TODO: 调用account,批改账户余额 accountClient.decrease(order.getUserId(),order.getMoney()); //调用完了,拜访,怎么拜访呢?必定是通过订单拜访,那同过订单又怎么拜访呢 //http://localhost:8083/create?userId=1&productId=1&count=10&money=100 } }
启动测试
http://localhost:8083/create?userId=1&productId=1&count=10&money=100
控制台会有日志输入