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

Integrating with Spring Cloud Config Server #501

Closed
thiagolocatelli opened this issue Aug 17, 2018 · 45 comments
Closed

Integrating with Spring Cloud Config Server #501

thiagolocatelli opened this issue Aug 17, 2018 · 45 comments
Assignees
Labels
help wanted Extra attention is needed status: pr submitted A pull request has been submitted for the issue
Milestone

Comments

@thiagolocatelli
Copy link
Contributor

thiagolocatelli commented Aug 17, 2018

I want to migrate some of the applications I have written with Spring Boot but I would like to keep using the config servers I have. Looking at the documentation, I found this: "Alternatively one can register a PropertySourceLoader by creating a META-INF/services/io.micronaut.context.env.PropertySourceLoader containing a reference to the class name of the PropertySourceLoader".

I am creating my own spring config server client for Micronaut to read the properties from the config server, should I just go with my own implementation of PropertySourceLoader or should I follow the same approach used for Eureka and Consul in the discovery-client module?

@graemerocher
Copy link
Contributor

The latter. You should implement the ConfigurationClient interface

@thiagolocatelli
Copy link
Contributor Author

I will look into that. Thank you @graemerocher

@thiagolocatelli
Copy link
Contributor Author

thiagolocatelli commented Aug 21, 2018

Started working on this: https://github.com/thiagolocatelli/micronaut-spring-client

Still can't figure out why the method getPropertySources from SpringCloudConfigurationClient is not being invoked. I may be missing something. Decided to create the code in another repo and once it's stable and working 100% I will merge it into the oficial repo.

@thiagolocatelli
Copy link
Contributor Author

with the help form @jameskleeh I identified the discovery-client library was missing. After adding it to the project, my SpringCloudConfigurationClient started being invoked.

@thiagolocatelli
Copy link
Contributor Author

thiagolocatelli commented Aug 21, 2018

So the client for Spring Cloud config is working pretty well. I have just one question before I start writing the unit tests. The PropertySource.of method takes a priority. Which priority is loaded first: lowest or highest?

Let's say I have a property "application.location" with priorities 10 and 20, which one will have the final value in the application context?

@jameskleeh
Copy link
Contributor

@thiagolocatelli I believe the higher order will override config values with a lower order

@thiagolocatelli
Copy link
Contributor Author

@jameskleeh sounds good James. That's how I implemented. Thank you.

@thiagolocatelli
Copy link
Contributor Author

Progress: discovery client for spring cloud config server is working properly

13:49:12.321 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [dev, docker]
13:49:13.493 [main] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Config Server endpoint: http://127.0.0.1:8888/configserver
13:49:13.493 [main] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Application name: spring-config-sample, application profiles: dev,docker
13:49:14.211 [nioEventLoopGroup-1-2] DEBUG i.m.http.client.DefaultHttpClient - Sending HTTP Request: GET /configserver/spring-config-sample/dev%2Cdocker
13:49:14.211 [nioEventLoopGroup-1-2] DEBUG i.m.http.client.DefaultHttpClient - Chosen Server: 127.0.0.1(8888)
13:49:15.423 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application-docker.properties
13:49:15.423 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application-dev.properties
13:49:15.428 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/spring-config-sample.properties
13:49:15.433 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application.properties
13:49:15.465 [main] INFO  i.m.d.c.c.DistributedPropertySourceLocator - Resolved 3 configuration sources from client: compositeConfigurationClient(spring-cloud-config-client)
13:49:15.621 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 3289ms. Server Running: http://localhost:8080

@jameskleeh
Copy link
Contributor

jameskleeh commented Aug 23, 2018

@thiagolocatelli How are you ordering the property sources when both application and app specific config is specified for the environment?

We should probably standardize and document what order the property sources should return in

I think how it currently works for consul is:

application
foo
application,dev
foo,dev

@thiagolocatelli
Copy link
Contributor Author

I am currently ordering based on the response sent by the config server, which seems to the correct order to be used. The first one gets the highest priority and the subsequent ones get lowers priority.

@jameskleeh
Copy link
Contributor

@thiagolocatelli In your logs there is no example with the application name and environment

@thiagolocatelli
Copy link
Contributor Author

there is, in my example I am using -Dmicronaut.environments=dev,docker

13:49:13.493 [main] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Application name: spring-config-sample, application profiles: dev,docker
13:49:15.423 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application-docker.properties
13:49:15.423 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application-dev.properties
13:49:15.428 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/spring-config-sample.properties

@jameskleeh
Copy link
Contributor

@thiagolocatelli There isn't a property source for spring-config-sample specific to an environment

@thiagolocatelli
Copy link
Contributor Author

if I had included a file spring-config-sample-{env}.properties into my config server it would have been picked up by the client. My config server only contains application.properties, application-{env}.properties and spring-config-sample.properties.

@jameskleeh
Copy link
Contributor

@thiagolocatelli Great! Have you verified that if that file were there it would be returned in the correct order?

@thiagolocatelli
Copy link
Contributor Author

@jameskleeh working on that right now! will include the spring-config-sample-{env}.properties files there and see what comes up.

@thiagolocatelli
Copy link
Contributor Author

thiagolocatelli commented Aug 23, 2018

@jameskleeh so I have included the following files to my config server:

spring-sample-config.properties
spring-sample-config-dev.properties
spring-sample-config-docker.properties

the response back from the config server was:

15:47:05.218 [main] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Application name: spring-config-sample, application profiles: dev,docker
15:47:07.782 [nioEventLoopGroup-1-2] DEBUG i.m.http.client.DefaultHttpClient - Sending HTTP Request: GET /configserver/spring-config-sample/dev%2Cdocker
15:47:11.051 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/spring-config-sample-docker.properties
15:47:11.051 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application-docker.properties
15:47:11.052 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/spring-config-sample-dev.properties
15:47:11.052 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application-dev.properties
15:47:11.052 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/spring-config-sample.properties
15:47:11.052 [nioEventLoopGroup-1-2] INFO  i.m.d.s.c.SpringCloudConfigConfigurationClient - Property source entry: http://*****/application.properties  

so it seems to me that spring property source ordering is as follow, considering app name is 'foo' and environments are 'dev' and 'docker':

foo-docker.properties
application-docker.properties
foo-dev.properties
application-dev.properties
foo.properties
application.properties

I am taking the environments names from environment.getActiveNames(), which returns a java.util.Set, so no matter the order the developer wants, it can be changed due to the underlying implementation of Set, which is sorted by default (maybe this could be an enhancement and let the order provided in the micronaut.environment property be honored by the Micronaut).

@jameskleeh
Copy link
Contributor

@thiagolocatelli Seems like it is returning them in the correct order. I think it would be wise to ensure that the order is correct locally to be future proof of changes in spring cloud.

It makes sense to me that the environment order should be a list and the order should be respected

@thiagolocatelli
Copy link
Contributor Author

@jameskleeh I will open a separate issue on that and will do my best to send a PR with a fix. ok?

@graemerocher
Copy link
Contributor

@thiagolocatelli PRs welcome

@thiagolocatelli
Copy link
Contributor Author

@graemerocher PR will be sent soon. First I need to setup micronaut-core on my local machine and move the client code to micronaut-core, right now its a library inside my project, its not inside micronaut yet. Second, I need to write unit tests for it and then I will be ready for the PR.

Second phase would be include Vault as part of the process too, but I guess spring cloud config is a good beginning. Sorry for the delay.

@graemerocher
Copy link
Contributor

No worries, thanks for contributing!

@thiagolocatelli
Copy link
Contributor Author

@graemerocher I have made the client available at https://github.com/thiagolocatelli/micronaut-spring-client

I am still trying to figure out how I can use the SPRING_CLOUD_CONFIG_URI environment variable. The default scenario should be: first use the environment variable, if not available, then use spring.cloud.config.uri property, if that is also not available, default to http://localhost:8888 (default config server uri when deployed locally). Once I figure how to achieve this, I will merge that code into micronaut-core and send the PR.

If you have any suggestion on how to achieve that, let me know.

@jameskleeh
Copy link
Contributor

@thiagolocatelli A PR was just merged yesterday to allow backticks.

${SPRING_CLOUD_CONFIG_URI:spring.cloud.config.uri:`http://localhost:8888`}

@thiagolocatelli
Copy link
Contributor Author

@jameskleeh per our discussion over Gitter, we found out that Micronaut wasnt evaluating the second element in the expression. It was returning a constant instead of trying to extract the value from the application context. ${SPRING_CLOUD_CONFIG_URI:spring.cloud.config.uri} returns 'spring.cloud.config.uri' instead of the value for it. I had opened an issue #516 but it was closed a couple of days ago. Even though I have spring.cloud.config.uri in the bootstrap.yml, micronaut is not evaluating it.

In your example ${SPRING_CLOUD_CONFIG_URI:spring.cloud.config.uri:`http://localhost:8888`}, it will never return localhost:8888 because the constant 'spring.cloud.config.uri' will be returned if env variable is not there.

@jameskleeh
Copy link
Contributor

@thiagolocatelli That isn't true, only the last value is treated as a constant

@thiagolocatelli
Copy link
Contributor Author

@jameskleeh ok, Thanks. I will wait for the next release with the PR sent to allow backticks to keep working on the config client.

@datamaskin
Copy link

We have a SCCS running in house using ${GIT_URL} defined in the shell which SCCS seems to recognize from application.yml or a default. Would really like to see a MN profile for creating SCCS clients. Thanks.

@graemerocher graemerocher added the help wanted Extra attention is needed label Oct 9, 2018
@MichelSchudel
Copy link
Contributor

@graemerocher I'd like to take a crack at this and help you with this one, since we would like to have this feature so we can embed new MN services in our Spring Cloud ecosystem. The ability to use SCC for MN configuration would be very helpful to us. So, what's still open exactly, the ability to use SCC (a Client, I saw @thiagolocatelli did a lot of work already), or is creating a profile the only work left?

@graemerocher
Copy link
Contributor

We have made no progress on this issue. I don’t know what the state of @thiagolocatalli’s work is. Any help would be appreciated!

@thiagolocatelli
Copy link
Contributor Author

@MichelSchudel Hey Michel, I have completed the code, its available in this repo: https://github.com/thiagolocatelli/micronaut-spring-client

I just havent had the chance to merge my changes back into the official repo, you can give it a try. It has been working for us.

@MichelSchudel
Copy link
Contributor

MichelSchudel commented Nov 10, 2018 via email

@mdkhwajams
Copy link

Great work! Thanks for this integration with spring cloud config server looking forward to utilize it.

@MichelSchudel
Copy link
Contributor

MichelSchudel commented Nov 16, 2018 via email

@thiagolocatelli
Copy link
Contributor Author

@MichelSchudel Did you get any lucky integrating with the micronaut-core project?

@MichelSchudel
Copy link
Contributor

MichelSchudel commented Nov 28, 2018 via email

@thiagolocatelli
Copy link
Contributor Author

Awesome! Thank you

@graemerocher
Copy link
Contributor

@MichelSchudel maybe you want to send the PR anyway and we can clean it up if necessary

@MichelSchudel
Copy link
Contributor

@graemerocher is there any way I can send you a private message? Twitter would be preferrable (follow me)

@thiagolocatelli
Copy link
Contributor Author

@MichelSchudel were you able to send the PR? If not, I will work on sending the PR by Friday.

@MichelSchudel
Copy link
Contributor

Hi @thiagolocatelli,
I have updated your client project (through a fork) to work with the recent version of micronaut master and added an integration test. I sent you a pull request, plz review and merge if you like.

Due to a tragic familiy event I have been unable to cast your code into the micronaut-core java and test package structures. So, you still have to do that. Sorry but I have no time for that right now. So if you do, please use my changes, and make a pull request, by all means.

@thiagolocatelli
Copy link
Contributor Author

@MichelSchudel sorry to hear about the family event. I will take care now and send @graemerocher a PR into their repo. I will be adding the code to their discovery module.

Thank you for the changes you made.

@MichelSchudel
Copy link
Contributor

MichelSchudel commented Dec 26, 2018 via email

@thiagolocatelli
Copy link
Contributor Author

@graemerocher PR sent

@graemerocher graemerocher added the status: pr submitted A pull request has been submitted for the issue label Jan 2, 2019
@graemerocher graemerocher added this to the 1.1.0 milestone Jan 3, 2019
@MichelSchudel
Copy link
Contributor

MichelSchudel commented Jan 3, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed status: pr submitted A pull request has been submitted for the issue
Projects
None yet
Development

No branches or pull requests

6 participants