开发者社区> 问答> 正文

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

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

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

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

展开
收起
爱吃鱼的程序员 2020-06-06 20:47:31 2294 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>找到答案了。。。在MyService居然指定的是自身的地址,而不是微服务的地址,扇自己一耳光</p>
    
                    
    
                        <div class='ref'><h4>引用来自“特拉仔”的评论</h4><p>找到答案了。。。在MyService居然指定的是自身的地址,而不是微服务的地址,扇自己一耳光</p>
    
    
                            回复 <a class="referer" target="_blank">@坐拥三千佳丽</a>  : 在RequestMapping中指定的路径复制成了调用者的,而不是被调用者的
                        
    
                            回复 <a class="referer" target="_blank">@特拉仔</a>  :也就是指的  eureka注册中心的 Application名字吧
                        
    
                            就是把地址是设置错了,应该指向要访问的微服务地址
                        
    
                        <p>还是没看出问题在哪啊</p>
                    
    
                        <p>A通过Fegin访问B,在feign的service中写的请求路径A的Controller中的requestMapping中的地址。。</p>
    
    2020-06-06 20:47:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建基于SpringCloud的微服务应用 立即下载
Nacos架构&原理 立即下载
Apache Dubbo3 源码深入解读 立即下载