Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to use custom RestTemplate in config-client #501

Closed
ankit8051 opened this issue Sep 9, 2016 · 5 comments
Closed

Document how to use custom RestTemplate in config-client #501

ankit8051 opened this issue Sep 9, 2016 · 5 comments
Assignees
Milestone

Comments

@ankit8051
Copy link

Is there any way to use custom RestTemplate object that is used in config-client?

The reason why I need this is that we are working in distributed system. So if we would like to connect some https application then we are creating restTemplate with ClientHttpRequestFactory. Within ClientHttpRequestFactory we are setting sslConnectionSocketFactory property in such a way that we don't need to do deal with installing trust store etc.

In normal applicatin it works fine, but Now we are moving config-server to https, So to connect with it config-client use RestTemplate object and that is generated within config-client.

I want a some way that config-client use restTemplate object that is generated by me.

@spencergibb
Copy link
Member

There is some discussion in #177

@spencergibb
Copy link
Member

Specifically #177 (comment)

@spencergibb spencergibb changed the title Is there any way to use custom RestTemplate object that is used in config-client? Document how to use custom RestTemplate in config-client Sep 9, 2016
@yspatel140
Copy link

I am following same steps as mentioned in #177, but I am getting below error.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configSourceLocator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [packagename.ConfigSourceLocator$$EnhancerBySpringCGLIB$$108293a2]: No default constructor found; nested exception is java.lang.NoSuchMethodException: packagename.ConfigSourceLocator$$EnhancerBySpringCGLIB$$108293a2.()

It seems that it is looking for default constructor. Is there any way to force to execute args constructor by skipping default one?

Below is my code

@Order(0)
@Configuration
public class ConfigSourceLocator extends ConfigServicePropertySourceLocator {

    @Autowired
    private Environment environment;

    @Autowired
    public ConfigSourceLocator(ConfigClientProperties configClientProperties) {
        super(configClientProperties);
    }

    @Bean
    public ConfigClientProperties configClientProperties() {
        ConfigClientProperties client = new ConfigClientProperties(this.environment);
        client.setEnabled(false);
        return client;
    }

    @Primary
    @Bean
    public ConfigServicePropertySourceLocator configServicePropertySourceLocator() {
        ConfigClientProperties clientProperties = configClientProperties();
        ConfigServicePropertySourceLocator sourceLocator = new ConfigServicePropertySourceLocator(clientProperties);
        sourceLocator.setRestTemplate(getSecureRestTemplate(clientProperties));
        return sourceLocator;
    }

    private RestTemplate getSecureRestTemplate(ConfigClientProperties client) {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setReadTimeout((60 * 1000 * 3) + 5000); // TODO 3m5s, make configurable?
        RestTemplate template = new RestTemplate(requestFactory);
        return template;
    }
}

@yspatel140
Copy link

After removing @order(0) and @configuration annotation Its work for me.

@yspatel140
Copy link

I am able to customize restTemplate object by following step mentioned in #177. Here, three beans are created for ConfigServicePropertySourceLocator and therefore three calls are made for connecting config-server(Highlighted below). I have mentioned @primary in my code so its give priorities to it and its work fine. So my only concern is that Is this the best way to customize restTemplate or I miss something?

2016-09-29 19:27:26.599 INFO 14156 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: https://localhost:9443
2016-09-29 19:28:50.403 INFO 14156 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=appname, profiles=[dev], label=null, version=219511
2016-09-29 19:28:50.403 INFO 14156 --- [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='aaa'], MapPropertySource [name='aaa'], MapPropertySource [name='aaa']]]
2016-09-29 19:28:50.404 INFO 14156 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
2016-09-29 19:28:52.501 WARN 14156 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/appname/dev": Connect to localhost:8888 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8888 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
2016-09-29 19:28:52.516 INFO 14156 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: https://localhost:9443
2016-09-29 19:29:09.476 INFO 14156 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=appname, profiles=[dev], label=null, version=219511
2016-09-29 19:29:09.477 INFO 14156 --- [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='aaa'], MapPropertySource [name='aaa'], MapPropertySource [name='aaa']]]
2016-09-29 19:29:14.000 WARN 14156 --- [ restartedMain] o.s.c.a.ConfigurationClassPostProcessor : Cannot enhance @configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2016-09-29 19:29:28.537 WARN 14156 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2016-09-29 19:29:35.489 WARN 14156 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.

ryanjbaxter added a commit to ryanjbaxter/spring-cloud-config that referenced this issue Oct 11, 2016
@ryanjbaxter ryanjbaxter self-assigned this Oct 11, 2016
spencergibb pushed a commit that referenced this issue Oct 13, 2016
@spencergibb spencergibb added this to the 1.2.1.RELEASE milestone Oct 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants