微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ZUUL API 网关 RIBBONCLIENT 请求错误

如何解决ZUUL API 网关 RIBBONCLIENT 请求错误

我正在尝试使用 zuul api、eureka 服务器、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","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",5"));

        return movies;
    }
}

application.properties

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

enter image description here

enter image description here

解决方法

我在我的代理界面和 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。