_ ____
| | __ ___ ____ _ __ _____ / ___| ___
_ | |/ _` \ \ / / _` | \ \ / / __| | | _ / _ \
| |_| | (_| |\ V / (_| | \ V /\__ \ | |_| | (_) |
\___/ \__,_| \_/ \__,_| \_/ |___/ \____|\___/
- Go version >= 1.12
- Plugin for Go in your favorite IDE (eg. GoLand for Intellij)
- ab tool for load testing
- Docker version >= 2.x
-
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`. -
Create directory and file for your application code
mkdir cmd; cd cmd touch go_sample_service.go
-
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!") }
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 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
Generate some load and measure memory usage.
git checkout memory_usage
Instructions:
- Run the service:
go run cmd/go_sample_service.go
-
Open Activity Monitor and find process called
go_sample_service
. -
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
- Generate some load on application:
ab -T 'application/json' -p 'docs/assets/sample_payload.json' -n 10000 -c 100 http://localhost:8080/echo-request
Proxy to httpbin
service:
- the service exposes one endpoint:
/httpbin/json
- the endpoint rewrites response returned by the following endpoint: http://httpbin.org/#/Response_formats/get_json.
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
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
Build docker image with the service, and verify it's size.
Checkout source code:
git checkout docker_image
Instructions:
-
Build image for this service:
docker build -t go_sample_service:1.0.0 -f assets/Dockerfile_service_go ./
-
Verify the service image size:
docker images | grep go_sample_service
-
Run the service image:
docker run -p 8080:8080 go_sample_service:1.0.0
-
Curl the service (or open in the browser):
curl http://localhost:8080/httpbin/json