Open ShevaXu opened 5 years ago
protoc
(from https://github.com/protocolbuffers/protobuf)#!/usr/bin/env bash
VER=3.6.1
PLATFORM=osx-x86_64
wget "https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-$VER-$PLATFORM.zip"
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u google.golang.org/grpc
protoc --go_out=plugins=grpc:. *.proto
The original project for reference is a mono-repo with services implemented in different languages (Go, Node.js, Java ...). One of my goal is to rewrite them all in Go (might be easier for single-developer or team with few devs) with Go modules (as an experiment).
The first thing to deal with is the project layout w.r.t. go.mod
, and it settled with two separate repos:
go.mod
for services to depend on;go.mod
for build.The services codebase should have no exported pkg for sharing thus their go.mod
s are just for reproductible builds. The core can be imported like this:
import "github.com/practigo/hipstershop/genproto"
So the first to reimplement are the leaves. And shipping/payment are the test-bed for Go/non-Go service migrations.
The core repo just contains the proto file/definition and the generated .go
file for import's sake.
Project Layout
The first thing I had to deal with is the project layout w.r.t.
go.mod
, and end up with two separate repos:
I talked with the member from the original repo and got some useful feedback:
replace
directive locally as maintainers, and remove it before commit;Technically adding the replace
into go.mod
in VCS should work (across dev team and on build system); and I found this on Go module wiki:
replace
also can be used to inform the go tooling of the relative or absolute on-disk location of modules in a multi-module project, such as:replace example.com/project/foo => ../foo
And more about multi-mods-repo here.
The original shipping codebase is in Go so I just got rid of all the tracing codes to make it go build
-able. And then
go mod init github.com/practigo/hipstershop/shippingservice
To debug the gPRC (make a call), I found BloomRPC quite useful (GUI like Postman but for gRPC). Other tools like gRPCurl also worth trying.
The original payment service is in Node.js. With the shipping service in Go already, migrating one from another language turned out easy. Actually simply replacing the shipping proto implement (from main.go) with the payment's interfaces provided a runnable boilerplate already. That's one good thing about gPRC - the generated codes give both servers and clients. (Consider adding a tool to generate boilerplate directly from the proto or proto-generated go file.)
The Charge
method is the only one to implement. It depends on a credit card checking module and I found some equivalents in Go (e.g., github.com/durango/go-credit-card). The sub-pkg creditcard
serves as a wrapper that just returns a mock UUID as transaction ID for now but can connect to real charging service in the future.
Introduction
The Hipster Shop at https://github.com/GoogleCloudPlatform/microservices-demo is a great demo for learning microservices, including:
This issue is open for tracking the process of the reimplementation.