关于java:Spring-Cloud微服务案例无事务版

45次阅读

共计 2922 个字符,预计需要花费 8 分钟才能阅读完成。

## 1. 新建我的项目 dbinit 用于初始化数据库
## 2. 搭建 eureka 注册核心
## 3. 新建共工依赖的 maven 工程:order-parent
## 4. 别离创立 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

 控制台会有日志输入 


正文完
 0