├── .gitignore ├── src ├── main │ ├── appengine │ │ └── app.yaml │ ├── resources │ │ └── config │ │ │ ├── application-docker.properties │ │ │ ├── application-kubernetes.properties │ │ │ ├── application.properties │ │ │ └── application-gae.properties │ └── java │ │ └── com │ │ └── google │ │ └── spring │ │ └── demo │ │ ├── controller │ │ ├── AppEngineLifecycleController.groovy │ │ └── HelloWorldController.groovy │ │ └── Application.groovy ├── docker-compose │ ├── single.yml │ └── load-balanced.yml └── kubernetes │ ├── spring-boot-demo-service.yaml │ ├── redis-master-service.yaml │ ├── spring-boot-demo-external-service.yaml │ ├── redis-master-pod.yaml │ └── spring-boot-demo-controller.yaml ├── Dockerfile ├── README.md ├── pom.xml └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | target/ 3 | .settings/ 4 | .classpath 5 | .project 6 | .springBeans 7 | -------------------------------------------------------------------------------- /src/main/appengine/app.yaml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | runtime: custom 18 | vm: true 19 | api_version: 1 -------------------------------------------------------------------------------- /src/main/resources/config/application-docker.properties: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | spring.profiles.include=replication -------------------------------------------------------------------------------- /src/main/resources/config/application-kubernetes.properties: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | spring.profiles.include=replication -------------------------------------------------------------------------------- /src/main/resources/config/application.properties: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | app.name=Spring Boot Demo 18 | app.version=1.0 -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | FROM maven:3.2-jdk-8-onbuild 18 | CMD ["java", "-jar", "target/spring-boot-demo-0.0.1-SNAPSHOT.jar"] 19 | -------------------------------------------------------------------------------- /src/docker-compose/single.yml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | app: 18 | image: spring-boot-demo 19 | links: 20 | - redis 21 | redis: 22 | image: redis 23 | -------------------------------------------------------------------------------- /src/main/resources/config/application-gae.properties: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | logging.path=/var/log/app_engine/custom_logs/ 18 | spring.redis.host=redis-51kt 19 | spring.redis.port=6379 20 | spring.profiles.include=replication -------------------------------------------------------------------------------- /src/kubernetes/spring-boot-demo-service.yaml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | id: spring-boot-demo-service 18 | kind: Service 19 | apiVersion: v1beta1 20 | port: 80 21 | containerPort: 8080 22 | sessionAffinity: none 23 | selector: 24 | name: spring-boot-demo 25 | labels: 26 | visualize: "true" 27 | -------------------------------------------------------------------------------- /src/kubernetes/redis-master-service.yaml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | kind: Service 18 | apiVersion: v1beta1 19 | id: redis-master 20 | port: 6379 21 | containerPort: redis-server 22 | selector: 23 | name: redis 24 | role: master 25 | labels: 26 | name: redis 27 | role: master 28 | visualize: "true" 29 | -------------------------------------------------------------------------------- /src/kubernetes/spring-boot-demo-external-service.yaml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | id: spring-boot-demo-e 18 | kind: Service 19 | apiVersion: v1beta1 20 | port: 80 21 | containerPort: 8080 22 | createExternalLoadBalancer: true 23 | selector: 24 | name: spring-boot-demo 25 | labels: 26 | name: spring-boot-demo-service 27 | visualize: "true" 28 | -------------------------------------------------------------------------------- /src/main/java/com/google/spring/demo/controller/AppEngineLifecycleController.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.spring.demo.controller 17 | 18 | import org.springframework.web.bind.annotation.RequestMapping; 19 | import org.springframework.web.bind.annotation.RestController; 20 | 21 | @RestController 22 | class AppEngineLifecycleController { 23 | @RequestMapping("/_ah/start") 24 | def start() { 25 | return "OK"; 26 | } 27 | 28 | @RequestMapping("/_ah/health") 29 | def health() { 30 | return "OK"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/kubernetes/redis-master-pod.yaml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | kind: Pod 18 | apiVersion: v1beta1 19 | id: redis-master 20 | labels: 21 | name: redis 22 | role: master 23 | visualize: "true" 24 | desiredState: 25 | manifest: 26 | version: v1beta1 27 | id: redis-master 28 | containers: 29 | - name: redis-master 30 | image: redis 31 | ports: 32 | - name: redis-server 33 | containerPort: 6379 34 | -------------------------------------------------------------------------------- /src/docker-compose/load-balanced.yml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | 17 | lb: 18 | image: jwilder/nginx-proxy 19 | ports: 20 | - "80:80" 21 | volumes: 22 | - "/var/run/docker.sock:/tmp/docker.sock" 23 | app1: 24 | image: spring-boot-demo 25 | links: 26 | - redis 27 | environment: 28 | VIRTUAL_HOST: docker-machine.dev 29 | app2: 30 | image: spring-boot-demo 31 | links: 32 | - redis 33 | environment: 34 | VIRTUAL_HOST: docker-machine.dev 35 | redis: 36 | image: redis 37 | -------------------------------------------------------------------------------- /src/kubernetes/spring-boot-demo-controller.yaml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright 2015 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ############################################################################### 16 | kind: ReplicationController 17 | apiVersion: v1beta1 18 | id: spring-boot-demo-controller 19 | labels: 20 | name: spring-boot-demo 21 | visualize: "true" 22 | desiredState: 23 | replicas: 2 24 | replicaSelector: 25 | name: spring-boot-demo 26 | podTemplate: 27 | labels: 28 | name: spring-boot-demo 29 | uses: redis-master 30 | visualize: "true" 31 | desiredState: 32 | manifest: 33 | version: v1beta1 34 | id: spring-boot-demo 35 | labels: 36 | name: spring-boot-demo-pod 37 | visualize: "true" 38 | containers: 39 | - name: spring-boot-demo 40 | image: gcr.io/wise-coyote-827/spring-boot-demo 41 | imagePullPolicy: PullAlways 42 | ports: 43 | - name: http 44 | containerPort: 8080 45 | -------------------------------------------------------------------------------- /src/main/java/com/google/spring/demo/controller/HelloWorldController.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.spring.demo.controller 17 | 18 | import org.springframework.beans.factory.annotation.Value 19 | import org.springframework.context.annotation.Scope 20 | import org.springframework.web.bind.annotation.RequestMapping 21 | import org.springframework.web.bind.annotation.RequestMethod 22 | import org.springframework.web.bind.annotation.RestController 23 | import org.springframework.web.context.request.RequestContextHolder 24 | 25 | @RestController 26 | @Scope("session") 27 | class HelloWorldController implements Serializable { 28 | @Value('${app.version}') 29 | String version; 30 | Integer count = 0; 31 | 32 | @RequestMapping(value="/helloworld", method=RequestMethod.GET, produces="application/json") 33 | def helloworld() { 34 | HelloWorldResponse response = new HelloWorldResponse( 35 | hostname: hostname(), 36 | sessionId: RequestContextHolder.currentRequestAttributes().getSessionId(), 37 | count: ++count, 38 | version: version 39 | ); 40 | 41 | return response; 42 | } 43 | 44 | @RequestMapping("/version") 45 | def version() { 46 | return version 47 | } 48 | 49 | def hostname() { 50 | try { 51 | return InetAddress.getLocalHost().getHostName(); 52 | } catch (all) { 53 | return "unknown"; 54 | } 55 | } 56 | } 57 | 58 | class HelloWorldResponse { 59 | String hostname; 60 | String sessionId; 61 | int count; 62 | String version; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 16 | 17 | Spring Boot Docker Example 18 | ========================== 19 | This is an example Spring Boot "Hello World" micro-service application that can be containerized and subsequently executed in Docker, Kubernetes, and Google App Engine! 20 | 21 | This is not an official Google product. 22 | 23 | Building 24 | ======== 25 | Regular Maven build: 26 | 27 | mvn package 28 | 29 | Build Docker container: 30 | 31 | mvn docker:build 32 | 33 | Running 34 | ======= 35 | Java 36 | ---- 37 | Pretty straight forward: 38 | 39 | mvn exec:java 40 | 41 | Docker 42 | ------ 43 | Without Redis for session: 44 | 45 | docker run -ti spring-boot-demo 46 | 47 | With Redis for session: 48 | 49 | docker run -d --name redis -p 6379:6379 redis 50 | docker run -ti --rm --link redis:redis -p 8080:8080 spring-boot-demo 51 | 52 | Docker Compose 53 | -------------- 54 | Docker Compose uses a descriptor to describe the Docker container and linking configurations. 55 | Rather than manually executing commands like the previous section, you can start the demo with Redis by running: 56 | 57 | cd src/test/docker-compose 58 | docker-compose up 59 | 60 | The configuration is stored in `docker-compose.yml` 61 | 62 | Kubernetes 63 | ---------- 64 | The following instructions assume that you already have Kubernetes cluster up and running. If not, check out [Google Container Engine](https://cloud.google.com/container-engine/) to get started quickly. 65 | 66 | Change directory to where all the Kubernetes configuration files are: 67 | 68 | cd src/test/kubernetes 69 | 70 | Deploy Redis master pod: 71 | 72 | kubectl create -f redis-master-pod.yaml 73 | 74 | Create a Redis master service: 75 | 76 | kubectl create -f redis-master-service.yaml 77 | 78 | Deploy Spring Boot Demo replication controller: 79 | 80 | kubectl create -f spring-boot-demo-controller.yaml 81 | 82 | Create a service: 83 | 84 | kubectl create -f spring-boot-demo-service.yaml 85 | 86 | If you are on Google Container Engine, or Kubernetes deployment that supports external services, you can create an external service: 87 | 88 | kubectl create -f spring-boot-demo-external-service.yaml 89 | 90 | Google App Engine 91 | ----------------- 92 | The instruction assumes that you have [Google Cloud SDK](https://cloud.google.com/sdk/) already installed and that you are familiar with [Google App Engine Managed VMs](https://cloud.google.com/appengine/docs/managed-vms/). 93 | 94 | To run locally: 95 | 96 | mvn gcloud:run 97 | 98 | To deploy into Google App Engine, you'll first need to create a Redis server. You can deploy Redis easily by using [Click to Deploy](https://cloud.google.com/solutions/redis/). 99 | 100 | Once deployed, update `src/main/resources/application-gae.properties` file, and set: 101 | 102 | spring.redis.host=YOUR_REDIS_MASTER_INSTANCE_NAME 103 | 104 | Build the Docker image, so that it generates Dockerfile 105 | 106 | mvn docker:build 107 | 108 | Finally, deploy the application using `gcloud` plugin: 109 | 110 | mvn gcloud:deploy -Dgcloud.project=YOUR_PROJECT_ID 111 | 112 | -------------------------------------------------------------------------------- /src/main/java/com/google/spring/demo/Application.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.spring.demo 17 | 18 | import groovy.util.logging.Slf4j; 19 | 20 | import java.util.Arrays; 21 | 22 | import javax.annotation.PostConstruct; 23 | 24 | import org.springframework.beans.factory.annotation.Value; 25 | import org.springframework.boot.SpringApplication 26 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration 27 | import org.springframework.boot.context.embedded.FilterRegistrationBean; 28 | import org.springframework.context.annotation.Bean; 29 | import org.springframework.context.annotation.ComponentScan 30 | import org.springframework.context.annotation.Configuration 31 | import org.springframework.context.annotation.Profile; 32 | import org.springframework.core.annotation.Order; 33 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 34 | import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; 35 | import org.springframework.session.web.http.SessionRepositoryFilter; 36 | import org.springframework.web.filter.DelegatingFilterProxy; 37 | 38 | import com.fasterxml.jackson.databind.deser.impl.BeanAsArrayBuilderDeserializer; 39 | 40 | @Configuration 41 | @EnableAutoConfiguration 42 | @ComponentScan(basePackages = "com.google.spring.demo") 43 | class Application { 44 | static void main(String[] args) { 45 | def executionEnv = new ExecutionEnvironment(); 46 | def app = new SpringApplication(Application.class); 47 | def profiles = executionEnv.determineProfiles(); 48 | println profiles 49 | if (profiles != null) { 50 | app.additionalProfiles = profiles; 51 | } 52 | 53 | app.run(args); 54 | } 55 | } 56 | 57 | @Slf4j 58 | class ExecutionEnvironment { 59 | def determineProfiles() { 60 | def gaeEnvironment = System.getenv("GAE_PARTITION"); 61 | def dockerEnvironment = System.getenv("REDIS_NAME"); 62 | def kubernetesEnvironment = System.getenv("REDIS_MASTER_SERVICE_HOST"); 63 | 64 | if (gaeEnvironment != null) { 65 | if ("dev" == gaeEnvironment) { 66 | log.info "Detected Google App Engine development environment - no session replication" 67 | } 68 | if ("dev" != gaeEnvironment) { 69 | log.info "Detected Google App Engine production environment - enable session replication" 70 | return ["GAE", "replication"]; 71 | } 72 | } else if (dockerEnvironment != null) { 73 | log.info "Detected Docker environment with Redis - enable session replication" 74 | return ["docker", "replication"]; 75 | } else if (kubernetesEnvironment != null) { 76 | log.info "Detected Kubernetes environment with Redis - enable session replication" 77 | return ["kubernetes", "replication"] 78 | } else { 79 | log.info "Didn't detect any special environments - no replication" 80 | } 81 | } 82 | } 83 | 84 | @Profile("kubernetes") 85 | @Configuration 86 | class KubernetesConfig { 87 | @Value(value="#{systemEnvironment['REDIS_MASTER_SERVICE_HOST']}") 88 | String redisHost; 89 | 90 | @Value("#{systemEnvironment['REDIS_MASTER_SERVICE_PORT']}") 91 | Integer redisPort; 92 | 93 | @Bean 94 | JedisConnectionFactory connectionFactory() { 95 | return new JedisConnectionFactory(hostName: redisHost, port: redisPort); 96 | } 97 | } 98 | 99 | @Profile("docker") 100 | @Configuration 101 | class DockerConfig { 102 | @Value(value="#{systemEnvironment['REDIS_PORT_6379_TCP_ADDR']}") 103 | String redisHost; 104 | 105 | @Value("#{systemEnvironment['REDIS_PORT_6379_TCP_PORT']}") 106 | Integer redisPort; 107 | 108 | @Bean 109 | JedisConnectionFactory connectionFactory() { 110 | return new JedisConnectionFactory(hostName: redisHost, port: redisPort); 111 | } 112 | } 113 | 114 | @Profile("GAE") 115 | @Configuration 116 | class GAEConfig { 117 | @Value('${spring.redis.host}') 118 | String redisHost; 119 | 120 | @Value('${spring.redis.port}') 121 | Integer redisPort; 122 | 123 | @Bean 124 | JedisConnectionFactory connectionFactory() { 125 | return new JedisConnectionFactory(hostName: redisHost, port: redisPort); 126 | } 127 | } 128 | 129 | @Configuration 130 | @Profile("replication") 131 | @EnableRedisHttpSession 132 | @Slf4j 133 | class SessionReplicationConfig { 134 | @Bean 135 | @Order(value = 0) 136 | def sessionRepositoryFilterRegistration( 137 | SessionRepositoryFilter springSessionRepositoryFilter) { 138 | log.info "Enabling Redis Http Session" 139 | def bean = new FilterRegistrationBean( 140 | filter: new DelegatingFilterProxy(springSessionRepositoryFilter), 141 | urlPatterns: ["/*"] as List); 142 | return bean; 143 | } 144 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 19 | 4.0.0 20 | 21 | com.google.examples 22 | spring-boot-demo 23 | 0.0.1-SNAPSHOT 24 | 25 | 26 | 1.9.18-SNAPSHOT 27 | com.google.spring.demo.Application 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-parent 33 | 1.2.1.RELEASE 34 | 35 | 36 | 37 | 38 | org.codehaus.groovy 39 | groovy 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-web 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-actuator 48 | 49 | 50 | org.springframework.session 51 | spring-session-data-redis 52 | 1.0.0.RELEASE 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-starter-social-linkedin 57 | 58 | 59 | com.google.inject 60 | guice 61 | 2.0 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-data-elasticsearch 66 | 67 | 68 | org.scala-lang 69 | scala-library 70 | 2.10.4 71 | 72 | 73 | 74 | 75 | 76 | 77 | org.springframework.boot 78 | spring-boot-maven-plugin 79 | 80 | 81 | com.spotify 82 | docker-maven-plugin 83 | 0.1.1 84 | 85 | java:8 86 | ${project.artifactId} 87 | ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/${project.build.finalName}.jar"] 88 | 89 | 8080 90 | 91 | 92 | 93 | /app/ 94 | ${project.build.directory} 95 | ${project.build.finalName}.jar 96 | 97 | 98 | / 99 | src/main/appengine 100 | *.* 101 | 102 | 103 | 104 | 105 | 106 | maven-compiler-plugin 107 | 108 | groovy-eclipse-compiler 109 | 110 | 111 | 112 | org.codehaus.groovy 113 | groovy-eclipse-compiler 114 | 2.9.1-01 115 | 116 | 117 | org.codehaus.groovy 118 | groovy-eclipse-batch 119 | 2.3.7-01 120 | 121 | 122 | 123 | 124 | org.codehaus.groovy 125 | groovy-eclipse-compiler 126 | 2.9.1-01 127 | true 128 | 129 | 130 | org.codehaus.mojo 131 | exec-maven-plugin 132 | 133 | ${main.class} 134 | 135 | 136 | 137 | com.google.appengine 138 | gcloud-maven-plugin 139 | ${gcloud.version} 140 | 141 | ${gcloud.project} 142 | target/docker/ 143 | 144 | 145 | 146 | 147 | 148 | 149 | google-snapshots 150 | Google Snapshots 151 | https://oss.sonatype.org/content/repositories/google-snapshots/ 152 | 153 | true 154 | 155 | 156 | false 157 | 158 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. --------------------------------------------------------------------------------