浅谈将JAVA与Azure Redis配合使用
你可以使用 http://redis.io/clients 中列出的任何 Java 客户端来进行操作。这里就不一一列举了 。
检索主机名和访问密钥
若要连接到某个 Azure Redis 缓存实例,缓存客户端需要该缓存的主机名、端口和密钥。 在某些客户端中,这些项的名称可能略有不同。 可以在 Azure 门户中检索该信息,也可以通过命令行工具(例如 Azure CLI)来检索。
使用 Azure 门户检索主机名、端口和访问密钥
若要使用 Azure 门户检索主机名、端口和访问密钥,请浏览到 Azure 门户中的缓存, 然后在“资源”菜单中单击“访问密钥”和“属性”。
使用 Azure CLI 检索主机名、端口和访问密钥
若要使用 Azure CLI 2.0 检索主机名和端口,可调用 az redis show;若要检索密钥,可调用 az redis list-keys。 以下脚本调用这两个命令,并将主机名、端口和密钥回显到控制台。
Azure CLI
复制
#/bin/bash
# Retrieve the hostname, ports, and keys for contosoCache located in contosoGroup
# Retrieve the hostname and ports for an Azure Redis Cache instance
redis=($(az redis show --name contosoCache --resource-group contosoGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))
# Retrieve the keys for an Azure Redis Cache instance
keys=($(az redis list-keys --name contosoCache --resource-group contosoGroup --query [primaryKey,secondaryKey] --output tsv))
# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}
有关此脚本的详细信息,请参阅获取 Azure Redis 缓存的主机名、端口和密钥。 有关 Azure CLI 2.0 的详细信息,请参阅 Install Azure CLI 2.0(安装 Azure CLI 2.0)和 Get started with Azure CLI 2.0(Azure CLI 2.0 入门)。
使用 SSL 安全地连接到缓存
最新版本的 jedis 支持使用 SSL 连接到 Azure Redis 缓存。以下示例显示了如何使用 SSL 终结点 6380 连接到 Azure Redis 缓存。将 <name> 替换为缓存名称,将 <key> 替换为主密钥或辅助密钥,如前面的检索主机名和访问密钥部分中所述。
复制
boolean useSsl = true;
/* In this line, replace <name> with your cache name: */
JedisShardInfo shardInfo = new JedisShardInfo("<name>.redis.cache.chinacloudapi.cn", 6380, useSsl);
shardInfo.setPassword("<key>"); /* Use your access key. */
Note
为新的 Azure Redis 缓存实例禁用了非 SSL 端口。如果使用的是不支持 SSL 的不同客户端,请参阅如何启用非 SSL 端口。
在缓存中添加一些内容并检索此内容
复制
package com.mycompany.app;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
public class App
{
public static void main( String[] args )
{
boolean useSsl = true;
/* In this line, replace <name> with your cache name: */
JedisShardInfo shardInfo = new JedisShardInfo("<name>.redis.cache.chinacloudapi.cn", 6380, useSsl);
shardInfo.setPassword("<key>"); /* Use your access key. */
Jedis jedis = new Jedis(shardInfo);
jedis.set("foo", "bar");
String value = jedis.get("foo");
}
}
关于如何在 Azure 上创建 Redis 缓存以及操作界面截图,大家可以点击这里阅读。
检索主机名和访问密钥
若要连接到某个 Azure Redis 缓存实例,缓存客户端需要该缓存的主机名、端口和密钥。 在某些客户端中,这些项的名称可能略有不同。 可以在 Azure 门户中检索该信息,也可以通过命令行工具(例如 Azure CLI)来检索。
使用 Azure 门户检索主机名、端口和访问密钥
若要使用 Azure 门户检索主机名、端口和访问密钥,请浏览到 Azure 门户中的缓存, 然后在“资源”菜单中单击“访问密钥”和“属性”。
使用 Azure CLI 检索主机名、端口和访问密钥
若要使用 Azure CLI 2.0 检索主机名和端口,可调用 az redis show;若要检索密钥,可调用 az redis list-keys。 以下脚本调用这两个命令,并将主机名、端口和密钥回显到控制台。
Azure CLI
复制
#/bin/bash
# Retrieve the hostname, ports, and keys for contosoCache located in contosoGroup
# Retrieve the hostname and ports for an Azure Redis Cache instance
redis=($(az redis show --name contosoCache --resource-group contosoGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))
# Retrieve the keys for an Azure Redis Cache instance
keys=($(az redis list-keys --name contosoCache --resource-group contosoGroup --query [primaryKey,secondaryKey] --output tsv))
# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}
有关此脚本的详细信息,请参阅获取 Azure Redis 缓存的主机名、端口和密钥。 有关 Azure CLI 2.0 的详细信息,请参阅 Install Azure CLI 2.0(安装 Azure CLI 2.0)和 Get started with Azure CLI 2.0(Azure CLI 2.0 入门)。
使用 SSL 安全地连接到缓存
最新版本的 jedis 支持使用 SSL 连接到 Azure Redis 缓存。以下示例显示了如何使用 SSL 终结点 6380 连接到 Azure Redis 缓存。将 <name> 替换为缓存名称,将 <key> 替换为主密钥或辅助密钥,如前面的检索主机名和访问密钥部分中所述。
复制
boolean useSsl = true;
/* In this line, replace <name> with your cache name: */
JedisShardInfo shardInfo = new JedisShardInfo("<name>.redis.cache.chinacloudapi.cn", 6380, useSsl);
shardInfo.setPassword("<key>"); /* Use your access key. */
Note
为新的 Azure Redis 缓存实例禁用了非 SSL 端口。如果使用的是不支持 SSL 的不同客户端,请参阅如何启用非 SSL 端口。
在缓存中添加一些内容并检索此内容
复制
package com.mycompany.app;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
public class App
{
public static void main( String[] args )
{
boolean useSsl = true;
/* In this line, replace <name> with your cache name: */
JedisShardInfo shardInfo = new JedisShardInfo("<name>.redis.cache.chinacloudapi.cn", 6380, useSsl);
shardInfo.setPassword("<key>"); /* Use your access key. */
Jedis jedis = new Jedis(shardInfo);
jedis.set("foo", "bar");
String value = jedis.get("foo");
}
}
关于如何在 Azure 上创建 Redis 缓存以及操作界面截图,大家可以点击这里阅读。
...全文
相关推荐
SpringAI接入与使用[源码]
该框架深度整合了Spring Boot 3.x的响应式编程模型与Spring Framework 6.x的核心特性,要求运行环境必须基于JDK 17或更高版本,这是由于其底层大量使用了Java 17引入的密封类
Spring AI 玩转 Azure OpenAI 向量模型:从零配置到实战案例
在项目中接入 Azure OpenAI 向量模型,配置复杂又容易踩坑?别慌!本文小米将带你一步步用 Spring AI 跑通 Embedding,从前置条件到自动配置、示例代码,再到手动调优,全流程讲解,帮你快速上手,轻松搞定语义搜索与向量检索。
简历聊天机器人:用Azure+LangChain构建可对话的职业知识体
简历本质上是结构化职业知识的载体,但传统PDF格式导致信息检索低效、表达失真。基于大语言模型(LLM)与向量检索的技术组合,可将静态简历升级为支持自然语言问答、上下文感知与来源可溯的动态知识系统。其核心原理在于语义切分、混合搜索(关键词+向量)、约束式提示工程与工业级AI服务编排。该方案显著提升技术岗求职者项目细节表达力、面试官真实性验证效率及HR隐性能力识别精度,已在后端、算法、数据工程等高复杂度岗位落地验证。本文详解如何利用Azure SQL Server、LangChain、Azure AI Sear
JAVA核心知识点全集
加密算法如AES、RSA等保护数据安全,分布式缓存如Redis和Memcached提高数据访问速度。
java编程岗位职责2篇.docx
*沟通和表达能力:具有良好的沟通和表达能力,以便更好地与团队成员和客户合作。Java编程岗位职责是一个复杂的岗位,要求具备广泛的技术知识和技能。


非技术区
