zoukankan      html  css  js  c++  java
  • 订单服务,使用feign调用服务

    通过feign实现订单中心(feign调用服务,在orderservice中调用product-server)

        1、copy orderserver 为orderserverfeign

        2、并使用idea打开

        3、将服务增加个feign

        在项目右键修改名称orderserver为orderserverfeign

     

        4、打开端口为8661 yml中的端口修改为8661

        5、修改各类名称refacter,并将application名字也修改正确

        6、增加依赖:

                    <dependency>

                 <groupId>org.springframework.cloud</groupId>

                 <artifactId>spring-cloud-starter-openfeign</artifactId>

                 </dependency>

        7、在Application类中增加注释

            @EnableFeignClients

        8、service中增加一个type为interface的类:ProductFeignService

            增加注解:@FeignClient(name="product-server")

                注意:name为服务定义的名称

                @FeignClient(name="PRODUCT-SERVICE") /*调用服务的名称必须和原服务名称一致,需要在服务中查找*/ 并增加一个函数,

                

                    public interface ProductFeignService {

                        @GetMapping("/api/v1/product/findById") /*调用服务目录必须和原服务名称一致,需要在服务中查找*/

                        String fingById(@RequestParam(value = "id") int id);

                    /**

                     * http://192.168.136.128:8766/api/v1/product/findById?id=2

                     */

                    }

     

        9、增加包utils(用于解析josn字符串为json对象)

            增加类JsonUtils

             private static final ObjectMapper objectMapper =new ObjectMapper();

     

                /**

                 * json字符串转换为Json格式

                 */

                public static JsonNode str2JsonNode(String str){

                    try {

                        return objectMapper.readTree(str);

                    } catch (IOException e)

                    {

                        return null;

                    }

     

                }

        10、impl实现中增加

                    @Autowired

                    private ProductFeignService productFeignService;

                    public ProductOrderFeign save(int userId, int productId) {

                    //获取商品详情TODO

                    String response=productFeignService.fingById(productId);

                    JsonNode jsonNode=JsonUtils.str2JsonNode(response);

     

                    ProductOrderFeign productOrderFeign =new ProductOrderFeign();

                    productOrderFeign.setCreateTime(new Date());

                    productOrderFeign.setUserId(userId);

                    productOrderFeign.setProductId(productId);

                    productOrderFeign.setTradeNo(UUID.randomUUID().toString());

                    productOrderFeign.setProductName(jsonNode.get("name").toString());

                    productOrderFeign.setPrice(Integer.parseInt(jsonNode.get("price").toString()));

     

                    return productOrderFeign;;

        11、运行、访问http://192.168.136.128:8661/api/v1/orderfeign/save?userId=2&productId=2成功

        

  • 相关阅读:
    回文自动机+DFS
    iOS 图表工具charts之CandleStickChartView(K线)
    iOS 图表工具charts之PieChartView
    iOS 图表工具charts之BarChartView
    iOS 图表工具charts之LineChartView
    iOS 图表工具charts介绍
    cocoapods [!] Unable to find a pod with name, author, summary, or description matching `xx`
    iOS charts CombinedChartView First and last bars are shown half only
    iOS OC中桥接swift第三方库
    iOS 日志系统 本地日志打包上传到服务器
  • 原文地址:https://www.cnblogs.com/programer-xinmu78/p/10520773.html
Copyright © 2011-2022 走看看