SpringCloud-Feign按照官方文档整还是报错404

特拉仔 发布于 2018/06/27 19:10
阅读 8K+
收藏 0

看了半天书和官方文档也没找到错误的原因,请各位帮忙看看

使用的SpringCLoud版本是Dalstonsr1

使用的SpringBoot版本是1.5.9

报错内容:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is feign.FeignException: status 404 reading MyService#list(); content:
{"timestamp":1530096509277,"status":404,"error":"Not Found","message":"Not Found","path":"/web/payment/query"}

Caused by: feign.FeignException: status 404 reading MyService#list(); content:
{"timestamp":1530096509277,"status":404,"error":"Not Found","message":"Not Found","path":"/web/payment/query"}

Eurka控制台截图

Eureka应该是没什么问题,因为在同一个项目里面用Ribbon是可以正确访问到微服务的

Feign工程

Pom.xml(非完整版)

        <!-- 引入自己定义的common -->
        <dependency>
            <groupId>com.jv</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Ribbon需要从eureka中拿微服务      begin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- Ribbon需要从eureka中拿微服务      end-->
        <!-- Ribbon begin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <!-- Ribbon end-->
        <!-- Feign start-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- Feign end-->

application.yml

server:
  port: 80
eureka:
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    service-url:
      defaultZone: http://eureka01.com:7001/eureka/,http://eureka02.com:7002/eureka/,http://eureka03.com:7003/eureka/

接口

package com.jv.cloud.service;

import com.jv.entity.Payment;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@FeignClient("PAYMENTSERVICE")
public interface MyService {
    @RequestMapping(value = "/web/payment/query",method = RequestMethod.GET)
    public List<Payment> list();

    @RequestMapping(value = "/web/new/query",method = RequestMethod.GET)
    public List<Payment> list_new();

    @RequestMapping(value = "/web/payment/discovery",method = RequestMethod.GET)
    public Object discovery();

    @RequestMapping(value = "/web/new/discovery",method = RequestMethod.GET)
    public Object discovery_new();
}

Controller

package com.jv.cloud.controller;

import com.jv.cloud.service.MyService;
import com.jv.entity.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class WebPaymentController {

    //private PaymentService paymentService;
    @Autowired
    private MyService myservice;

    @RequestMapping(value = "/web/payment/query",method = RequestMethod.GET)
    public List<Payment> list(){ return myservice.list(); }

    @RequestMapping(value = "/web/new/query",method = RequestMethod.GET)
    public List<Payment> list_new(){
        return myservice.list_new();
    }

    @RequestMapping(value = "/web/payment/discovery",method = RequestMethod.GET)
    public Object discovery(){
        return myservice.discovery();
    }

    @RequestMapping(value = "/web/new/discovery",method = RequestMethod.GET)
    public Object discovery_new(){
        return myservice.discovery_new();
    }
}

主启动类

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class WebPaymentFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebPaymentFeignApplication.class, args);
    }
}

如果还需要什么信息,请留言,谢谢!

加载中
0
特拉仔
特拉仔

找到答案了。。。在MyService居然指定的是自身的地址,而不是微服务的地址,扇自己一耳光

0
小胖子_
小胖子_

引用来自“特拉仔”的评论

找到答案了。。。在MyService居然指定的是自身的地址,而不是微服务的地址,扇自己一耳光

我有遇到404,MyService指定的是自身的地址是什么意思?

特拉仔
特拉仔
回复 @坐拥三千佳丽 : 在RequestMapping中指定的路径复制成了调用者的,而不是被调用者的
坐拥三千佳丽
坐拥三千佳丽
回复 @特拉仔 :也就是指的 eureka注册中心的 Application名字吧
特拉仔
特拉仔
就是把地址是设置错了,应该指向要访问的微服务地址
0
俊杰kobe
俊杰kobe
该评论暂时无法显示,详情咨询 QQ 群:点此入群
0
特拉仔
特拉仔

A通过Fegin访问B,在feign的service中写的请求路径A的Controller中的requestMapping中的地址。。

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部