Skip to content

dpacierpnik/java_vs_go_examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

     _                                 ____
    | | __ ___   ____ _  __   _____   / ___| ___
 _  | |/ _` \ \ / / _` | \ \ / / __| | |  _ / _ \
| |_| | (_| |\ V / (_| |  \ V /\__ \ | |_| | (_) |
 \___/ \__,_| \_/ \__,_|   \_/ |___/  \____|\___/

Prerequisites

  1. Go version >= 1.12
  2. Plugin for Go in your favorite IDE (eg. GoLand for Intellij)
  3. ab tool for load testing
  4. Docker version >= 2.x

Examples

First Go application

  1. Go to the project directory, and execute:

    go mod init com.jamf.services.java_vs_go

    It will initialize go project with `` which describes module name and version of go. It will be also used to manage project dependencies, like pom.xml or `build.gradle`.

  2. Create directory and file for your application code

    mkdir cmd; cd cmd
    touch go_sample_service.go
  3. Paste the following code in go_sample_service.go:

    package main
    
    // public static void main(String[] args) {
    func main() {
      // argsWithProg := os.Args
      // argsWithoutProg := os.Args[1:]
      println("Hello Silesia Java Users!")
    }

Simple HTTP service

Very simple HTTP microservice:

  • the service listens on port 8080
  • the service exposes one endpoint: /hello which returns the following string: Hello Silesia Java Users!

Checkout source code:

git checkout http_service

Run the service:

go run cmd/go_sample_service.go

Echo HTTP service and testing

Echo service:

  • the service exposes endpoint: /echo-request
  • the endpoint returns response, which contains data from the request encoded in JSON format
  • tests for the endpoint

Example of the response:

{
  "queryString": "?test=true",
  "headers": [
    {
      "name": "content-type",
      "value": "application/json"
    },
    {
      "name": "accept",
      "value": "*/*"
    }
  ],
  "body": "body-as-string"
}

Checkout source code:

git checkout echo_service_and_testing

Run tests

go test ./... 

Run tests with coverage

go test ./... -cover

Run the service:

go run cmd/go_sample_service.go

Load on HTTP service + memory usage

Generate some load and measure memory usage.

git checkout memory_usage

Instructions:

  1. Run the service:
go run cmd/go_sample_service.go
  1. Open Activity Monitor and find process called go_sample_service.

  2. Verify if application is working correctly:

curl -H 'Content-Type: application/json' -X POST -d '@docs/assets/sample_payload.json' http://localhost:8080/echo-request
  1. Generate some load on application:
ab -T 'application/json' -p 'docs/assets/sample_payload.json' -n 10000 -c 100 http://localhost:8080/echo-request

Simple HTTP client + stubbing

Proxy to httpbin service:

Checkout source code:

git checkout http_client_and_stubbing

Run tests

go test ./... 

Run tests with coverage

go test ./... -cover

Run the service:

go run cmd/go_sample_service.go

Dependency management with gomod

Refactoring of simple HTTP microservice:

  • add gorilla mux dependency to the project and use it to simplify endpoints development

Checkout source code:

git checkout deps_management

Run the service:

go run cmd/go_sample_service.go

Docker image

Build docker image with the service, and verify it's size.

Checkout source code:

git checkout docker_image

Instructions:

  1. Build image for this service:

    docker build -t go_sample_service:1.0.0 -f assets/Dockerfile_service_go ./
  2. Verify the service image size:

    docker images | grep go_sample_service
  3. Run the service image:

    docker run -p 8080:8080 go_sample_service:1.0.0
  4. Curl the service (or open in the browser):

    curl http://localhost:8080/httpbin/json

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published