grpc: No connection could be made because the target machine actively refused it
Всем привет!
Есть вот такой сервер https://github.com/3110Y/cc-profile/blob/init/cmd/main.go
package main
import (
"fmt"
"github.com/3110Y/profile/internal/infrastructure/di"
"github.com/3110Y/profile/pkg/profileGRPC"
"github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"log"
"net"
"os"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%s", os.Getenv("GRPC_PORT")))
if err != nil {
grpclog.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
initializeDI, err := di.InitializeDI()
if err != nil {
grpclog.Fatalf("failed", err)
}
profileGRPC.RegisterProfileServiceServer(grpcServer, initializeDI.ProfileRPC)
fmt.Println(grpcServer.GetServiceInfo())
err = grpcServer.Serve(listener)
if err != nil {
grpclog.Fatalf("failed to listen: %v", err)
}
}
и есть вот такой клиент https://github.com/3110Y/cc-profile/blob/init/client/client.go
package main
import (
"context"
"fmt"
"github.com/3110Y/profile/pkg/profileGRPC"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/grpclog"
)
func main() {
conn, err := grpc.Dial("127.0.0.1:5300", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
grpclog.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := profileGRPC.NewProfileServiceClient(conn)
request := &profileGRPC.ProfileWithoutIdSystemField{
Email: "testtesttest",
Phone: 611,
Password: "Password",
Surname: "Surname",
Name: "Name",
Patronymic: "Patronymic",
}
response, err := client.Add(context.Background(), request)
if err != nil {
grpclog.Fatalf("fail to client: %v", err)
}
fmt.Println(response)
}
docker-compose https://github.com/3110Y/cc-profile/blob/init/docker-compose.yml
При попытке сделать запрос, выдает
2023/11/05 19:13:30 FATAL: fail to client: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial tcp 127.0.0.1:5300: connectex: No connection could be made because the target machine actively refused it."
код самого проекта https://github.com/3110Y/cc-profile/tree/init
Адреса совпадают, в docker-compose проброс есть.
ports:
- ${GRPC_PORT}:${GRPC_PORT}
- "40000:40000"
Не могу понять как поправить.
Ответы (1 шт):
Мне помогло
Right-click on your PC Properties Advanced system settings Environment Variables Add new Variable: DOCKER_HOST Value: tcp://127.0.0.1:2375 Run Docker Desktop bottom-right, right-click on Docker-Desktop Settings Enable the following option: "Expose daemon on tcp://localhost:2375 without TLS"