1

Hallo Ich versuche, einen Spring-Boot-Service von einem anderen Dienst durch Rest-Vorlage aufrufen. Ich bin mit Konsul als Service-Discovery- jetzt Dienste1 -> Rest -> Service2Service zu Service-Kommunikation über Resttemplate mit Spring-Cloud (Oauth 2)

Ich möchte nicht diese Kommunikation durch Zuul service1 sollte service2 direkt anrufen können, aber Band sollte den zweiten Service durch Konsul entdecken. Alles funktioniert gut, aber wenn ich versuche, service1 zu rufen Service2 i bei service1 unter Ausnahme bin immer

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:41) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:187) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at com.sun.proxy.$Proxy84.getAccessToken(Unknown Source) ~[na:na] 
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:125) ~[spring-security-oauth2-2.0.9.RELEASE.jar:na] 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.web.client.RestTemplate$$FastClassBySpringCGLIB$$aa4e9ed0.invoke(<generated>) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.cloud.netflix.metrics.RestTemplateUrlTemplateCapturingAspect.captureUrlTemplate(RestTemplateUrlTemplateCapturingAspect.java:33) ~[spring-cloud-netflix-core-1.1.1.BUILD-SNAPSHOT.jar:1.1.1.BUILD-SNAPSHOT] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45] 
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45] 
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:620) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:609) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.security.oauth2.client.OAuth2RestTemplate$$EnhancerBySpringCGLIB$$edba1d43.getForObject(<generated>) ~[spring-security-oauth2-2.0.9.RELEASE.jar:na] 
    at hello.PCNApplication.available(PCNApplication.java:47) ~[classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45] 
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45] 
    at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:116) ~[hystrix-javanica-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.executeWithArgs(MethodExecutionAction.java:93) ~[hystrix-javanica-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:78) ~[hystrix-javanica-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.contrib.javanica.command.GenericCommand$1.execute(GenericCommand.java:47) ~[hystrix-javanica-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommand.process(AbstractHystrixCommand.java:146) ~[hystrix-javanica-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.contrib.javanica.command.GenericCommand.run(GenericCommand.java:44) ~[hystrix-javanica-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:293) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:289) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.Observable.unsafeSubscribe(Observable.java:8460) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.Observable.unsafeSubscribe(Observable.java:8460) ~[rxjava-1.1.5.jar:1.1.5] 
    at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) ~[rxjava-1.1.5.jar:1.1.5] 
    at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69) ~[hystrix-core-1.5.3.jar:1.5.3] 
    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) ~[rxjava-1.1.5.jar:1.1.5] 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_45] 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_45] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_45] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_45] 
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45] 

Unten ist mein Code

@RestController 
@SpringBootApplication 
@EnableDiscoveryClient 
@RibbonClient(name = "service1", configuration = SayHelloConfiguration.class) 
@EnableResourceServer 
public class Service1Application extends ResourceServerConfigurerAdapter{ 

    @Autowired 
    private RestTemplate restTemplate; 

    @LoadBalanced 
    @Bean 
    public RestTemplate restTemplate() { 
     return new RestTemplate(); 
    } 
    @RequestMapping(value = "/someHome") 
    @HystrixCommand 
    public String available() { 

    String response=restTemplate.getForObject("https://Service2/service2Home", String.class); 
     return " Welcome to service1 Notification home Page :-"+response; 

    } 


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

@Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
     .authorizeRequests().antMatchers("/health").permitAll().and() 
     .authorizeRequests().antMatchers("/login").anonymous().and() 
     .authorizeRequests().anyRequest().authenticated().and() 
     .httpBasic().and() 
     .addFilterAfter(new OAuth2ClientContextFilter(), SecurityContextPersistenceFilter.class); 
    } 
} 

HINWEIS: alles ist auf https

+0

Wir fanden, dass dies wegen der Hystrix. Kann mir jemand helfen, dies mit Hystrix zu lösen ???? –

+0

hast du das irgendwie gelöst? – bilak

+0

Nein, wir verwenden Oauth nicht auf diese Weise, aber Sie können es mit dem Feign-Client versuchen (anstatt RestTemplate zu verwenden) konnten wir dasselbe erreichen ... –

Antwort

0

Wenn Sie Ihren HystricCommand so konfigurieren, dass er im selben Thread ausgeführt wird, kann er die für OAuth2RestTemplate erforderlichen OAuth2-Authentifizierungsdetails für die Sitzung sehen:

@HystrixCommand(commandProperties={ 
    @HystrixProperty(name = execution.isolation.strategy, value = "SEMAPHORE") 
}) 

Vorsicht, Sie verlieren Timeout Fähigkeiten von HystrixCommands (siehe Anmerkung unter https://github.com/Netflix/Hystrix/wiki/How-it-Works#semaphores).

Verwandte Themen