├── .gitignore ├── .gitmodules ├── .travis.yml ├── configuration-basics ├── pom.xml └── src │ └── main │ ├── java │ ├── boot │ │ └── Application.java │ ├── classic │ │ └── Application.java │ ├── env │ │ └── Application.java │ └── profiles │ │ └── Application.java │ └── resources │ ├── application.yml │ ├── classic.xml │ ├── some-prod.properties │ └── some.properties ├── configuration-client ├── manifest.yml ├── pom.xml └── src │ └── main │ ├── java │ └── demo │ │ ├── Application.java │ │ ├── ProjectNameRestController.java │ │ └── RefreshCounter.java │ └── resources │ └── bootstrap.yml ├── configuration-it ├── pom.xml └── src │ └── integration-test │ └── java │ └── configuration │ └── ConfigurationIT.java ├── configuration-service ├── manifest.yml ├── pom.xml └── src │ └── main │ ├── java │ └── demo │ │ └── Application.java │ └── resources │ ├── application.properties │ └── bootstrap.yml └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .#* 3 | *# 4 | *.sw* 5 | _site/ 6 | .factorypath 7 | .gradletasknamecache 8 | .DS_Store 9 | /application.yml 10 | /application.properties 11 | asciidoctor.css 12 | atlassian-ide-plugin.xml 13 | bin/ 14 | dump.rdb 15 | out 16 | target/ 17 | test-output 18 | 19 | # Eclipse artifacts, including WTP generated manifests 20 | .classpath 21 | .project 22 | .settings/ 23 | .springBeans 24 | spring-*/src/main/java/META-INF/MANIFEST.MF 25 | 26 | # IDEA artifacts and output dirs 27 | *.iml 28 | *.ipr 29 | *.iws 30 | .idea -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "build"] 2 | path = build 3 | url = git@github.com:cloud-native-java/build.git 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | sudo: required 4 | 5 | jdk: 6 | - oraclejdk8 7 | 8 | cache: 9 | directories: 10 | - $HOME/.m2/repository/ 11 | 12 | script: 13 | - mvn -e verify deploy 14 | -------------------------------------------------------------------------------- /configuration-basics/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | 9 | cnj 10 | configuration 11 | 1.0.0-SNAPSHOT 12 | 13 | 14 | 15 | 16 | boot.Application 17 | 18 | 19 | configuration-basics 20 | configuration/configuration-basics 21 | 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-actuator 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /configuration-basics/src/main/java/boot/Application.java: -------------------------------------------------------------------------------- 1 | package boot; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.boot.context.properties.ConfigurationProperties; 9 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 10 | import org.springframework.stereotype.Component; 11 | 12 | // <1> 13 | @EnableConfigurationProperties 14 | @SpringBootApplication 15 | public class Application { 16 | 17 | private final Log log = LogFactory.getLog(getClass()); 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(Application.class); 21 | } 22 | 23 | @Autowired 24 | public Application(ConfigurationProjectProperties cp) { 25 | log.info("configurationProjectProperties.projectName = " 26 | + cp.getProjectName()); 27 | } 28 | } 29 | 30 | // <2> 31 | @Component 32 | @ConfigurationProperties("configuration") 33 | class ConfigurationProjectProperties { 34 | 35 | private String projectName; // <3> 36 | 37 | public String getProjectName() { 38 | return projectName; 39 | } 40 | 41 | public void setProjectName(String projectName) { 42 | this.projectName = projectName; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /configuration-basics/src/main/java/classic/Application.java: -------------------------------------------------------------------------------- 1 | package classic; 2 | 3 | import org.apache.commons.logging.LogFactory; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class Application { 7 | 8 | public static void main(String[] args) { 9 | new ClassPathXmlApplicationContext("classic.xml"); 10 | } 11 | 12 | public void setConfigurationProjectName(String pn) { 13 | LogFactory.getLog(getClass()).info("the configuration project name is " + pn); 14 | } 15 | } -------------------------------------------------------------------------------- /configuration-basics/src/main/java/env/Application.java: -------------------------------------------------------------------------------- 1 | package env; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.beans.factory.InitializingBean; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.context.annotation.PropertySource; 12 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 13 | import org.springframework.core.env.Environment; 14 | 15 | import javax.annotation.PostConstruct; 16 | 17 | // <1> 18 | @Configuration 19 | @PropertySource("some.properties") 20 | public class Application { 21 | 22 | private final Log log = LogFactory.getLog(getClass()); 23 | 24 | public static void main(String[] args) throws Throwable { 25 | new AnnotationConfigApplicationContext(Application.class); 26 | } 27 | 28 | // <2> 29 | @Bean 30 | static PropertySourcesPlaceholderConfigurer pspc() { 31 | return new PropertySourcesPlaceholderConfigurer(); 32 | } 33 | 34 | // <3> 35 | @Value("${configuration.projectName}") 36 | private String fieldValue; 37 | 38 | // <4> 39 | @Autowired 40 | Application(@Value("${configuration.projectName}") String pn) { 41 | log.info("Application constructor: " + pn); 42 | } 43 | 44 | // <5> 45 | @Value("${configuration.projectName}") 46 | void setProjectName(String projectName) { 47 | log.info("setProjectName: " + projectName); 48 | } 49 | 50 | // <6> 51 | @Autowired 52 | void setEnvironment(Environment env) { 53 | log.info("setEnvironment: " + env.getProperty("configuration.projectName")); 54 | } 55 | 56 | // <7> 57 | @Bean 58 | InitializingBean both(Environment env, 59 | @Value("${configuration.projectName}") String projectName) { 60 | return () -> { 61 | log.info("@Bean with both dependencies (projectName): " + projectName); 62 | log.info("@Bean with both dependencies (env): " 63 | + env.getProperty("configuration.projectName")); 64 | }; 65 | } 66 | 67 | @PostConstruct 68 | void afterPropertiesSet() throws Throwable { 69 | log.info("fieldValue: " + this.fieldValue); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /configuration-basics/src/main/java/profiles/Application.java: -------------------------------------------------------------------------------- 1 | package profiles; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.beans.factory.InitializingBean; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.context.annotation.*; 8 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 9 | import org.springframework.core.env.Environment; 10 | import org.springframework.util.StringUtils; 11 | 12 | @Configuration 13 | public class Application { 14 | 15 | private Log log = LogFactory.getLog(getClass()); 16 | 17 | @Bean 18 | static PropertySourcesPlaceholderConfigurer pspc() { 19 | return new PropertySourcesPlaceholderConfigurer(); 20 | } 21 | 22 | // <1> 23 | @Configuration 24 | @Profile("prod") 25 | @PropertySource("some-prod.properties") 26 | public static class ProdConfiguration { 27 | 28 | @Bean 29 | InitializingBean init() { 30 | return () -> LogFactory.getLog(getClass()).info("prod InitializingBean"); 31 | } 32 | } 33 | 34 | @Configuration 35 | @Profile({ "default", "dev" }) 36 | // <2> 37 | @PropertySource("some.properties") 38 | public static class DefaultConfiguration { 39 | 40 | @Bean 41 | InitializingBean init() { 42 | return () -> LogFactory.getLog(getClass()).info("default InitializingBean"); 43 | } 44 | } 45 | 46 | // <3> 47 | @Bean 48 | InitializingBean which(Environment e, 49 | @Value("${configuration.projectName}") String projectName) { 50 | return () -> { 51 | log.info("activeProfiles: '" 52 | + StringUtils.arrayToCommaDelimitedString(e.getActiveProfiles()) + "'"); 53 | log.info("configuration.projectName: " + projectName); 54 | }; 55 | } 56 | 57 | public static void main(String[] args) { 58 | AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); 59 | ac.getEnvironment().setActiveProfiles("dev"); // <4> 60 | ac.register(Application.class); 61 | ac.refresh(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /configuration-basics/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | configuration: 3 | projectName : Spring Boot 4 | management: 5 | security: 6 | enabled: false 7 | 8 | 9 | -------------------------------------------------------------------------------- /configuration-basics/src/main/resources/classic.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /configuration-basics/src/main/resources/some-prod.properties: -------------------------------------------------------------------------------- 1 | configuration.projectName=Spring Framework (in Production) -------------------------------------------------------------------------------- /configuration-basics/src/main/resources/some.properties: -------------------------------------------------------------------------------- 1 | configuration.projectName=Spring Framework 2 | 3 | 4 | -------------------------------------------------------------------------------- /configuration-client/manifest.yml: -------------------------------------------------------------------------------- 1 | --- 2 | applications: 3 | - name: configuration-client 4 | memory: 512M 5 | instances: 1 6 | host: configuration-client-${random-word} 7 | path: target/configuration-client.jar 8 | services: 9 | - configuration-service 10 | - rabbitmq-bus 11 | env: 12 | SPRING_PROFILES_ACTIVE: cloud 13 | DEBUG: "true" 14 | debug: "true" -------------------------------------------------------------------------------- /configuration-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | cnj 9 | configuration 10 | 1.0.0-SNAPSHOT 11 | 12 | 13 | configuration-client 14 | configuration/configuration-client 15 | 16 | 17 | 18 | org.springframework.cloud 19 | spring-cloud-config-client 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-actuator 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-bus-amqp 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-maven-plugin 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /configuration-client/src/main/java/demo/Application.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; 6 | import org.springframework.context.event.EventListener; 7 | import org.springframework.core.Ordered; 8 | import org.springframework.core.annotation.Order; 9 | import org.springframework.stereotype.Component; 10 | 11 | @SpringBootApplication 12 | public class Application { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(Application.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /configuration-client/src/main/java/demo/ProjectNameRestController.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.cloud.context.config.annotation.RefreshScope; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | // <1> 10 | @RestController 11 | @RefreshScope 12 | class ProjectNameRestController { 13 | 14 | private final String projectName; 15 | 16 | @Autowired 17 | public ProjectNameRestController( 18 | @Value("${configuration.projectName}") String pn) { // <2> 19 | this.projectName = pn; 20 | } 21 | 22 | @RequestMapping("/project-name") 23 | String projectName() { 24 | return this.projectName; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /configuration-client/src/main/java/demo/RefreshCounter.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; 6 | import org.springframework.context.event.EventListener; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.util.concurrent.atomic.AtomicLong; 10 | 11 | @Component 12 | public class RefreshCounter { 13 | 14 | private final Log log = LogFactory.getLog(getClass()); 15 | 16 | private final AtomicLong counter = new AtomicLong(0); // <1> 17 | 18 | // <2> 19 | @EventListener 20 | public void refresh(RefreshScopeRefreshedEvent e) { 21 | this.log.info("The refresh count is now at: " 22 | + this.counter.incrementAndGet()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /configuration-client/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: configuration-client 4 | cloud: 5 | config: 6 | uri: ${vcap.services.configuration-service.credentials.uri:http://localhost:pass:[]8888} 7 | 8 | 9 | -------------------------------------------------------------------------------- /configuration-it/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | cnj 9 | configuration 10 | 1.0.0-SNAPSHOT 11 | 12 | 13 | configuration-it 14 | 15 | 16 | 17 | cnj 18 | it-support 19 | ${project.version} 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | test 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-test 29 | 30 | 31 | org.cloudfoundry 32 | cloudfoundry-client-lib 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /configuration-it/src/integration-test/java/configuration/ConfigurationIT.java: -------------------------------------------------------------------------------- 1 | package configuration; 2 | 3 | import cnj.CloudFoundryService; 4 | import org.apache.commons.logging.Log; 5 | import org.apache.commons.logging.LogFactory; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.autoconfigure.SpringBootApplication; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.http.HttpStatus; 13 | import org.springframework.http.ResponseEntity; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | import org.springframework.web.client.RestTemplate; 16 | 17 | import java.io.File; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(classes = ConfigurationIT.Config.class) 24 | public class ConfigurationIT { 25 | 26 | @SpringBootApplication 27 | public static class Config { 28 | } 29 | 30 | private Log log = LogFactory.getLog(getClass()); 31 | 32 | private RestTemplate restTemplate = new RestTemplate(); 33 | 34 | @Autowired 35 | private CloudFoundryService service; 36 | 37 | @Before 38 | public void before() throws Throwable { 39 | 40 | File root = new File("."); 41 | File configClientManifest = new File(root, 42 | "../configuration-client/manifest.yml"); 43 | File configServiceManifest = new File(root, 44 | "../configuration-service/manifest.yml"); 45 | 46 | String rmqService = "rabbitmq-bus"; 47 | this.service.createServiceIfMissing("cloudamqp", "lemur", rmqService); 48 | this.service 49 | .pushApplicationAndCreateUserDefinedServiceUsingManifest(configServiceManifest); 50 | this.service.pushApplicationUsingManifest(configClientManifest); 51 | } 52 | 53 | @Test 54 | public void clientIsConnectedToService() throws Exception { 55 | 56 | String configClientUrl = this.service 57 | .urlForApplication("configuration-client"); 58 | log.info("the application is running at " + configClientUrl); 59 | String url = configClientUrl + "/project-name"; 60 | log.info("url: " + url); 61 | ResponseEntity entity = this.restTemplate.getForEntity(url, 62 | String.class); 63 | assertEquals(entity.getStatusCode(), HttpStatus.OK); 64 | assertTrue(entity.getBody().contains("Spring Cloud")); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /configuration-service/manifest.yml: -------------------------------------------------------------------------------- 1 | --- 2 | applications: 3 | - name: configuration-service 4 | memory: 512M 5 | instances: 1 6 | host: configuration-service-${random-word} 7 | path: target/configuration-service.jar 8 | env: 9 | SPRING_PROFILES_ACTIVE: cloud 10 | DEBUG: "true" 11 | debug: "true" -------------------------------------------------------------------------------- /configuration-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | cnj 9 | configuration 10 | 1.0.0-SNAPSHOT 11 | .. 12 | 13 | 14 | 15 | configuration/configuration-service 16 | configuration-service 17 | 18 | 19 | 20 | org.springframework.cloud 21 | spring-cloud-config-server 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-maven-plugin 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /configuration-service/src/main/java/demo/Application.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.config.server.EnableConfigServer; 6 | 7 | // <1> 8 | @SpringBootApplication 9 | @EnableConfigServer 10 | public class Application { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(Application.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /configuration-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8888 2 | spring.cloud.config.server.git.uri=\ 3 | https://github.com/cloud-native-java/config-server-configuration-repository 4 | -------------------------------------------------------------------------------- /configuration-service/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: configuration-service 4 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | pom 8 | 9 | 10 | cnj 11 | parent 12 | 1.0.0-SNAPSHOT 13 | 14 | 15 | 16 | 17 | configuration 18 | 19 | 20 | configuration-basics 21 | configuration-client 22 | configuration-service 23 | configuration-it 24 | 25 | 26 | 27 | 28 | 29 | org.jfrog.buildinfo 30 | artifactory-maven-plugin 31 | 2.4.0 32 | false 33 | 34 | 35 | build-info 36 | 37 | publish 38 | 39 | 40 | 41 | {{TRAVIS_COMMIT}} 42 | 43 | 44 | 45 | https://cloudnativejava.artifactoryonline.com/cloudnativejava 46 | 47 | ${env.ARTIFACTORY_USERNAME} 48 | ${env.ARTIFACTORY_PASSWORD} 49 | libs-release-local 50 | libs-snapshot-local 51 | 52 | 53 | Travis CI 54 | {{TRAVIS_BUILD_NUMBER}} 55 | 56 | http://travis-ci.org/{{TRAVIS_REPO_SLUG}}/builds/{{TRAVIS_BUILD_ID}} 57 | 58 | {{USER}} 59 | {{TRAVIS_COMMIT}} 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | false 72 | 73 | central 74 | libs-release 75 | https://cloudnativejava.artifactoryonline.com/cloudnativejava/libs-release 76 | 77 | 78 | 79 | snapshots 80 | libs-snapshot 81 | https://cloudnativejava.artifactoryonline.com/cloudnativejava/libs-snapshot 82 | 83 | 84 | 85 | 86 | 87 | false 88 | 89 | central 90 | plugins-release 91 | https://cloudnativejava.artifactoryonline.com/cloudnativejava/plugins-release 92 | 93 | 94 | 95 | snapshots 96 | plugins-snapshot 97 | https://cloudnativejava.artifactoryonline.com/cloudnativejava/plugins-snapshot 98 | 99 | 100 | 101 | 102 | 103 | --------------------------------------------------------------------------------