java - Spring Cloud Ribbon 抛出异常,名为“没有可用于 serverurl 的实例”

标签 java spring spring-boot spring-cloud netflix-ribbon

我正在使用带有 Spring 丝带的 Spring Boot 。我完成了功能区的所有配置。但是,当我向其余 Controller 发送请求时,它会抛出一个名为 没有可用于 serverurl 的实例 的异常。我该如何解决这个问题?

这些是我的配置

应用程序.yml

port: 8888

serverurl:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:8081,localhost:8082,localhost:8083
    ServerListRefreshInterval: 15000

Spring Boot主类

@SpringBootApplication
@RibbonClient(name = "serverurl", configuration = RibbonCongisuration.class)
public class Server {

    @LoadBalanced
    @Bean
    RestTemplate restTemplate(){
        return new RestTemplate();
    }


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

休息 Controller

@RestController
@RequestMapping(value = "api/v1/clients")
public class ClientController {



    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/{ID}")
    public ClientDTO findByID(@PathVariable("ID") String clientID){
       return  restTemplate.getForEntity("http://serverurl/api/v1/clients/"+clientID,ClientDTO.class).getBody();

    }
}

网址

http://localhost:8888/api/v1/clients/1234

最佳答案

1) 确保http://localhost:8081/api/v1/clients/1234 (8081/8082/8083) 响应。

2) 添加RibbonConfiguration 文件例如:

@Configuration
public class RibbonConfiguration{
@Bean
public IRule ribbonRule() {
    return new BestAvailableRule();
}

@Bean
public IPing ribbonPing() {
    return new PingUrl();
}

}

3)确保你有这种pom依赖(例如):

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

还有

<dependencyManagement>
 <dependencies>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.SR2</version>
        <type>pom</type>
        <scope>import</scope>
     </dependency>
 </dependencies>

关于java - Spring Cloud Ribbon 抛出异常,名为“没有可用于 serverurl 的实例”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55873476/

相关文章:

java - 在 spring 集成中将多个有效负载嵌入到 "Message"对象中的最佳方法是什么?

java - Spring 重试 : method annotated with @Recover not being called

java.lang.ClassCastException : java. lang.Float 无法转换为 java.lang.String

java - 我如何覆盖 80 的 checkstyle 注释行宽度检查?

java - Spring Java 配置。配置文件中PropertiesFactoryBean的使用

java - 如何使用应用程序属性和 pom 文件共享/传递变量

多模块项目中的Spring Boot i18n

java - 使用 Volley 预填充房间数据库

java - 获取自定义适配器android的textview

java - request.setAttribute() 和 model.addAttribute 之间有什么区别?