1

我正在尝试调用负载均衡器来访问服务实例,以便在我的配置类中构建 OAuth2RestTemplate 访问令牌 uri,但由于某种原因,当实例化 bean 时我不断收到异常。有没有人遇到过这个问题?或者也许有一些见识?

工厂方法“restTemplate”抛出异常;嵌套异常是 java.lang.IllegalStateException: Unable to locate ILoadBalancer for service: service

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [com/apop/services/config/rest/RestConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.client.OAuth2RestTemplate]: Factory method 'serviceRestTemplate' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate ILoadBalancer for service: service

这里还有我的服务和配置类:

@Service
public class ServiceIntegration implements Service, Serializable {


@Resource(name = "serviceRestTemplate")
OAuth2RestTemplate serviceRestTemplate;

@Autowired
private LoadBalancerClient balancerClient;

@Value("${service}")
private String service;

private ResponseExceptionHelper responseExceptionHelper = new ResponseExceptionHelper();

public ResponseExceptionHelper getResponseExceptionHelper() {
    return responseExceptionHelper;
}

public void setResponseExceptionHelper(
        ResponseExceptionHelper responseExceptionHelper) {
    this.responseExceptionHelper = responseExceptionHelper;
}

/**
 * @param payload
 * @param path
 * @param method
 * @return
 * @throws ResponseException
 */
private String execute(OAuth2RestTemplate restTemplate, String endPoint, String payload, HttpMethod method)
        throws ResponseException {
    HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> requestEntity = new HttpEntity<String>(payload, headers);

    String response = null;
    try {

        ServiceInstance serviceInstance = balancerClient.choose(service);
        String uri = String.format("http://%s:%s/%s", serviceInstance.getHost(), serviceInstance.getPort(), service) + endPoint;
        ResponseEntity<String> responseEntity = restTemplate.exchange(uri, method, requestEntity, String.class);
        if (responseEntity != null) {
            response = responseEntity.getBody();
        }
    } catch (Exception e) {
        logger.error(error, e);
        throw e;
    }

    logger.info(response);
    return response;
}

}

配置:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class RestConfiguration {


@Value("${service}")
private String service;


@Value("${token.service.endpoint}")
private String tokenServiceEndpoint;


@Value("${clientId}")
private String clientId;

@Value("${clientSecret}")
private String clientSecret;

@Value("${grant}")
private String grant;

@Autowired
private LoadBalancerClient balancerClient;


@Bean
public OAuth2RestTemplate serviceRestTemplate() {
    ClientCredentialsResourceDetails resources = getClientDetails();
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resources, new DefaultOAuth2ClientContext());
    return restTemplate;
}


public ClientCredentialsResourceDetails getClientDetails() {
    ServiceInstance serviceInstance = balancerClient.choose(service);
    String uri = String.format("http://%s:%s/%s", serviceInstance.getHost(), serviceInstance.getPort(), legacyService) + tokenServiceEndpoint;

    ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
    resource.setAccessTokenUri(uri);
    resource.setClientSecret(clientId);
    resource.setClientId(clientSecret);
    resource.setGrantType(grant);
    return resource;
}

}
4

1 回答 1

0

您正在尝试@Bean在服务目录可用之前(非常早)在定义中进行服务发现。RibbonInterceptor在其他正常情况下使用 a 会更好OAuth2RestTemplate(例如在RibbonAutoConfiguration中)。

于 2015-01-22T09:29:51.227 回答