Spring Cloud Ribbon抛出异常,称没有适用于serverurl的实例

时间:2019-04-26 18:53:12

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

我使用的是带有弹簧盖条的弹簧靴。我完成了功能区的所有配置。但是,当我向rest控制器发送请求时,它会引发一个名为没有可用于serverurl的实例的异常。我该如何解决?

这些是我的配置

application.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);
    }
}

其他控制器

@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();

    }
}

URL

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

1 个答案:

答案 0 :(得分:0)

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>