You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bind-service.html.md.erb
+83-122
Original file line number
Diff line number
Diff line change
@@ -10,22 +10,22 @@ The <a href="../service-offerings/index.html" target="_blank">service marketplac
10
10
Create the Redis datastore:
11
11
12
12
<preclass="terminal">
13
-
$ cf create-service redis small my-redis-service
14
-
Creating service instance my-redis-service in org MyOrg / space MySpace as [email protected]...
13
+
$ cf create-service redis small my-redis
14
+
Creating service instance my-redis in org MyOrg / space MySpace as [email protected]...
15
15
OK
16
16
17
-
Create in progress. Use 'cf services' or 'cf service my-redis-service' to check operation status.
17
+
Create in progress. Use 'cf services' or 'cf service my-redis' to check operation status.
18
18
19
-
Attention: The plan `small` of service `redis` is not free. The instance `my-redis-service` will incur a cost. Contact your administrator if you think this is in error.
19
+
Attention: The plan `small` of service `redis` is not free. The instance `my-redis` will incur a cost. Contact your administrator if you think this is in error.
20
20
</pre>
21
21
22
22
This creates a small Redis datastore for you which you now have to bind to your application. Binding means that the credentials and URL of the service will be written dynamically into the environment variables of the app as `VCAP_SERVICES` and can hence be used directly from there.
23
23
24
24
Now bind the new service to your existing application:
25
25
26
26
<preclass="terminal">
27
-
$ cf bind-service my-java-app my-redis-service
28
-
Binding service my-redis-service to app my-java-app in org MyOrg / space MySpace as [email protected]...
27
+
$ cf bind-service my-java-app my-redis
28
+
Binding service my-redis to app my-java-app in org MyOrg / space MySpace as [email protected]...
29
29
OK
30
30
TIP: Use 'cf restage my-java-app' to ensure your env variable changes take effect
31
31
</pre>
@@ -50,7 +50,7 @@ System-Provided:
50
50
"port": 59282
51
51
},
52
52
"label": "redis",
53
-
"name": "my-redis-service",
53
+
"name": "my-redis",
54
54
"plan": "small",
55
55
"provider": null,
56
56
"syslog_drain_url": null,
@@ -74,150 +74,111 @@ Restaging app my-java-app in MyOrg monitor / space MySpace as [email protected].
74
74
...
75
75
</pre>
76
76
77
-
Now you want to consume your new Redis datastore from within your application. In order to do that, you have to adjust the code to store and retrieve data via Redis datastore. But first you have to add following<ahref="https://github.com/xetorthio/jedis" target="_blank">Jedis</a> dependency to the `build.gradle` file. Jedis is a Java Redis client that lets your app interact with the Redis server.
77
+
Now you want to consume your new Redis datastore from within your application. In order to do that, you have to adjust the code to store and retrieve data via Redis. But first you have to add the<ahref="https://github.com/xetorthio/jedis" target="_blank">Jedis</a> dependency to the "dependencies" section of your `build.gradle` file. Jedis is a Java Redis client that lets your app interact with the Redis server.
throw new RuntimeException("Redis service declaration not found", exception);
113
+
}
114
+
}
115
+
throw new RuntimeException("Redis service declaration not found");
155
116
}
156
117
```
157
118
158
-
Thd code snippet above ensures that your app reads the credentials of your Redis service from the environment variables in order to setup the `ProductRepository`.
119
+
The code snippet above ensures that your app reads the credentials of your Redis service from the environment variables in order to setup the `ProductRepository`.
159
120
160
121
Now adjust the existing `ProductRepository` (a static inner class of `ProductServce`) to store and retrieve products via Redis datastore:
161
122
162
-
163
-
```
123
+
```java
164
124
public static class ProductRepository {
165
-
166
-
private Jedis jedis;
167
-
private ObjectMapper mapper;
168
-
169
-
public ProductRepository(Map<String,Object> redisCredentials) {
170
-
jedis = new Jedis((String) redisCredentials.get("host"), (int) redisCredentials.get("port"));
throw new RuntimeException("Product cannot be deserialized from JSON");
164
+
}
165
+
}
166
+
206
167
}
207
168
```
208
169
209
-
With this latest code you can still experiment locally, since your app uses a simple default `ProductRepository` instead of the Redis-based repository in case your Redis service binding cannot be found. This allows you to run your app locally as well as in the cloud without having to configure anything differently. However, you must install Redis on your local machine, if you want to test your app with a local Redis server.
170
+
With this latest code you can still experiment locally, since your app uses a simple default `ProductRepository` instead of the Redis-based repository in case your Redis service binding cannot be found. This allows you to run your app locally as well as in the cloud without having to configure anything differently. However, you must install <ahref="http://redis.io/topics/quickstart" target="_blank">Redis</a> on your local machine, if you want to test your app with a local Redis server.
210
171
211
172
Now compile/build your changes and push them to the cloud:
You can access other services like MongoDB or MariaDB in a similar matter, simply by binding them to your app and accessing them through the environment variables.
219
180
220
-
You can checkout the finished version of all these changes in the `advanced` branch of the sample app repository, at: https://github.com/swisscom/cf-sample-app-java/tree/advanced
181
+
You can checkout the finished version of all these changes in the `advanced` branch of the sample app repository on <ahref="https://github.com/swisscom/cf-sample-app-java/tree/advanced" target="_blank">Github</a>.
221
182
222
183
<divstyle="text-align:center;padding:3em;">
223
184
<ahref="./manifest.html" class="btn btn-primary">I've bound a service to my App</a>
Copy file name to clipboardExpand all lines: dependencies.html.md.erb
+9-9
Original file line number
Diff line number
Diff line change
@@ -5,25 +5,25 @@ owner: Daniel Freitag
5
5
6
6
<strong><%=modified_date%></strong>
7
7
8
-
<%=vars.product_short%> uses the Java buildpack to execute the uploaded JAR file as application package. The Java buildpack will run your application using the `main()` method of the `Main` class declard in the `build.gradle` file.
8
+
<%=vars.product_short%> uses the Java buildpack to execute the uploaded JAR file as application package. The Java buildpack will run your application using the `main()` method of the `Main` class declared in the `build.gradle` file.
9
9
10
-
The dependencies needed for the application are declared in the `build.gradle` file and are included in the standalone JAR.
10
+
The dependencies needed for the application are also declared in the `build.gradle` file and are therefore included in the standalone JAR.
11
11
12
-
The `build.gradle` file of your deployed sample app should look as follows:
12
+
The `build.gradle` file of your deployed sample app looks something like this:
Copy file name to clipboardExpand all lines: deploy.html.md.erb
+3-3
Original file line number
Diff line number
Diff line change
@@ -31,14 +31,14 @@ BUILD SUCCESSFUL
31
31
Total time: 6.78 secs
32
32
</pre>
33
33
34
-
Gradle will build a Java archive (JAR) file of your app. It is located under `target/build/libs/cf-sample-app-java-1.0.jar`. The Java archive contains all dependencies.
34
+
Gradle will build a Java archive (JAR) file of your app. It is located under `target/build/libs/cf-sample-app-java-1.0.jar`. The Java archive contains all dependencies needed by your app.
35
35
36
36
Push your app to the cloud by executing the following command and replacing the "my-random-hostname" with your own hostname. This will be part of the URL your app will be reached at and it has to be globally unique so be creative.
37
37
38
-
The `-b java_buildpack` tells Cloud Foundry to use the Java buildpack for our app and the `-p build/libs/cf-sample-app-java-1.0.jar` tells Cloud Foundry where to find the compiled application to push to the Cloud. Note that the `-b` option can be ommitted since the buildpack auto-detection support ensures that the appropriate buildpack for a given app will be found.
38
+
The `-p build/libs/cf-sample-app-java-1.0.jar` tells Cloud Foundry where to find the compiled application to push to the Cloud.
Copy file name to clipboardExpand all lines: environment.html.md.erb
+2-2
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ owner: Daniel Freitag
7
7
8
8
<%=vars.product_short%> lets you externalize configuration - storing application properties in <ahref="../devguide/deploy-apps/environment-variable.html" target="_blank">environment variables</a>.
9
9
10
-
At runtime, environment variables are exposed to the application through its environment. You can use this for example to run your app in production mode setting the APP_MODE environment variable.
10
+
At runtime, environment variables are exposed to the application through its environment. You can use this for example to run your app in production mode setting the `APP_MODE` environment variable.
11
11
12
12
<preclass="terminal">
13
13
$ cf set-env my-java-app APP_MODE production
@@ -26,7 +26,7 @@ Restaging app my-java-app in org MyOrg / space MySpace as [email protected]...
26
26
...
27
27
</pre>
28
28
29
-
All environment variables can be accessed using `System.getenv().get('ENV_VARIABLE')` or `new ProcessBuilder().environment().get("ENV_VARIABLE")` from within your Java app.
29
+
All environment variables can be accessed using `System.getenv().get('ENV_VARIABLE')` or `new ProcessBuilder().environment().get("ENV_VARIABLE")` from within your Java app.
30
30
31
31
<divstyle="text-align:center;margin:3em;">
32
32
<ahref="./bind-service.html" class="btn btn-primary">I've set my environment variable</a>
Furthermore, you must have a first Cloud Foundry <ahref="../concepts/roles.html#orgs" target="_blank">Organization</a> and <ahref="../concepts/roles.html#spaces" target="_blank">Space</a> to push your apps to. In case you don't have that yet, please follow the <ahref="https://console.developer.swisscom.com" target="_blank">tutorials in the web console</a> before returning here.
Copy file name to clipboardExpand all lines: next-steps.html.md.erb
+2-1
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,8 @@ owner: Daniel Freitag
5
5
6
6
You now know how to deploy an app, change its configuration, view logs, scale, and bind it to services.
7
7
8
-
Here’s some recommended reading. The first one describes the buildpack which Cloud Foundry uses for Java applications. The second one is a collection of factors you should consider when creating a modern cloud-native application.
8
+
Here’s some recommended reading. The first one is a set of tips for Java developers in the cloud. The second one describes the buildpack which Cloud Foundry uses to bootstrap Java applications. The third one is a collection of factors you should consider when creating a modern cloud-native application.
9
9
10
+
* <ahref="../buildpacks/java/java-tips.html" target="_blank">Tips for Java Applications in the cloud</a>
0 commit comments