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

发布于 2021-12-07 11:34:06 字数 5447 浏览 909 评论 6

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

使用的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);
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

复古式 2021-12-08 12:42:25

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

把昨日还给我 2021-12-08 12:42:21

就是把地址是设置错了,应该指向要访问的微服务地址

风透绣罗衣 2021-12-08 12:41:59

回复
@特拉仔 :也就是指的 eureka注册中心的 Application名字吧

回眸一笑 2021-12-08 12:39:43

回复
在RequestMapping中指定的路径复制成了调用者的,而不是被调用者的

混吃等死 2021-12-08 11:12:13

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

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

沦落红尘 2021-12-08 05:04:26

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文