2014-10-09 26 views
7

Yaylı bulut kullanarak bir yaylı önyükleme uygulaması oluşturdum ve RestTemplate uygulamasını istemci uygulamda kullanmak istiyorum (ki bu da bir microservice). Bütünleştirme testi için mockMvc kullanmaya devam edebilirim. Ben çağırıyorum hizmet içinde müşteri microservice ve eureka istemci ile varsayılan şerit/eureka/hystrix istemci kurulumunu kullanıyorum. Bu çalışmaktadır (serviceTDS, restTemplate içinde bir hizmet son noktasını tanımlayan şey olduğunu anladım). Benim sorunum, restTemplate okuma ve bağlantı zaman aşımını 300ms'lik bir varsayılan gibi görünenlerden değiştiremiyorum. çağrı üzerineYaylı şerit/eureka/hystrix restTemplate kullanarak bağlanamaz/zaman aşımlarını ayarlayamıyor

Detayları:

spring: 
    cloud: 
    client: 
     serviceIds: 
     - someservice 

someservice: 
    ribbon: 
    #listOfServers: localhost:7080 
    #NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList 

    # the eureka vipAddress of the target service (Disabled) 
    DeploymentContextBasedVipAddresses: someservice 

    # Interval to refresh the server list from the source 
    ServerListRefreshInterval: 1000 

    # Connect timeout used by Apache HttpClient.. apparently not by restTemplate 
    ConnectTimeout: 30000 

    # Read timeout used by Apache HttpClient.. apparently not by restTemplate 
    ReadTimeout: 30000 

eureka: 
    client: 
    #Region where eureka is deployed -For AWS specify one of the AWS regions, for other datacenters specify a arbitrary string 
    #indicating the region.This is normally specified as a -D option (eg) -Deureka.region=us-east-1 
    region: default 

    #For eureka clients running in eureka server, it needs to connect to servers in other zones 
    preferSameZone: false 

    us-east-1: 
     availabilityZones: default 

    instance: 
    #Virtual host name by which the clients identifies this service 
    virtualHostName: ${spring.application.name} 
    appGroupName: ericGroup 


# disable Ribbon's cicruit breaker and rely soley on Hystrix. 
# this helps to avoid confusion. 
# see https://github.com/Netflix/ribbon/issues/15 
niws: 
    loadbalancer: 
    availabilityFilteringRule: 
     filterCircuitTripped: false 

Herkes ben restTemplate varsayılan zaman aşımı değiştirmek için bu kadar set gerekenleri özellikleri biliyorum: içeren application.properties ile

`@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
@EnableConfigurationProperties 
@EnableHystrix 
@EnableEurekaClient 
public class Application { ... public static void main(String[] args) {} ... } 

@Component 
class EricComponentToDoHystrix { // apparently this has to be a component for hystrix to work btw 
    @Autowired 
    RestTemplate restTemplate; 
    .. 
    @HystrixCommand(fallbackMethod="defaultRestTemplateCall") 
    public void doRestTemplateCall() 
     ResponseEntity<String> result = restTemplate.getForEntity("http://someservice/doSomething", String.class); // actually make a call 
    .. 
    } 
}` 

? Dokümantasyon bu konuda çok açık ve kod yakın bir zamanda, restTemplate'in şerit/eureka yay önyükleme varsayılanlarına karşı kullanılmasına izin verilmiş gibi görünüyor. Belki de bu henüz inşa edilmemiştir.

cevap

6

RestTemplate sen enjekte edilir tamamen sizin için URI fiziksel host (https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.java bakınız) seçen bir RibbonInterceptor haricinde vanilya olduğunu. Zaman aşımları ve diğer özellikler ClientHttpRequest aracılığıyla RestTemplate'da kontrol edilir. Muhtemelen RibbonInterceptor'u kendi RestTemplate içine enjekte etmeli ve zaman aşımlarını yapmak için bir ClientHttpRequestFactory kurmalısınız, örn.

@Component 
class EricComponentToDoHystrix { 
    private RestTemplate restTemplate; 
    @Autowired 
    public EricComponentToDoHystrix(RibbonInterceptor interceptor) { 
     restTemplate = new RestTemplate(); 
     restTemplate.setInterceptors(Arrays.asList(interceptor)); 
     restTemplate.setRequestFactory(...); 
    } 
} 
+0

İkinize de çok hızlı cevap verdiğiniz için teşekkür ederiz! Hem bu hem de @ spencergibb'in cevabı bana yardımcı oldu. Ribonun konfigürasyonunun RestTemplate ile hiçbir ilgisi olmadığını ve RibbonInterceptor dışındaki vanilin de test edilmesine yardımcı olacağını bilmek. Kendimi yukarıda listelendiği gibi ayarlayacağım. btw, burada oyunda başka bir zaman aşımı var ve bu da hystrix'in execution.isolation.thread.timeoutInMilliseconds. Yani şimdi oyunda 3 zaman aşımı var, Şeritler, RestTemplate ve Hystrix'ler. – RubesMN

+0

@DaveSyer "RibbonInterceptor" RELEASE sürümünde kaybolmuş gibi görünüyor. Beni şimdi RestTemplate'i nasıl yapılandıracağınıza yönlendirebilir misiniz? –

+0

'LoadBalancerInterceptor' ı deneyin. –

1

Yorum yapamadığım için cevap vereceğim. RestTemplate tümleştirmesi yalnızca RestClient veya NFHttpClient değil Ribbon LoadBalancer kullanır.

Artık spring.cloud.client.serviceIds, btw dosyasına ihtiyacınız yok. Dokümanlardaysa, onu kaldıracağım.

+0

serviceIds neden artık gerekli değil? öneri ne? bulduğum örnekler serviceIDs kullanıyor. İlgili belgelere işaret edebilir misiniz? Ayrıca, mümkün olduğunda lütfen bu yazıyla ilgili görüşlerinizi bildirin. http: // stackoverflow.com/sorular/31901054/yay bulut how-to-get-faydaları-of-the yeniden deneme-yük dengeleme-ve-devre-kesici-f –

+0

'serviceIds' artık ihtiyaç erken bir uygulama oldu. Hangi örnekler hala var? Https://github.com/spring-cloud-samples/customers-stores adresine bakın. Bildiğim kadarıyla, bu SO sorusu onu referans gösteren tek şey. Ne yapmaya çalışıyorsun? – spencergibb

+0

Bunlar, varsayalım servisini kullanır. Josh Long'un dersleri. https://github.com/joshlong/service-registration-and-discovery ve https://github.com/joshlong/spring-doge-microservice –