您好,我正在尝试使.NET Core
服务器与Redis
数据库通信。服务器在容器外运行良好。
容器已设置好。我可以使用主机端口映射访问数据库。
但是,当我执行需要服务器调用数据库的操作时,连接被拒绝。
码头工人撰写
version: '3.3'
services:
db:
image: redis:4.0.5-alpine
container_name: redis0
networks:
- redis-net
ports:
- 6381:6379 //i can acess redis from the hostmachine via 6381 but i can't acess it via 6379 from the container with the server
backend:
image: server
container_name: server0
build: ./Server
command: ["dotnet","Server.dll"]
ports:
- 9400:9300
networks:
- redis-net
environment:
- dbport=6379
depends_on:
- db
networks:
redis-net:
启动
public void ConfigureServices(IServiceCollection services) {
services.AddOptions();
services.AddMvc();
try {
var env = Environment.GetEnvironmentVariables();
var portStr = env["dbport"].ToString();
System.IO.File.AppendAllText("dock.txt",$"dbport:{portStr}");
if (!(int.TryParse(portStr,out int redisPort))) {
return;
}
var mux = new Multiplexer();
var logicalConnection = mux.OpenLink(redius.Address.Create(redisPort));
services.AddSingleton(logicalConnection);
} catch (Exception ex) {
Console.WriteLine("Could not open link, ex:" + ex.Message);
}
}
为了调试,我正在编写exception
服务器在容器中的文件中抛出的内容,我使用docker exec -it <container_id> bash
抛出的控制器方法
public async Task<List<User>> GetUsers()
{
try
{
var usersRaw = await this.connection.HMGetAllAsync("users");
}
catch (Exception ex)
{
System.IO.File.AppendAllText(Constants.OUTPUT,ex.Message);
return null;
}
return null;
}
错误说:connection refused for 127.0.0.1:6379
.ip
地址错误吗?基本上问题如下:
Redis Server
HostPort 6381 9400
ContainerPort 6379 9300
AppPort 6379 9300
我可以通过外部访问 Redis,6381
也可以Server
通过9400
. 服务器可以与Redis
两个外部容器通信(因此没有专有库故障)。
无法Redis
从Server
via访问6379
(都在network
->中redis-net
)