qq_34940987
子鸢0618
2021-01-24 00:43

spring cloud消费端调服务端时报的错 请大神帮忙看看是哪的问题

  • spring
  • java
  • intellij-idea

2021-01-23 12:58:31.416 ERROR 396 --- [nio-8082-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.FeignException$NotFound: [404] during [GET] to [http://yh-provider/user/getUser] [UserService#selectByPrimaryKey(String)]: [{"timestamp":"2021-01-23T04:58:31.393+00:00","status":404,"error":"Not Found","message":"","path":"/user/getUser"}]] with root cause

feign.FeignException$NotFound: [404] during [GET] to [http://yh-provider/user/getUser] [UserService#selectByPrimaryKey(String)]: [{"timestamp":"2021-01-23T04:58:31.393+00:00","status":404,"error":"Not Found","message":"","path":"/user/getUser"}]
    at feign.FeignException.clientErrorStatus(FeignException.java:201) ~[feign-core-10.10.1.jar:na]
    at feign.FeignException.errorStatus(FeignException.java:177) ~[feign-core-10.10.1.jar:na]
    at feign.FeignException.errorStatus(FeignException.java:169) ~[feign-core-10.10.1.jar:na]
    at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92) ~[feign-core-10.10.1.jar:na]
    at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96) ~[feign-core-10.10.1.jar:na]
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-10.10.1.jar:na]
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.10.1.jar:na]
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.10.1.jar:na]
    at com.sun.proxy.$Proxy116.selectByPrimaryKey(Unknown Source) ~[na:na]
    at com.yh.web.controller.UserController.getUser(UserController.java:29) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.2.jar:5.3.2]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.41.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.2.jar:5.3.2]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.41.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.2.jar:5.3.2]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.2.jar:5.3.2]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93) ~[spring-boot-actuator-2.4.1.jar:2.4.1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.2.jar:5.3.2]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.2.jar:5.3.2]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_144]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_144]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.41.jar:9.0.41]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144]

 

 

eureka配置信息
server:
  port: 8000

eureka:
  instance:
    hostname: localhost
  client:
    #     声明是否将自己的信息注册到 Eureka 服务器上
    registerWithEureka: false
    #     是否到 Eureka 服务器中抓取注册信息
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


spring:
  application:
    name: eurka-service

并且确定生产者、消费者均已注册到注册中心了

 

 生产者方代码

 

配置信息

spring:
  application:
    name: yh-provider
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/yh?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
  redis:
    host: 127.0.0.1
    port: 6379
    lettuce:
      pool:
        #连接池最大链接数默认值为8
        max-active: 8
        #连接池最大阻塞时间(使用负值表示没有限制)默认为-1
        max-wait: -1
        #连接池中的最大空闲连接数 默认为8
        max-idle: 8
        #连接池中的最小空闲连接数 默认为0
        min-idle: 0

server:
  port: 8081

eureka:
  client:
    serviceUrl:
      #defaultZone 这个是不会提示的,此处需要自己写
      #实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
      #但是自己还需要写一个defaultZone;
      defaultZone: http://localhost:8000/eureka
  #服务注册中心实例的主机名
  instance:
    hostname: ${spring.application.name}
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}


mybatis:
  mapper-locations: classpath*:/com/yh/mapper/*.xml
  type-aliases-package: com.yh.entity

# 启用hiddenMethod过滤器,否则@PostMapping不能使用
mvc:
  hiddenmethod:
    filter:
      enabled: true

启动类信息

package com.yh.web;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication(scanBasePackages="com.yh")//引用provider-service扫描地址,否则将无法注入
@EnableEurekaClient
@MapperScan(basePackages = {"com.yh.mapper"})//扫描mapper接口
@EnableSwagger2//集成swagger2
public class ProviderWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderWebApplication.class, args);
    }

}

 

接口信息:

package com.yh.web.controller;

import com.yh.common.vo.ResponseVO;
import com.yh.entity.User;
import com.yh.service.UserService;
import io.swagger.annotations.*;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping(name = "/user")
@Api(tags = "UserController")
public class UserController {

    @Autowired
    private UserService userService;

    
    @PostMapping(name = "/add")
    @ApiOperation(value = "注册用户接口",response = ResponseVO.class)
    @ApiResponses({
            @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "SC_BAD_REQUEST"),
            @ApiResponse(code = HttpStatus.SC_OK,message = "成功")
    })
    public ResponseVO add(@RequestBody User user){
        return userService.insert(user);
    }

    @GetMapping(name = "/getUser")
    @ApiOperation(value = "获取用户信息接口",response = User.class)
    @ApiResponses({
            @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "SC_BAD_REQUEST"),
            @ApiResponse(code = HttpStatus.SC_OK,message = "成功")
    })
    public ResponseVO getUser(@RequestParam("id") String id){
        return userService.selectByPrimaryKey(id);
    }

}

 

消费者端信息

 

配置信息

server:
  #定义端口号
  port: 8082

spring:
  application:
    #定义应用名称,即服务名称
    name: yh-consumer

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka
  #服务注册中心实例的主机名
  instance:
    hostname: ${spring.application.name}
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}


#设置feign客户端超时时间(OpenFeign默认支持Ribbon)
ribbon:
  # 指的是建立连接后从服务器读取到可用资源所用的时间,单位ms
  ReadTimeout: 5000
  # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间,单位ms
  ConnectTimeout: 5000

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.xhanglog.springcloud.service.PaymentFeignService: debug

 

启动类信息

package com.yh.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableFeignClients(basePackages = "com.yh.consumer")
@EnableEurekaClient
@EnableSwagger2//集成swagger2
public class ConsumerWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerWebApplication.class, args);
    }

}

 

 

controller代码:

package com.yh.web.controller;

import com.yh.common.vo.ResponseVO;
import com.yh.consumer.UserService;
import com.yh.consumer.dto.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(name = "/user")
@Api(tags = "UserController")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(name = "/getUser")
    @ApiOperation(value = "获取用户信息接口",response = User.class)
    @ApiResponses({
            @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "SC_BAD_REQUEST"),
            @ApiResponse(code = HttpStatus.SC_OK,message = "成功")
    })
    public ResponseVO getUser(String id){
        return userService.selectByPrimaryKey(id);
    }

}

 

Feign接口代码:
package com.yh.consumer;


import com.yh.common.vo.ResponseVO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@Service
@FeignClient(value = "yh-provider")//这里的value对应调用服务的spring.applicatoin.name
public interface UserService {

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/getUser",method = RequestMethod.GET)
    ResponseVO selectByPrimaryKey(String id);


}

主要代码就这些。生产者、消费者是两个多模块工程。

感觉请求的接口已经对应上了。就是请求不过去。请大神们指正。我vx:1378975235.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答