3

私は使っている

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-netflix</artifactId>
  <version>1.2.3.RELEASE</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>

私の主なクラス:

@SpringBootApplication
//@Configuration
@ComponentScan(basePackages = "com.mypackage")
@EnableAutoConfiguration
@EnableEurekaClient
@EnableSwagger2
public class App 
{
  public static void main( String[] args )
  {

    SpringApplication.run(App.class, args);
  }

  @LoadBalanced
  @Bean(name="template")
  RestTemplate restTemplate() {
    return new RestTemplate();
  }
}

私のサービス呼び出し:

@Autowired
private RestTemplate template;

ResponseEntity<String> avs = template.exchange("http://localhost:7075/xyz/json/authenticate",HttpMethod.POST ,request,String.class); 

次の例外をスローしています

java.lang.IllegalStateException: No instances available for localhost
    at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90)
    at org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor$1.doWithRetry(RetryLoadBalancerInterceptor.java:60)
    at org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor$1.doWithRetry(RetryLoadBalancerInterceptor.java:48)
    at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:276)
    at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:157)
4

2 に答える 2

6

を使用する場合@LoadBalanced RestTemplate、ホスト名は実際のホスト名ではなく serviceId である必要があります。あなたの場合、eurekaレコードを見つけようとしていて、見つけるlocalhostことができません。複数のオブジェクトを使用する方法については、ドキュメントを参照してください。RestTemplate

@Configuration
public class MyConfiguration {

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

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

public class MyClass {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    @LoadBalanced
    private RestTemplate loadBalanced;

    public String doOtherStuff() {
        return loadBalanced.getForObject("http://stores/stores", String.class);
    }

    public String doStuff() {
        return restTemplate.getForObject("http://example.com", String.class);
    }
}
于 2017-01-10T19:13:39.083 に答える
1

私が読んだところによると、この Netflix クラウドを使用しているときに RestTemplate を Autowire しようとすると、何らかの問題が発生します。しかし、私は回避策を見つけました。最初に新しい @Component クラスを宣言し、その中で RestTemplate を返すメソッドを作成します。

@Component
public class RestTemplateComponentFix{

 @Autowired
 SomeConfigurationYouNeed someConfiguration;

 @LoadBalanced
 public RestTemplate getRestTemplate() {
       // TODO set up your restTemplate
        rt.setRequestFactory( new HttpComponentsClientHttpRequestFactory() );
        return rt;
    }

}

その後、クラスでrestTemplateComponentFixを自動配線し、残りのテンプレートが必要なときにrestTemplate()メソッドを呼び出します。このようなもの:

@Service
public class someClass{

    @Autowired
    RestTemplateComponentFix restTemplateComponentFix;

    public void methodUsingRestTemplate(){
        // Some code...
        RestTemplate rt = restTemplateComponentFix.getRestTemplate();
        // Some code...
    }
}

クールな部分は、次のようなコードを使用して、このコードを簡単に単体テストできることです。

RestTemplate rt = Mockito.mock(RestTemplate.class) 
when(restTemplateComponentFix.getRestTemplate()).thenReturn(rt);
when(rt.someMethod()).thenReturn(something);
于 2017-11-10T10:23:16.333 に答える