【问题标题】:ZUUL API GATEWAY RIBBONCLIENT REQUEST ERRORZUUL API 网关 RIBBONCLIENT 请求错误
【发布时间】:2021-01-29 12:46:17
【问题描述】:

我正在尝试使用 zuul api、eureka server、docker 和 docker compose 为微服务设置 API 网关。

我有这样的客户。

@SpringBootApplication
@EnableFeignClients("com.microService.movieServerClient")
@EnableDiscoveryClient
public class MovieServerClientApplication {

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

}
package com.microService.movieServerClient;

import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import com.microService.movieServerClient.models.Movie;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@FeignClient(name="zuul-api-gateway")
@RibbonClient(name="movie-server")
public interface MovieServerClientProxy {

    @GetMapping("/list")
    List<Movie> getAllMovies();
}

这是电影控制器。

@RestController
public class MovieController {

  //  @Autowired
   // private DiscoveryClient discoveryClient;
    
    @Autowired
    private MovieServerClientProxy movieServerClientProxy;

    @GetMapping("/")
    public String handleRequest(Model model) {

        List<Movie> result = movieServerClientProxy.getAllMovies();
        model.addAttribute("result", result);

        return "movie";
    }

}

和 application.yml 一样

server:
  port: 9000
spring:
  application:
    name: client-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://registiration-server:8761/eureka
  instance:
    prefer-ip-address: true

client-server:
  ribbon:
    listOfServers: client-server:9000,movie-server:8081

我想请求电影服务器应用程序,但出现这样的错误。我不知道为什么。你能帮我找出这些异常的原因吗?

zuul-api-gateway_1      | 2021-01-29 12:14:37.673  INFO 1 --- [nio-8762-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
zuul-api-gateway_1      | 2021-01-29 12:14:37.674  INFO 1 --- [nio-8762-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
zuul-api-gateway_1      | 2021-01-29 12:14:37.732  INFO 1 --- [nio-8762-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 57 ms
zuul-api-gateway_1      | 2021-01-29 12:14:37.832  INFO 1 --- [nio-8762-exec-1] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/movie-server/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
zuul-api-gateway_1      | 2021-01-29 12:14:37.832  INFO 1 --- [nio-8762-exec-1] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/client-server/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
client-server_1         | 2021-01-29 12:14:38.062 ERROR 1 --- [nio-9000-exec-4] 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: status 404 reading MovieServerClientProxy#getAllMovies(); content:
client-server_1         | {"timestamp":"2021-01-29T12:14:37.927+0000","status":404,"error":"Not Found","message":"No message available","path":"/list"}] with root cause
client-server_1         | 
client-server_1         | feign.FeignException: status 404 reading MovieServerClientProxy#getAllMovies(); content:
client-server_1         | {"timestamp":"2021-01-29T12:14:37.927+0000","status":404,"error":"Not Found","message":"No message available","path":"/list"}
client-server_1         |       at feign.FeignException.errorStatus(FeignException.java:60) ~[feign-core-9.7.0.jar!/:na]
client-server_1         |       at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:89) ~[feign-core-9.7.0.jar!/:na]
client-server_1         |       at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:143) ~[feign-core-9.7.0.jar!/:na]

电影服务器如下所示。

@RestController
@SpringBootApplication
public class MovieController {

    @GetMapping("/list")
    public List<Movie> getAllMovies() {

        List<Movie> movies = new ArrayList<>();

        movies.add(new Movie("Kelebek Etkisi", "2004", "7,7"));
        movies.add(new Movie("Zamanın Ötesinde", "2014", "7,5"));

        return movies;
    }
}

application.properties

server.port=8081
spring.application.name=movie-server
eureka.client.service-url.defaultZone=http://registiration-server:8761/eureka

【问题讨论】:

  • 只需仔细检查具有 MovieController 端点的应用程序的上下文路径。
  • 你是说这部分吗?我认为那部分给我带来了麻烦。 movieServerClients MovieController 调用得很好。问题是用丝带打电话。客户端服务器:功能区:listOfServers:客户端服务器:9000,电影服务器:8081

标签: spring-boot netflix-eureka netflix-zuul feign netflix-ribbon


【解决方案1】:

我在代理接口和 application.yml 中找到了我的解决方案。我确实使用了@GetMapping("movie-server/list") 而不是@GetMapping("/list")。

@FeignClient(name="zuul-api-gateway")
@RibbonClient(name="movie-server")
public interface MovieServerClientProxy {

    @GetMapping("movie-server/list")
    List<Movie> getAllMovies();
}

application.yml 必须如下所示。

server:
  port: 9000
spring:
  application:
    name: client-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://registiration-server:8761/eureka
  instance:
    prefer-ip-address: true

movie-server:
      ribbon:
        listOfServers: http://movie-server:8081

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 2021-09-25
    • 2021-01-01
    • 2020-03-18
    • 2017-08-23
    • 2021-05-28
    • 2019-02-07
    • 2019-09-04
    • 2018-08-19
    相关资源
    最近更新 更多