lorem_hystrix

package
v0.0.0-...-d5bb415 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2017 License: GPL-3.0 Imports: 20 Imported by: 2

README

lorem-hystrix

This is simple service module. Only for showing the micro service with HTTP and return json. The purpose for this service is only generating lorem ipsum paragraph and return the payload.

In this part I will demonstrate the circuit breaker patter. I copied from lorem-consul.

I am fully using all three functions from the golorem library.

Required libraries

go get github.com/go-kit/kit
go get github.com/drhodes/golorem
go get github.com/gorilla/mux
go get github.com/juju/ratelimit
go get github.com/prometheus/client_golang/prometheus
go get github.com/go-kit/kit/sd/consul
go get github.com/afex/hystrix-go/hystrix
service.go

Business logic will be put here

endpoint.go

Endpoint will be created here

transport.go

Handling about encode and decode json

logging.go

Logging function is under this file

instrument.go

Middleware function. For this sample, this function for rate limiting and metrics.

discovery.go

Consul service registration utility

lorem-consul.d

Go main function to build service and register to consul

discover.d

Go main function for discover service

Running Consul
docker run --rm -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul -server -bootstrap -ui-dir /ui
Running hystrix dashboard

The dashboard is running on http://localhost:8181/hystrix

docker run -p 8181:9002 --name hystrix-dashboard mlabouardy/hystrix-dashboard:latest
Running Prometheus and Grafana

To execute type

cd $GOPATH/src/github.com/ru-rocker/gokit-playground
docker-compose -f docker/docker-compose-prometheus-grafana-consul.yml up -d
execute
cd $GOPATH/src/github.com/ru-rocker/gokit-playground
go run lorem-hystrix/lorem-hystrix.d/main.go -consul.addr localhost -consul.port 8500 -advertise.addr 192.168.1.103 -advertise.port 7002
go run lorem-hystrix/discover.d/main.go -consul.addr localhost -consul.port 8500
execute request in forever loop
while true; do curl -XPOST -d'{"requestType":"word", "min":10, "max":10}' http://localhost:8080/sd-lorem; sleep 1; done;
while true; do curl -XPOST -d'{"requestType":"sentence", "min":10, "max":10}' http://localhost:8080/sd-lorem; sleep 1; done;
while true; do curl -XPOST -d'{"requestType":"paragraph", "min":10, "max":10}' http://localhost:8080/sd-lorem; sleep 1; done;

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadRouting is returned when an expected path variable is missing.
	ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)")
)
View Source
var (
	ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph")
)

Functions

func DecodeLoremRequest

func DecodeLoremRequest(_ context.Context, r *http.Request) (interface{}, error)

decode url path variables into request

func EncodeResponse

func EncodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error

encodeResponse is the common method to encode all response types to the client.

func Hystrix

func Hystrix(commandName string, fallbackMesg string, logger log.Logger) endpoint.Middleware

func MakeHealthEndpoint

func MakeHealthEndpoint(svc Service) endpoint.Endpoint

creating health endpoint

func MakeHttpHandler

func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler

Make Http Handler

func MakeLoremLoggingEndpoint

func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint

creating Lorem Ipsum Endpoint

func Register

func Register(consulAddress string,
	consulPort string,
	advertiseAddress string,
	advertisePort string) (registar sd.Registrar)

Types

type Endpoints

type Endpoints struct {
	LoremEndpoint  endpoint.Endpoint
	HealthEndpoint endpoint.Endpoint
}

endpoints wrapper

type HealthRequest

type HealthRequest struct {
}

Health Request

type HealthResponse

type HealthResponse struct {
	Status bool `json:"status"`
}

Health Response

type LoremRequest

type LoremRequest struct {
	RequestType string `json:"requestType"`
	Min         int    `json:"min"`
	Max         int    `json:"max"`
}

Lorem Request

type LoremResponse

type LoremResponse struct {
	Message string `json:"message"`
	Err     error  `json:"err,omitempty"`
}

Lorem Response

type LoremService

type LoremService struct {
}

Implement service with empty struct

func (LoremService) HealthCheck

func (LoremService) HealthCheck() bool

func (LoremService) Paragraph

func (LoremService) Paragraph(min, max int) string

func (LoremService) Sentence

func (LoremService) Sentence(min, max int) string

func (LoremService) Word

func (LoremService) Word(min, max int) string

Implement service functions

type Service

type Service interface {
	// generate a word with at least min letters and at most max letters.
	Word(min, max int) string

	// generate a sentence with at least min words and at most max words.
	Sentence(min, max int) string

	// generate a paragraph with at least min sentences and at most max sentences.
	Paragraph(min, max int) string

	// health check
	HealthCheck() bool
}

Define service interface

type ServiceMiddleware

type ServiceMiddleware func(Service) Service

create type that return function. this will be needed in main.go

func LoggingMiddleware

func LoggingMiddleware(logger log.Logger) ServiceMiddleware

implement function to return ServiceMiddleware

func Metrics

func Metrics(requestCount metrics.Counter,
	requestLatency metrics.Histogram) ServiceMiddleware

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL