|
| 1 | +--- |
| 2 | +title: Configure settings for the Spring Cloud Configure Server component in Azure Container Apps (preview) |
| 3 | +description: Learn how to configure a Spring Cloud Config Server component for your container app. |
| 4 | +services: container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: container-apps |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 03/13/2024 |
| 9 | +ms.author: cshoe |
| 10 | +--- |
| 11 | + |
| 12 | +# Configure settings for the Spring Cloud Config Server component in Azure Container Apps (preview) |
| 13 | + |
| 14 | +Spring Cloud Config Server provides a centralized location to make configuration data available to multiple applications. Use the following guidance to learn how to configure and manage your Spring Cloud Config Server component. |
| 15 | + |
| 16 | +## Show |
| 17 | + |
| 18 | +You can view the details of an individual component by name using the `show` command. |
| 19 | + |
| 20 | +Before you run the following command, replace placeholders surrounded by `<>` with your values. |
| 21 | + |
| 22 | +```azurecli |
| 23 | +az containerapp env java-component spring-cloud-config show \ |
| 24 | + --environment <ENVIRONMENT_NAME> \ |
| 25 | + --resource-group <RESOURCE_GROUP> \ |
| 26 | + --name <JAVA_COMPONENT_NAME> |
| 27 | +``` |
| 28 | + |
| 29 | +## List |
| 30 | + |
| 31 | +You can list all registered Java components using the `list` command. |
| 32 | + |
| 33 | +Before you run the following command, replace placeholders surrounded by `<>` with your values. |
| 34 | + |
| 35 | +```azurecli |
| 36 | +az containerapp env java-component list \ |
| 37 | + --environment <ENVIRONMENT_NAME> \ |
| 38 | + --resource-group <RESOURCE_GROUP> |
| 39 | +``` |
| 40 | + |
| 41 | +## Bind |
| 42 | + |
| 43 | +Use the `--bind` parameter of the `update` command to create a connection between the Spring Cloud Config Server component and your container app. |
| 44 | + |
| 45 | +Before you run the following command, replace placeholders surrounded by `<>` with your values. |
| 46 | + |
| 47 | +```azurecli |
| 48 | +az containerapp update \ |
| 49 | + --name <CONTAINER_APP_NAME> \ |
| 50 | + --resource-group <RESOURCE_GROUP> \ |
| 51 | + --bind <JAVA_COMPONENT_NAME> |
| 52 | +``` |
| 53 | + |
| 54 | +## Unbind |
| 55 | + |
| 56 | +To break the connection between your container app and the Spring Cloud Config Server component, use the `--unbind` parameter of the `update` command. |
| 57 | + |
| 58 | +Before you run the following command, replace placeholders surrounded by `<>` with your values. |
| 59 | + |
| 60 | +``` azurecli |
| 61 | +az containerapp update \ |
| 62 | + --name <CONTAINER_APP_NAME> \ |
| 63 | + --unbind <JAVA_COMPONENT_NAME> \ |
| 64 | + --resource-group <RESOURCE_GROUP> |
| 65 | +``` |
| 66 | + |
| 67 | +## Configuration options |
| 68 | + |
| 69 | +The `az containerapp update` command uses the `--configuration` parameter to control how the Spring Cloud Config Server is configured. You can use multiple parameters at once as long as they're separated by a space. You can find more details in [Spring Cloud Config Server](https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_spring_cloud_config_server) docs. |
| 70 | + |
| 71 | +The following table lists the different configuration values available. |
| 72 | + |
| 73 | +The following configuration settings are available on the `spring.cloud.config.server.git` configuration property. |
| 74 | + |
| 75 | +| Name | Property path | Description | |
| 76 | +|---|---|---| |
| 77 | +| URI | `repos.{repoName}.uri` | URI of remote repository. | |
| 78 | +| Username | `repos.{repoName}.username` | Username for authentication with remote repository. | |
| 79 | +| Password | `repos.{repoName}.password` | Password for authentication with remote repository. | |
| 80 | +| Search paths | `repos.{repoName}.search-paths` | Search paths to use within local working copy. By default searches only the root. | |
| 81 | +| Force pull | `repos.{repoName}.force-pull` | Flag to indicate that the repository should force pull. If this value is set to `true`, then discard any local changes and take from remote repository. | |
| 82 | +| Default label | `repos.{repoName}.default-label` | The default label used for Git is `main`. If you don't set `default-label` and a branch named `main` doesn't exist, then the config server tries to check out a branch named `master`. To disable the fallback branch behavior, you can set `tryMasterBranch` to `false`. | |
| 83 | +| Try `master` branch | `repos.{repoName}.try-master-branch` | When set to `true`, the config server by default tries to check out a branch named `master`. | |
| 84 | +| Skip SSL validation | `repos.{repoName}.skip-ssl-validation` | The configuration server’s validation of the Git server’s SSL certificate can be disabled by setting the `git.skipSslValidation` property to `true`. | |
| 85 | +| Clone-on-start | `repos.{repoName}.clone-on-start` | Flag to indicate that the repository should be cloned on startup (not on demand). Generally leads to slower startup but faster first query. | |
| 86 | +| Timeout | `repos.{repoName}.timeout` | Timeout (in seconds) for obtaining HTTP or SSH connection (if applicable). Default 5 seconds. | |
| 87 | +| Refresh rate | `repos.{repoName}.refresh-rate` | How often the config server fetches updated configuration data from your Git backend. | |
| 88 | +| Private key | `repos.{repoName}.private-key` | Valid SSH private key. Must be set if `ignore-local-ssh-settings` is `true` and Git URI is SSH format. | |
| 89 | +| Host key | `repos.{repoName}.host-key` | Valid SSH host key. Must be set if `host-key-algorithm` is also set. | |
| 90 | +| Host key algorithm | `repos.{repoName}.host-key-algorithm` | One of `ssh-dss`, `ssh-rsa`, `ssh-ed25519`, `ecdsa-sha2-nistp256`, `ecdsa-sha2-nistp384`, or `ecdsa-sha2-nistp521`. Must be set if `host-key` is also set. | |
| 91 | +| Strict host key checking | `repos.{repoName}.strict-host-key-checking` | `true` or `false`. If `false`, ignore errors with host key. | |
| 92 | +| Repo location | `repos.{repoName}` | URI of remote repository. | |
| 93 | +| Repo name patterns | `repos.{repoName}.pattern` | The pattern format is a comma-separated list of {application}/{profile} names with wildcards. If {application}/{profile} doesn't match any of the patterns, it uses the default URI defined under. | |
| 94 | + |
| 95 | +### Common configurations |
| 96 | + |
| 97 | +- logging related configurations |
| 98 | + - [**logging.level.***](https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-logging.html#boot-features-custom-log-levels) |
| 99 | + - [**logging.group.***](https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-logging.html#boot-features-custom-log-groups) |
| 100 | + - Any other configurations under logging.* namespace should be forbidden, for example, writing log files by using `logging.file` should be forbidden. |
| 101 | + |
| 102 | +- **spring.cloud.config.server.overrides** |
| 103 | + - Extra map for a property source to be sent to all clients unconditionally. |
| 104 | + |
| 105 | +- **spring.cloud.config.override-none** |
| 106 | + - You can change the priority of all overrides in the client to be more like default values, letting applications supply their own values in environment variables or System properties, by setting the spring.cloud.config.override-none=true flag (the default is false) in the remote repository. |
| 107 | + |
| 108 | +- **spring.cloud.config.allow-override** |
| 109 | + - If you enable config first bootstrap, you can allow client applications to override configuration from the config server by placing two properties within the applications configuration coming from the config server. |
| 110 | + |
| 111 | +- **spring.cloud.config.server.health.** |
| 112 | + - You can configure the Health Indicator to check more applications along with custom profiles and custom labels |
| 113 | + |
| 114 | +- **spring.cloud.config.server.accept-empty** |
| 115 | + - You can set `spring.cloud.config.server.accept-empty` to `false` so that the server returns an HTTP `404` status, if the application is not found. By default, this flag is set to `true`. |
| 116 | + |
| 117 | +- **Encryption and decryption (symmetric)** |
| 118 | + - **encrypt.key** |
| 119 | + - It is convenient to use a symmetric key since it is a single property value to configure. |
| 120 | + - **spring.cloud.config.server.encrypt.enabled** |
| 121 | + - You can set this to `false`, to disable server-side decryption. |
| 122 | + |
| 123 | +## Refresh |
| 124 | + |
| 125 | +Services that consume properties need to know about the change before it happens. The default notification method for Spring Cloud Config Server involves manually triggering the refresh event, such as refresh by call `https://<YOUR_CONFIG_CLIENT_HOST_NAME>/actuator/refresh`, which may not be feasible if there are many app instances. |
| 126 | + |
| 127 | +Instead, you can automatically refresh values from Config Server by letting the config client poll for changes based on a refresh internal. Use the following steps to automatically refresh values from Config Server. |
| 128 | + |
| 129 | +1. Register a scheduled task to refresh the context in a given interval, as shown in the following example. |
| 130 | + |
| 131 | + ``` Java |
| 132 | + @Configuration |
| 133 | + @AutoConfigureAfter({RefreshAutoConfiguration.class, RefreshEndpointAutoConfiguration.class}) |
| 134 | + @EnableScheduling |
| 135 | + public class ConfigClientAutoRefreshConfiguration implements SchedulingConfigurer { |
| 136 | + @Value("${spring.cloud.config.refresh-interval:60}") |
| 137 | + private long refreshInterval; |
| 138 | + @Value("${spring.cloud.config.auto-refresh:false}") |
| 139 | + private boolean autoRefresh; |
| 140 | + private final RefreshEndpoint refreshEndpoint; |
| 141 | + public ConfigClientAutoRefreshConfiguration(RefreshEndpoint refreshEndpoint) { |
| 142 | + this.refreshEndpoint = refreshEndpoint; |
| 143 | + } |
| 144 | + @Override |
| 145 | + public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { |
| 146 | + if (autoRefresh) { |
| 147 | + // set minimal refresh interval to 5 seconds |
| 148 | + refreshInterval = Math.max(refreshInterval, 5); |
| 149 | + scheduledTaskRegistrar.addFixedRateTask(refreshEndpoint::refresh, Duration.ofSeconds(refreshInterval)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + ``` |
| 154 | + |
| 155 | +1. Enable `autorefresh` and set the appropriate refresh interval in the *application.yml* file. In the following example, the client polls for a configuration change every 60 seconds, which is the minimum value you can set for a refresh interval. |
| 156 | + |
| 157 | + By default, `autorefresh` is set to `false`, and `refresh-interval` is set to 60 seconds. |
| 158 | + |
| 159 | + ``` yaml |
| 160 | + spring: |
| 161 | + cloud: |
| 162 | + config: |
| 163 | + auto-refresh: true |
| 164 | + refresh-interval: 60 |
| 165 | + management: |
| 166 | + endpoints: |
| 167 | + web: |
| 168 | + exposure: |
| 169 | + include: |
| 170 | + - refresh |
| 171 | + ``` |
| 172 | + |
| 173 | +1. Add `@RefreshScope` in your code. In the following example, the variable `connectTimeout` is automatically refreshed every 60 seconds. |
| 174 | + |
| 175 | + ``` Java |
| 176 | + @RestController |
| 177 | + @RefreshScope |
| 178 | + public class HelloController { |
| 179 | + @Value("${timeout:4000}") |
| 180 | + private String connectTimeout; |
| 181 | + } |
| 182 | + ``` |
| 183 | + |
| 184 | +## Encryption and decryption with a symmetric key |
| 185 | + |
| 186 | +### Server-side decryption |
| 187 | + |
| 188 | +By default, server-side encryption is enabled. Use the following steps to enable decryption in your application. |
| 189 | + |
| 190 | +1. Add the encrypted property in your *.properties* file in your git repository. |
| 191 | + |
| 192 | + For example, your file should resemble the following example: |
| 193 | + |
| 194 | + ``` |
| 195 | + message={cipher}f43e3df3862ab196a4b367624a7d9b581e1c543610da353fbdd2477d60fb282f |
| 196 | + ``` |
| 197 | + |
| 198 | +1. Update the Spring Cloud Config Server Java component to use the git repository that has the encrypted property and set the encryption key. |
| 199 | + |
| 200 | + Before you run the following command, replace placeholders surrounded by `<>` with your values. |
| 201 | + |
| 202 | + ```azurecli |
| 203 | + az containerapp env java-component spring-cloud-config update \ |
| 204 | + --environment <ENVIRONMENT_NAME> \ |
| 205 | + --resource-group <RESOURCE_GROUP> \ |
| 206 | + --name <JAVA_COMPONENT_NAME> \ |
| 207 | + --configuration spring.cloud.config.server.git.uri=<URI> encrypt.key=randomKey |
| 208 | + ``` |
| 209 | + |
| 210 | +### Client-side decryption |
| 211 | + |
| 212 | +You can use client side decryption of properties by following the steps: |
| 213 | + |
| 214 | +1. Add the encrypted property in your `*.properties*` file in your git repository. |
| 215 | + |
| 216 | +1. Update the Spring Cloud Config Server Java component to use the git repository that has the encrypted property and disable server-side decryption. |
| 217 | + |
| 218 | + Before you run the following command, replace placeholders surrounded by `<>` with your values. |
| 219 | + |
| 220 | + ```azurecli |
| 221 | + az containerapp env java-component spring-cloud-config update \ |
| 222 | + --environment <ENVIRONMENT_NAME> \ |
| 223 | + --resource-group <RESOURCE_GROUP> \ |
| 224 | + --name <JAVA_COMPONENT_NAME> \ |
| 225 | + --configuration spring.cloud.config.server.git.uri=<URI> spring.cloud.config.server.encrypt.enabled=false |
| 226 | + ``` |
| 227 | + |
| 228 | +1. In your client app, add the decryption key `ENCRYPT_KEY=randomKey` as an environment variable. |
| 229 | + |
| 230 | + Alternatively, if you include *spring-cloud-starter-bootstrap* on the `classpath`, or set `spring.cloud.bootstrap.enabled=true` as a system property, set `encrypt.key` in `bootstrap.properties`. |
| 231 | + |
| 232 | + Before you run the following command, replace placeholders surrounded by `<>` with your values. |
| 233 | + |
| 234 | + ```azurecli |
| 235 | + az containerapp update \ |
| 236 | + --name <APP_NAME> \ |
| 237 | + --resource-group <RESOURCE_GROUP> \ |
| 238 | + --set-env-vars "ENCRYPT_KEY=randomKey" |
| 239 | + ``` |
| 240 | + |
| 241 | + ``` |
| 242 | + encrypt: |
| 243 | + key: somerandomkey |
| 244 | + ``` |
| 245 | + |
| 246 | +## Next steps |
| 247 | + |
| 248 | +> [!div class="nextstepaction"] |
| 249 | +> [Set up a Spring Cloud Config Server](spring-cloud-config-server.md) |
0 commit comments