8小时学会使用Spring Cloud Alibaba(4)OpenFeign使用

    科技2024-11-08  10

    一 OpenFeign的作用

    声明式Http客户端,用于微服务之间的调用,集成Ribbon的负载均衡。

    二 用户微服务

    1.entity

    import com.fasterxml.jackson.databind.annotation.JsonSerialize; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor @Builder(toBuilder = true) @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class User implements Serializable{     //实体参数     private String id;     private String name;     private Integer money;     private Date createTime; }

     

     

    2.dao

    import com.study.cloudalibaba.entity.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Update; @Mapper public interface UserDao {     @Update("UPDATE t_user SET money=money-#{money} WHERE id=#{id}")     public void decrease(User user); }

     

    3.service

    import com.study.cloudalibaba.dao.UserDao; import com.study.cloudalibaba.entity.User; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserService {     @Resource     private UserDao userDao;     public void decrease(User user){         userDao.decrease(user);     } }

    4.controller

    import com.study.cloudalibaba.entity.User; import com.study.cloudalibaba.service.UserService; import com.study.cloudalibaba.vo.JsonVo; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class UserController {     @Resource     private UserService userService;     @RequestMapping(value = "/user/decrease")     public JsonVo<User> decrease(@RequestParam("id") String id,     @RequestParam("money") Integer money) {         userService.decrease(User.builder().id(id).money(money).build());         JsonVo<User> result = JsonVo.<User>builder()                 .code(200)                 .message("扣减余额成功")                 .build();         return result;     } }

     

    三 商品微服务 

    1.entity

    import com.fasterxml.jackson.databind.annotation.JsonSerialize; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor @Builder(toBuilder = true) @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class Product implements Serializable{     //实体参数     private String id;     private String name;     private Integer price;     private Integer stock;     private Date createTime; }

    2.dao

    import com.study.cloudalibaba.entity.Product; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Update; @Mapper public interface ProductDao {     @Update("UPDATE t_product SET stock=stock-#{stock} WHERE id=#{id}")     public void decrease(Product product); } 3.service import com.study.cloudalibaba.dao.ProductDao; import com.study.cloudalibaba.entity.Product; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class ProductService {     @Resource     private ProductDao productDao;     public void decrease(Product product){         productDao.decrease(product);     } }

    4.controller

    import com.study.cloudalibaba.entity.Product; import com.study.cloudalibaba.service.ProductService; import com.study.cloudalibaba.vo.JsonVo; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class ProductController {     @Resource     private ProductService productService;     @RequestMapping(value = "/product/decrease")     public JsonVo<Product> decrease(@RequestParam("id") String id,@RequestParam("stock") Integer stock) {         productService.decrease(Product.builder().id(id).stock(stock).build());         JsonVo<Product> result = JsonVo.<Product>builder()                 .code(200)                 .message("扣减库存成功")                 .build();         return result;     } }

    四 订单微服务

    1.entity

    import com.fasterxml.jackson.databind.annotation.JsonSerialize; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor @Builder(toBuilder = true) @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class Order implements Serializable{     //实体参数     private String id;     private String userId;     private String productId;     private Integer amount;     private Date createTime; }

    2.dao

    import com.study.cloudalibaba.entity.Order; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Update; @Mapper public interface OrderDao {     @Insert("INSERT INTO t_order (id, userId, productId,amount,createTime)      VALUES (#{id}, #{userId}, #{productId},#{amount},#{createTime})")     public void save(Order order); }

    3.service

    import com.study.cloudalibaba.dao.OrderDao; import com.study.cloudalibaba.entity.Order; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class OrderService {     @Resource     private OrderDao orderDao;     @Resource     private UserFeignService userFeignService;     @Resource     private ProductFeignService productFeignService;     public void save(Order order){         //这个只是测试,用户买了1件商品,花了10块钱         //添加订单         orderDao.save(order);         //扣账户余额         userFeignService.decrease(order.getUserId(),10);         //扣库存数量         productFeignService.decrease(order.getProductId(),order.getAmount());     } } import com.study.cloudalibaba.vo.JsonVo; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "cloudalibaba-user") public interface UserFeignService {     @PostMapping(value = "/user/decrease")     JsonVo decrease(@RequestParam("id") String id,@RequestParam("money") Integer money); } import com.study.cloudalibaba.vo.JsonVo; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "cloudalibaba-product") public interface ProductFeignService {     @PostMapping(value = "/product/decrease")     JsonVo decrease(@RequestParam("id") String id,      @RequestParam("stock") Integer stock); }

    4.controller

    import com.study.cloudalibaba.entity.Order; import com.study.cloudalibaba.service.OrderService; import com.study.cloudalibaba.vo.JsonVo; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Date; import java.util.UUID; @RestController @Slf4j public class OrderController {     @Resource     private OrderService orderService;     @RequestMapping(value = "/order/save")     public JsonVo<Order> save(@RequestParam("userId") String userId,     @RequestParam("productId") String productId,@RequestParam("amount") Integer amount)      {         orderService.save(Order.builder()                 .id(UUID.randomUUID().toString().replaceAll("-",""))                 .userId(userId)                 .productId(productId)                 .amount(amount)                 .createTime(new Date())                 .build());         JsonVo<Order> result = JsonVo.<Order>builder()                 .code(200)                 .message("下订单成功")                 .build();         return result;     } }

    五 IDEA 开启Run Dashboard管理

    1.找到右上角面板,选择Edit Configurations

    2.选择Templates,点击右边的+号,选择Spring Boot,点确定

    依次启动用户微服务,商品微服务,订单微服务

    六 将用户微服务,商品微服务,订单微服务分别上传到192.168.1.1,192.168.1.2,192.168.1.3服务器,并分别启动

    nohup java -jar cloudalibaba-user.jar >log.file 2>&1 &

    nohup java -jar cloudalibaba-product.jar >log.file 2>&1 &

    nohup java -jar cloudalibaba-order.jar >log.file 2>&1 &

    随便访问1台服务器测试:http://192.168.1.1:9003/order/save?userId=1&productId=1&amount=1

     

    Processed: 0.027, SQL: 8