お世話になってります。
ローカル環境にてGolang+grpc-gatewayで個人開発を行っています。
Dockerを使わずにGoサーバーとgatewayサーバー間ではRESTapi通信を行うことに成功したのですがdocker-composeに変えたところ
{ "error": "connection error: desc = \"transport: Error while dialing dial tcp 127.0.0.1:8080: connect: connection refused\"", "code": 14, "message": "connection error: desc = \"transport: Error while dialing dial tcp 127.0.0.1:8080: connect: connection refused\"" }
上記のエラーがでるようになりました。Goサーバー、gatewayサーバーのロジック変更はしていないのでDockerfile,またはdocker-compose.ymlの設定が間違っていると思われるのですが解決できず苦戦しております。
エラー解決をお手伝いして頂ける方を探しています。
docker-compose.yml
version: '3' services: shop_grpc: build: context: ./backend/server/shopping dockerfile: ../docker/Dockerfile.shopping target: dev stdin_open: true tty: true ports: - 8080:8080 volumes: - ./backend/server:/go/src/shopping/backend/server gateway: build: context: ./backend/server/gateway dockerfile: ../docker/Dockerfile.gateway target: dev container_name: gateway stdin_open: true tty: true ports: - 9999:9999 volumes: - ./backend/server:/go/src/shopping/backend/server
gatewayサーバのDockerfile
FROM golang:latest as dev COPY . . WORKDIR /go/src/shopping/backend/server/gateway EXPOSE 9999 CMD ["go", "run", "main.go"]
GoサーバのDockerfile
FROM golang:latest as dev COPY . . WORKDIR /go/src/shopping/backend/server/shopping EXPOSE 8080 CMD ["go", "run", "main.go"]
gateway/main.go
package main import ( "flag" "fmt" "net/http" "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/runtime" pw_test "github.com/shimo0108/shopping/backend/server/pb/test" "golang.org/x/net/context" "google.golang.org/grpc" ) func run() error { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() mux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithInsecure()} endpoint := fmt.Sprintf("localhost:8080") err := pw_test.RegisterTestServiceHandlerFromEndpoint(ctx, mux, endpoint, opts) if err != nil { fmt.Printf("serve: %v\n", err) } return http.ListenAndServe(":9999", mux) } func main() { flag.Parse() defer glog.Flush() if err := run(); err != nil { glog.Fatal(err) } }
shopping/main.go
package main import ( "context" "fmt" "log" "net" pb "github.com/shimo0108/shopping/backend/server/pb/test" "google.golang.org/grpc" ) type TestService struct { pb.UnimplementedTestServiceServer } func main() { fmt.Println("Go gRPC Beginners Tutorial!") lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 8080)) if err != nil { log.Fatalf("failed to listen: %v", err) } grpcServer := grpc.NewServer() pb.RegisterTestServiceServer(grpcServer, &TestService{}) log.Printf("gRPC server start") if err := grpcServer.Serve(lis); err != nil { log.Fatalf("failed to serve: %s", err) } } func (grpcServer *TestService) Test(ctx context.Context, message *pb.TestRequest) (*pb.TestResponse, error) { log.Printf("Received: %v", message.Message) return &pb.TestResponse{Message: "Test " + message.Message}, nil }
% docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a9b54e5f7683 shopping_shop_grpc "go run main.go" 43 seconds ago Up 42 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp shopping_shop_grpc_1 baa29a1015c8 shopping_gateway "go run main.go" 43 seconds ago Up 42 seconds 0.0.0.0:9999->9999/tcp, :::9999->9999/tcp gateway
% docker network ls NETWORK ID NAME DRIVER SCOPE b2222a151caa _default bridge local 6479de7b885b bridge bridge local dbf48ebd65ef host host local 9b2af41eb40b none null local d4dc05738338 shopping_default bridge local
dockerを使わずhttp://localhost:9999/test?message=success
を叩いた場合
{ "message": "Test success" }
docker-composeを起動しhttp://localhost:9999/test?message=success
を叩いた場合
{ "error": "connection error: desc = \"transport: Error while dialing dial tcp 127.0.0.1:8080: connect: connection refused\"", "code": 14, "message": "connection error: desc = \"transport: Error while dialing dial tcp 127.0.0.1:8080: connect: connection refused\"" }
エラー解決にご協力いただけると幸いです。よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。