page_type | languages | name | description | products | |||
---|---|---|---|---|---|---|---|
sample |
|
Quickstart: Use Azure Cache for Redis in Java with Redisson Redis client and JCache |
Learn how to incorporate Azure Cache for Redis into a Java app using the Redisson Redis client and JCache. |
|
In this quickstart, you incorporate Azure Cache for Redis into a Java app using the Redisson Redis client and JCP standard JCache API to have access to a secure, dedicated cache that is accessible from any application within Azure.
- Azure subscription - create one for free
- Apache Maven
- Use Microsoft Entra ID for cache authentication
Depending on your operating system, add environment variables for your cache's Host name and Primary access key. Open a command prompt, or a terminal window, and set up the following values. Ensure the value for REDIS_CACHE_HOSTNAME
is the fully qualified domain name. This value typically ends with .redis.cache.windows.net
.
set REDIS_CACHE_HOSTNAME=<YOUR_HOST_NAME>
set REDIS_ACCESS_KEY=<YOUR_PRIMARY_ACCESS_KEY>
export REDIS_CACHE_HOSTNAME=<YOUR_HOST_NAME>
export REDIS_ACCESS_KEY=<YOUR_PRIMARY_ACCESS_KEY>
Replace the placeholders with the following values:
<YOUR_HOST_NAME>
: The DNS host name. In the Settings section of the Azure Cache for Redis resource in the Azure portal, select Properties. Select the copy icon to the right of the Host name field.<YOUR_PRIMARY_ACCESS_KEY>
: The primary access key. In the Settings section of the Azure Cache for Redis resource in the Azure portal, select Authentication. Select the copy icon to the right of the Primary field.
Depending on your operating system, add environment variables for your cache's Host name and Username. Open a command prompt, or a terminal window, and set up the following values:
set REDIS_CACHE_HOSTNAME=<YOUR_HOST_NAME>
set REDIS_USER_NAME=<REDIS_USER_NAME>
export REDIS_CACHE_HOSTNAME=<YOUR_HOST_NAME>
export REDIS_USER_NAME=<REDIS_USER_NAME>
Replace the placeholders with the following values:
<YOUR_HOST_NAME>
: The DNS host name, obtained from the Properties section of your Azure Cache for Redis resource in the Azure portal.<REDIS_USER_NAME>
: Object ID of your managed identity or service principal.- You can find this value in the "(PREVIEW) Data Access Configuration" view on your cache resource in the Azure Portal. It appears on the "Redis Users" tab, in the "Username" column.
It is also possible to use DefaultAzureCredential
to provide the identity to be used with the Redis connection. For more details, see Azure authentication with Java and Azure Identity.
Download the sample code to your development PC.
Change directories to the folder containing this sample.
Execute the following Maven command to build and run the app:
mvn compile exec:java -D exec.mainClass=example.demo.App