最近用SpringCloud做微服务,一直无法成功进行服务消费。

我使用的服务消费者是Feign,声明式调用服务提供者。

排查过程

1.检查服务提供者:

(1)对提供的方法进行测试,确保提供的服务没有问题。

(2)是否在控制层上方添加了@Controller。

(3)方法的method是否正确。是GET还是POST。

2.仔细检查服务消费者:

(1)服务名value是否正确。服务名不一定是项目名,要检查服务提供者的application配置文件,对应的spring.application.name属性,也可以直接打开注册中心查看服务名。

(2)是否存在上下文。要检查服务提供者的application配置文件,是否有 context-path 属性。如果存在上下文属性,要加到方法参数value的前面 。

(3)方法的method是否正确。是GET还是POST。

(4)方法的url是否正确。对应服务提供者的url。

3.检查断路由Hystrix:

(1)在类的上方是否加了@Component

4.检查是在哪个环境下操作:

如果本地环境的程序没有关闭,就对部署在开发环境中的服务发起消费请求,可能会失败。

示例如下:

服务提供者,服务名为invoice,如下:

  1. @Controller
  2. public class InvoiceMsgController extends BaseController{
  3. private static final Logger logger=LoggerFactory.getLogger(InvoiceMsgController.class);
  4. @Autowired
  5. private SendMsgService sendMsgService;
  6. /**
  7. * 服务提供者的方法
  8. */
  9. @RequestMapping(value = "/sentMsg", method = RequestMethod.POST )
  10. public void sentMsgToWeChat(HttpServletRequest request) throws Exception {
  11. // 从请求中获取sendJson等其他逻辑忽略
  12. sendMsgService.SendWechatMessage(sendJson);
  13. logger.info("==============>成功推送个人号消息。");
  14. }
  15. }

服务提供者的yml配置文件:

  1. server:
  2. port: 10010
  3. context-path: /dev
  4. spring:
  5. application:
  6. name: invoice

服务消费者Feign,调用的服务提供者名称为invoice,如下:

  1. @FeignClient(value="invoice",fallback=InvoiceMsgHystrix.class)
  2. public interface InvoiceMsgService {
  3. /**
  4. * 调用服务提供者中的方法。
  5. * 注意:此处的/dev 是invoice服务的上下文,相关的properties配置为: server.context-path=/dev。如果服务提供者有上下文,就要加在Feign的value里面。没有则不加。
  6. */
  7. @RequestMapping(value = "/dev/sentMsg", method = RequestMethod.POST)
  8. JSONObject sentMsgToWeChat(JSONObject invoiceJson) ;
  9. }

服务熔断如下:

  1. /**
  2. * 断路由
  3. *
  4. */
  5. @Component
  6. public class InvoiceMsgHystrix implements InvoiceMsgService{
  7. private JSONObject createObject(){
  8. JSONObject object=new JSONObject();
  9. object.put("errcode", "0001");
  10. object.put("description", "发送消息失败");
  11. return object;
  12. }
  13. @Override
  14. public JSONObject sentMsgToWeChat(JSONObject request) {
  15. // TODO Auto-generated method stub
  16. return createObject();
  17. }
  18. }

微服务SpringCloud无法进行服务消费的更多相关文章

  1. 「 从0到1学习微服务SpringCloud 」05服务消费者Fegin

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  2. 「 从0到1学习微服务SpringCloud 」04服务消费者Ribbon+RestTemplate

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  3. 「 从0到1学习微服务SpringCloud 」10 服务网关Zuul

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config 「 从0到1学习微服务SpringCloud 」07 RabbitM ...

  4. 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  5. 「 从0到1学习微服务SpringCloud 」07 RabbitMq的基本使用

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  6. 「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  7. SpringCloud Alibaba Nacos 服务注册

    业务服务接入Nacos服务治理中心 启动Nacos访问地址为:http://101.200.201.195:8848/nacos/ 创建bom工程用于管理依赖(下方附加源码地址) 准备工作完成后开始接 ...

  8. springcloud之自定义简易消费服务组件

    本次和大家分享的是怎么来消费服务,上篇文章讲了使用Feign来消费,本篇来使用rest+ribbon消费服务,并且通过轮询方式来自定义了个简易消费组件,本文分享的宗旨是:自定义消费服务的思路:思路如果 ...

  9. 一个天气的微服务springcloud

    1.开发环境 jdk8  和 gradle 4. ---------------搭建一个天气预报系统 1.bootstrap就是一堆样式文件,首先有html标签,然后加上各种样式以后就变得好看了2.j ...

随机推荐

  1. java栈的实现复习

    栈是一种线性表,仅限在一端进行插入和删除操作,特点是先进后出. 由于栈是一种线性结构,首先可以想到用数组来实现,但由于数组初始化后容量就已经确定,如果不添加扩容操作,则会出现栈溢出,同时扩容操作也会降 ...

  2. GoStudy——Go语言入门第一个事例程序:HelloWorld.go

    package main import ( ​ "fmt" ) func main() { ​ fmt.Println("Hello,world!!!--2019年4月1 ...

  3. ubuntu deb pacakge 开发

    安装构建工具 apt-get install pbuilder 推荐安装 sudo apt-get install build-essential autoconf automake \ autoto ...

  4. Devexpress Winform MVVM

    归纳总结备忘 Devexpress Winform MVVM Practice 前言 MVVM Devexpress 正文 databindings及 UI Triggers Command 委托Co ...

  5. 关于bit,bin文件的一些研究

    关于bit,bin文件的一些研究 bit文件里面有head information 但bin文件里面并不包含 bit 文件里面包含如下信息 SPI flash 时钟需要用到的源语 watchdog 设 ...

  6. Ubuntu更新时提示错误 E: Sub-process /usr/bin/dpkg returned an error code (1)

    $ sudo su //root权限 $ sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old //现将info文件夹更名 $ sudo mkdir /v ...

  7. Centos6.5部署Sonar6.7.1备注

    1.一定要用非root账号登录(自己建立账号),建立Sonar目录并部署,因为使用了Elasticsearch 5.6.3做搜索服务器,而它不允许用root账号启动服务,会报如下错误: Excepti ...

  8. <Differential Geometry of Curves and Surfaces>(by Manfredo P. do Carmo) Notes

    <Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...

  9. 3076: 神经网络(bfs和拓扑排序)

    3076: 神经网络 时间限制: 1 Sec  内存限制: 125 MB提交: 7  解决: 5[提交][状态][讨论版][命题人:外部导入][Edit] [TestData] [同步数据] 题目描述 ...

  10. java web 三大核心组件Filter

    Filter 过滤: 定义: 过滤器是客户端与服务端之间的一道网,可以对请求的数据进行拦截和修改,同时也可以对,响应数据进行拦截和修改