{
  "slug": "Hello-world-in-go-micro-as-the-base-of-GoLang-in-a-microservice-architecture-5f6fa75af724",
  "title": "Hello world in go-micro as the base of GoLang in a microservice architecture",
  "subtitle": "What is Go Micro?",
  "excerpt": "What is Go Micro?",
  "date": "2023-05-24",
  "tags": [
    "Go",
    "Microservices",
    "Architecture"
  ],
  "readingTime": "3 min",
  "url": "https://medium.com/@mobinshaterian/hello-world-in-go-micro-as-the-base-of-golang-in-a-microservice-architecture-5f6fa75af724",
  "hero": "https://cdn-images-1.medium.com/max/800/1*WgbtBRJClMV9kR4fRJY86g.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "What is Go Micro?"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://github.com/micro/go-micro\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>“Go Micro</strong></a> is a pluggable RPC based library which provides the fundamental building blocks for writing microservices in Go. The Micro philosophy is “batteries included” with a pluggable architecture. Out of the box, it implements service discovery using consul, communication via http and encoding using proto-rpc or json-rpc.” <a href=\"http://twitter.com/asimaslam\" target=\"_blank\" rel=\"noreferrer noopener\">@asimaslam</a>"
    },
    {
      "type": "paragraph",
      "html": "In this article, I want to install and go-micro from the beginning to make my extensive application."
    },
    {
      "type": "paragraph",
      "html": "The best and easiest way to install Golang is following a formal tutorial. Another way is using snap to install Golang on your ubuntu device."
    },
    {
      "type": "code",
      "lang": "text",
      "code": "cdmkdir gocd gomkdir src"
    },
    {
      "type": "code",
      "lang": "javascript",
      "code": "export PATH=$PATH:/usr/local/go/binexport PATH=$PATH:$(go env GOPATH)/binexport GOPATH=$(go env\nGOPATH)"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "cd ~cat .bashrc# Set the GOPROXY environment variableexport GOPROXY=https://goproxy.io,direct# Set\nenvironment variable allow bypassing the proxy for specified repos (optional)export\nGOPRIVATE=git.mycompany.com,github.com/my/privateexport PATH=\"$PATH:$HOME/.local/bin\"export\nPATH=\"$PATH:$(go env GOPATH)/bin\"export GOPATH=$(go env GOPATH)cat .profileexport\nPATH=$PATH:/usr/local/go/bin"
    },
    {
      "type": "paragraph",
      "html": "Install Protoc"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Run: vim ~/.bash_profile   Add:export GO_PATH=~/goexport PATH=$PATH:/$GO_PATH/binRun:source\n~/.bash_profile"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-micro repository address"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-micro Demo"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-micro CLI"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-micro Dashboard"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Let’s start with Go-micro CLI"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go install github.com/go-micro/cli/cmd/go-micro@latest"
    },
    {
      "type": "paragraph",
      "html": "Generte microservice by command. This work is so beatiful that I can make it easily without any problem to copy hidden files."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go-micro new service helloworld\ncd helloworld\nmake init proto update tidy"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "cd helloworldmake proto tidygo-micro run"
    },
    {
      "type": "paragraph",
      "html": "Send Request with GRPC"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go-micro call helloworld Helloworld.Call '{\"name\": \"John\"}'"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*WgbtBRJClMV9kR4fRJY86g.png",
      "alt": "folder that make with CLI command",
      "caption": "folder that make with CLI command",
      "width": 284,
      "height": 341
    },
    {
      "type": "paragraph",
      "html": "It is so easy to change helloworld.proto and then build"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "make proto"
    },
    {
      "type": "paragraph",
      "html": "Go-micro also has a refresh mode. It means that when you change a file, Go-micro will automatically build the project."
    },
    {
      "type": "paragraph",
      "html": "main.go file of project will be like this."
    },
    {
      "type": "code",
      "lang": "go",
      "code": "func main() {\n    // Create service srv := micro.NewService() srv.Init(micro.Name(service),\n    micro.Version(version),)"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "// Register handler if err := pb.RegisterHelloworldHandler(srv.Server(), new(handler.Helloworld));\nerr != nil {  logger.Fatal(err) } // Run service if err := srv.Run(); err != nil {\nlogger.Fatal(err) }}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "execute go-micro"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go-micro run"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Jaeger"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go-micro new service --jaeger helloworld"
    },
    {
      "type": "paragraph",
      "html": "<code>handler/helloworld.go</code>"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "package helloworld"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "import (\n\"context\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"go-micro.dev/v4/logger\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"helloworld/greeter\"    pb \"helloworld/proto\")"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "type Helloworld struct{\n}"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "func (e *Helloworld) Call(ctx context.Context, req pb.CallRequest, rsp *pb.CallResponse) error {\n    logger.Infof(\"Received Helloworld.Call request: %v\", req)\n    rsp.Msg = greeter.Greet(ctx, req.Name)\n    return nil\n}"
    },
    {
      "type": "paragraph",
      "html": "<code>greeter/greeter.go</code>"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "package greeter"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "import (\n\"context\"\n\"fmt\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"go-micro.dev/v4/cmd/micro/debug/trace\")"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "func Greet(ctx context.Context, name string) string {\n    defer trace.NewSpan(ctx).Finish()\n    return fmt.Sprint(\"Hello \" + name)\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "I want to add jaeger to the go-micro project"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go-micro new service --jaeger helloworld\ncd helloworld\nmake init proto update tidy\ngo-micro run\ngo-micro\ncall helloworld Helloworld.Call '{\"name\": \"John\"}'"
    },
    {
      "type": "code",
      "lang": "yaml",
      "code": "version: \"3.7\"services:\n  # -----------------------------\n  # jaeger servcie\n  # -----------------------------\n  jaeger:\n    image: jaegertracing/all-in-one:1.20\n    ports:\n      - \"1314:6831/udp\"\n      - \"1315:16686\"\n    networks:\n      - backend# -----------------------------# networks# -----------------------------networks:\n  backend:\n    external: true"
    },
    {
      "type": "paragraph",
      "html": "— — — — — — — — — — — — — — — — — — — — — — — — — — —"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.bookstack.cn/read/microservices-in-golang/2.md\" target=\"_blank\" rel=\"noreferrer noopener\">https://www.bookstack.cn/read/microservices-in-golang/1.md</a>"
    },
    {
      "type": "paragraph",
      "html": "— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —"
    },
    {
      "type": "paragraph",
      "html": "Good Example"
    },
    {
      "type": "paragraph",
      "html": "— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Go-micro Make new Project"
    },
    {
      "type": "paragraph",
      "html": "‍‍‍"
    },
    {
      "type": "paragraph",
      "html": "For initial project use below command"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go-micro new service helloworld"
    },
    {
      "type": "paragraph",
      "html": "download the dependency"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go mod tidy go get -u"
    },
    {
      "type": "paragraph",
      "html": "Write the GRPC and then make it"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "make proto"
    },
    {
      "type": "paragraph",
      "html": "Import name of folder base on the path on the git hub in go.mod and import"
    },
    {
      "type": "paragraph",
      "html": "Subscribe to DDIntel <a href=\"https://ddintel.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\">Here</a>."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://ddintel.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>DDIntel</em> </a>captures the more notable pieces from our <a href=\"https://www.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>main site</em></a> and our popular <a href=\"https://medium.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>DDI Medium publication</em></a>. Check us out for more insightful work from our community."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.aitoolverse.com/register\" target=\"_blank\" rel=\"noreferrer noopener\">Register </a>on AItoolverse (alpha) to get 50 DDINs"
    },
    {
      "type": "paragraph",
      "html": "Support DDI AI Art Series: <a href=\"https://heartq.net/collections/ddi-ai-art-series\" target=\"_blank\" rel=\"noreferrer noopener\">https://heartq.net/collections/ddi-ai-art-series</a>"
    },
    {
      "type": "paragraph",
      "html": "Join our network here: <a href=\"https://datadriveninvestor.com/collaborate\" target=\"_blank\" rel=\"noreferrer noopener\">https://datadriveninvestor.com/collaborate</a>"
    },
    {
      "type": "paragraph",
      "html": "Follow us on <a href=\"https://www.linkedin.com/company/data-driven-investor\" target=\"_blank\" rel=\"noreferrer noopener\"><em>LinkedIn</em></a>, <a href=\"https://twitter.com/@DDInvestorHQ\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Twitter</em></a>, <a href=\"https://www.youtube.com/c/datadriveninvestor\" target=\"_blank\" rel=\"noreferrer noopener\"><em>YouTube</em></a>, and <a href=\"https://www.facebook.com/datadriveninvestor\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Facebook</em></a>."
    }
  ]
}
