通过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成功

    

订单服务,使用feign调用服务的更多相关文章

  1. 使用Feign调用服务的问题

    最近在搞微服务的东西,系统A提供了一个服务,定义方式: @RequestMapping(value = "/hasSuberList", method = RequestMetho ...

  2. Spring Cloud 使用Feign调用服务传递Header中的参数

    1.使用Feign 调用其他微服务,尤其是在多级调用的同时,需要将一些共同的参数传递至下一个服务,如:token.比较方便的做法是放在请求头中,在Feign调用的同时自动将参数放到restTempla ...

  3. Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录

    背景 :在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnectio ...

  4. spring cloud feign 调用服务注意问题

    服务端 rest api @RequestMapping(value = "/phone") public ResponsePhone getPhone(@RequestParam ...

  5. 使用feign调用服务的时候注意的问题

    服务端 rest api @RequestMapping(value = "/phone") public ResponsePhone getPhone(@RequestParam ...

  6. SpringCloud使用Feign调用服务时,@FeignClient注解无法使用

    关于解决这个问题的理论根源传送门:https://blog.csdn.net/alinyua/article/details/80070890我在这里只提供解决方案 0. 结论和解决方案 Spring ...

  7. 使用springcloud的feign调用服务时出现的错误:关于实体转换成json错误的介绍

    http://blog.csdn.net/java_huashan/article/details/46428971 原因:实体中没有添加无参的构造函数 fastjson的解释: http://www ...

  8. Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录(转)

    https://blog.csdn.net/uotail/article/details/84673347

  9. Spring Cloud微服务系列文,服务调用框架Feign

    之前博文的案例中,我们是通过RestTemplate来调用服务,而Feign框架则在此基础上做了一层封装,比如,可以通过注解等方式来绑定参数,或者以声明的方式来指定请求返回类型是JSON.    这种 ...

随机推荐

  1. java集合类型源码解析之ArrayList

    前言 作为一个老码农,不仅要谈架构.谈并发,也不能忘记最基础的语言和数据结构,因此特开辟这个系列的文章,争取每个月写1~2篇关于java基础知识的文章,以温故而知新. 如无特别之处,这个系列文章所使用 ...

  2. Vue学习手记04-跨域问题

    01-安装axios,指令(npm install --save axios)02-解决跨域问题 在config=>中创建一个新的文件proxyConfig.js module.exports ...

  3. Fences_3.08破解安装

    Fences_3.08破解安装 一.总结 一句话总结: 找破解软件去吾爱破解论坛,非常节约时间 二.Fences_3.08破解安装(亲测有效) 来源:吾爱破解论坛 百度网盘下载地址:链接:https: ...

  4. react组件中返回并列元素的方法

    我们在写react组件的时候,经常会遇到这种问题,在render中return元素只能有一个顶级元素,比如div,假如写成这样就会报错: render(){ return( <div>12 ...

  5. hadoop 2.7.3伪分布式环境运行官方wordcount

    hadoop 2.7.3伪分布式模式运行wordcount 基本环境: 系统:win7 虚机环境:virtualBox 虚机:centos 7 hadoop版本:2.7.3 本次以伪分布式模式来运行w ...

  6. Java 13新特性

    switch表达式 switch表达式是Java 12开始就提供的预览特性,到了Java 13仍然没有被转正.此特性可以把switch的执行结果组合到复合表达式中进行运算. import java.t ...

  7. ISO/IEC 9899:2011 条款6.3.2——其它操作数

    6.3.2 其它操作数 6.3.2.1 左值,数组,与函数指派符 1.一个左值是潜在地指派一个对象的一个表达式(具有一个对象类型,而不是void):[注:名字“左值”源自于赋值表达式E1 = E2,在 ...

  8. Eclipse中把项目导出为war包【我】

    项目右键,Export 全部默认一路下一步,选择一个目标文件夹,确定即可.

  9. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_04-用户认证技术方案-SpringSecurityOauth2

    2.3 Spring security Oauth2认证解决方案 本项目采用 Spring security + Oauth2完成用户认证及用户授权,Spring security 是一个强大的和高度 ...

  10. 算法习题---3.08循环小数(UVa202)

    一:题目 输入整数a和b(<=a<=,<=b<=),输出a/b的循环小数表示以及循环节长度. 例如,a=,b=,小数表示为0.(),循环字节长度为21 当循环节长度超过50时, ...