乐趣区

关于java:十八order调用item和user

1. 在 order 的 pom 中加三个依赖

feign,hystrix,actuator

1.1 编辑配置类

# 启用 feign
feign:
  hystrix:
    enabled: true
#裸露监控端点
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

1.2 启动类加注解:@EnableFeignClients、@EnableCircuitBreaker

1.3 为 item 和 user 增加申明式客户端接口和实现类

cn.tedu.sp04.feign.ItemClient
cn.tedu.sp04.feign.UserClient
@FeignClient(name="item-service",fallback = ItemClientFB.class)
public interface ItemClient {
    // 依据 orderId 获取商品
 @GetMapping("/{orderId}" )
    JsonResult<List<Item>> getItems(@PathVariable String orderId);
    // 缩小商品库存
 @PostMapping("/decreaseNumber")
    JsonResult<?> decreaseNumber(@RequestBody List<Item> items);
}
@FeignClient(name="user-service",fallback =UserClientFB.class)
public interface UserClient {
    // 获取用户,依据 orderId?????不对,值依据 userId
 /* @GetMapping("{orderId}")
 JsonResult<User> getUser(@PathVariable String orderId); // 减少用户积分,依据 orderId 和积分
 @PostMapping("{orderId}/score") JsonResult<?> addScore(@PathVariable String orderId,Integer score);*/ @GetMapping("/{userId}")
    JsonResult<User> getUser(@PathVariable Integer userId);
    @PostMapping("/{userId}/score")
    JsonResult<?> addScore(@PathVariable Integer userId,Integer score);
}

@Component
public class ItemClientFB implements ItemClient{
    @Override
 public JsonResult<List<Item>> getItems(String orderId) {
        // 增加缓存数据
 if(Math.random()>0.5){ArrayList<Item> list = new ArrayList<>();
            list.add(new Item(1,"缓存商品 1",1));
            list.add(new Item(2,"缓存商品 2",2));
            list.add(new Item(3,"缓存商品 3",3));
        return JsonResult.ok().data(list);
        }
        return JsonResult.err("获取商品失败");
    }
    @Override
 public JsonResult<?> decreaseNumber(List<Item> items) {return JsonResult.err("缩小商品库存失败");
    }
}

@Component
public class UserClientFB implements UserClient{
    @Override
 public JsonResult<User> getUser(Integer userId) {
        // 缓存数据
 if(Math.random()>0.5){User user = new User(1,"缓存用户","缓存明码");
            return JsonResult.ok().data(user);
        }
        return JsonResult.err("获取用户失败");
    }
    @Override
 public JsonResult<?> addScore(Integer userId, Integer score) {return JsonResult.err("减少用户积分失败");
    }
}

1.4 编辑 OrderServiceImpl

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
 private ItemClient itemClient;
    @Autowired
 private UserClient userClient;
    @Override
 public Order getOrder(String orderId) {log.info("获取订单:orderId="+orderId);
        //TODO: 近程调用商品服务,获取商品列表
 JsonResult<List<Item>> items = itemClient.getItems(orderId);
        //TODO: 近程调用用户服务,获取用户数据
 //userClient.getUser(userId); 获取用户时,id 写死
 JsonResult<User> user = userClient.getUser(99);
        // 写死订单数据
 Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData());
        order.setItems(items.getData());
        return order;
    }
    @Override
 public void saveOrder(Order order) {log.info("保留订单:"+order);
        //TODO: 近程调用商品服务,缩小商品库存
 itemClient.decreaseNumber(order.getItems());
        //TODO: 近程调用用户服务,减少用户积分
 userClient.addScore(order.getUser().getId(),100);
    }
}

1.5order-service 配置启动参数, 启动两台服务器

1.6 启动服服,进行测试

*   依据 orderid,获取订单
http://localhost:8201/123abc
http://localhost:8202/123abc

*   保留订单
http://localhost:8201/

[http://localhost:8202/

留神:商品可能能获取到,可能获取的是缓存的,也肯获取不到。显示 null。

退出移动版