Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this quickstart, you'll enhance a basic Go console application to dynamically refresh configuration from Azure App Configuration. This allows your application to pick up configuration changes without requiring a restart.
Prerequisites
- Complete the Quickstart: Create a Go console app with Azure App Configuration as the starting point for this quickstart
Reload data from App Configuration
Open the file
appconfig.go
. Inside theloadAzureAppConfiguration
function, update theoptions
to enable refresh. Go provider will reload the entire configuration whenever it detects a change in any of the selected key-values (those starting with Config. and having no label). For more information about monitoring configuration changes, see Best practices for configuration refresh.options := &azureappconfiguration.Options{ Selectors: []azureappconfiguration.Selector{ { KeyFilter: "Config.*", }, }, TrimKeyPrefixes: []string{"Config."}, RefreshOptions: azureappconfiguration.KeyValueRefreshOptions{ Enabled: true, }, }
Tip
You can set the
Interval
property of theRefreshOptions
to specify the minimum time between configuration refreshes. In this example, you use the default value of 30 seconds. Adjust to a higher value if you need to reduce the number of requests made to your App Configuration store.Open the file
unmarshal_sample.go
and add the following code to your main function:// Existing code in unmarshal_sample.go // ... ... fmt.Printf("Timeout: %d seconds\n", config.App.Settings.Timeout) fmt.Printf("Retry Count: %d\n", config.App.Settings.RetryCount) // Register refresh callback to update and display the configuration provider.OnRefreshSuccess(func() { // Re-unmarshal the configuration err := appCfgProvider.Unmarshal(&updatedConfig, nil) if err != nil { log.Printf("Error unmarshalling updated configuration: %s", err) return } // Display the updated configuration displayConfig(config) }) // Setup a channel to listen for termination signals done := make(chan os.Signal, 1) signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) fmt.Println("\nWaiting for configuration changes...") fmt.Println("(Update values in Azure App Configuration to see refresh in action)") fmt.Println("Press Ctrl+C to exit") // Start a ticker to periodically trigger refresh ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() // Keep the application running until terminated for { select { case <-ticker.C: // Trigger refresh in background go func() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := provider.Refresh(ctx); err != nil { log.Printf("Error refreshing configuration: %s", err) } }() case <-done: fmt.Println("\nExiting...") return } }
Run the application
Run your application:
go run unmarshal_sample.go
Keep the application running.
Navigate to your App Configuration store and update the value of the
Config.Message
key.Key Value Content type Config.Message Hello World - updated! Leave empty Observe your console application - within 30 seconds, it should detect the change and display the updated configuration.
Clean up resources
If you don't want to continue using the resources created in this article, delete the resource group you created here to avoid charges.
Important
Deleting a resource group is irreversible. The resource group and all the resources in it are permanently deleted. Ensure that you don't accidentally delete the wrong resource group or resources. If you created the resources for this article inside a resource group that contains other resources you want to keep, delete each resource individually from its respective pane instead of deleting the resource group.
- Sign in to the Azure portal, and select Resource groups.
- In the Filter by name box, enter the name of your resource group.
- In the result list, select the resource group name to see an overview.
- Select Delete resource group.
- You're asked to confirm the deletion of the resource group. Enter the name of your resource group to confirm, and select Delete.
After a few moments, the resource group and all its resources are deleted.