Spring Cloud微服务案例无事务版

## 1.新建项目dbinit用于初始化数据库
## 2.搭建eureka注册中心
## 3.新建共工依赖的maven工程:order-parent
## 4.分别创建account、storage、order。账户、库存和订单模块
## 5.复制全局唯一发号器工程到工作目录下:

修改yml配置:添加基本配置和注册

Spring Cloud微服务案例无事务版_第1张图片

它有两个算法:一个雪花算法,一个数据库算法,我们这里用数据库算法

Spring Cloud微服务案例无事务版_第2张图片

将原有的db1.properties复制一份改为seata_order.properties,并修改其中配置

Spring Cloud微服务案例无事务版_第3张图片
启动测试
http://localhost:9090/segment/ids/next_id?businessType=order_business
Spring Cloud微服务案例无事务版_第4张图片

## 6.order 远程调用发号器、库存、账户

yml 配置ribbon,关闭重试,增加超时时间

Spring Cloud微服务案例无事务版_第5张图片

启动类添加 @EnableFeignClients
声明式客户端接口
  • EasyIdClient
  • AccountClient
  • StorageClient

Spring Cloud微服务案例无事务版_第6张图片

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 使用接口调用远程服务
Spring Cloud微服务案例无事务版_第7张图片

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

控制台会有日志输出

Spring Cloud微服务案例无事务版_第8张图片
Spring Cloud微服务案例无事务版_第9张图片

你可能感兴趣的:(java)