eureka

package module
v0.0.0-...-9128365 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2018 License: MIT Imports: 18 Imported by: 0

README

go-eureka-client

Build Status GoDoc Go Report Card

A simple package used for golang developing integrated with eureka.

This package wraps hudl/fargo to communicate with eureka server.

Features now

  • Cooperating with Spring Cloud.
  • Instance register/deregister/heartbeat automatically.
  • Simple http client.

Basic Usage

Register our instance

You should have a eureka server first. Usually you should setup it using Spring Cloud.

Now suppose that I setup a eureka server with url: http://localhost:8761/eureka.

Then my go code likes like:

func main() {
	// New instance with AppName "testapp" and port 9527.
	ins, err := eureka.NewInstanceWithPort("testapp", 9527, "http://localhost:8761/eureka")
	if err != nil {
		panic(err)
	}
	// Run instance. Note that this method will not block.
	if err := ins.Run(); err != nil {
		panic(err)
	}

	// Do anything you want. Maybe setup a http server.
	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		io.Copy(w, strings.NewReader("Hello World"))
	})
	if err := http.ListenAndServe(":9527", nil); err != nil {
		panic(err)
	}
}

Now if you open url http://localhost:8761/, you will see the instance.

After you kill the program(SIGINT and SIGTERM), the instance will deregister automatically. Or you can do it will ins.Stop() manually.

Send request to app
func main() {
	// New a AppClient with AppName "testapp".
	c, err := eureka.NewAppClient("testapp", "http://localhost:8761/eureka")
	if err != nil {
		panic(err)
	}

	// Send GET request to the app with path "hello"
	res, err := c.Get("/hello", nil)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	bs, err := ioutil.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bs))
}

Run the code above. You should get it print Hello World.

You may also construct the AppClient by using ins.GetAppClient("testapp"), if you already have a Instance.

Whatever

The package is not fully tested yet. If you run into any issues, please just raise it.

Issues and PRs are welcomed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoIP4

func AutoIP4() (string, bool)

AutoIP4 fetch the proper IPv4 address.

Still not stable and reliable.

Types

type AppClient

type AppClient struct {
	App string
	// contains filtered or unexported fields
}

AppClient defines http client to send request to specified app instance.

func NewAppClient

func NewAppClient(app string, serviceUrls ...string) (*AppClient, error)

NewAppClient returns a new AppClient.

func NewAppClientWithTimeout

func NewAppClientWithTimeout(app string, timeout time.Duration, serviceUrls ...string) (*AppClient, error)

NewAppClientWithTimeout returns a new AppClient with timeout.

func (*AppClient) Delete

func (ac *AppClient) Delete(path string, headers http.Header) (*http.Response, error)

Delete sends a DELETE request.

func (*AppClient) Get

func (ac *AppClient) Get(path string, headers http.Header) (*http.Response, error)

Get sends a GET request.

func (*AppClient) GetURL

func (ac *AppClient) GetURL() (url string, err error)

GetURL randomly choose one instance of the app and returns its url(format: http://ip:port/). The url is valid only when the err is nil. The err is not nil when there's no instance of the app.

func (*AppClient) Patch

func (ac *AppClient) Patch(path string, body io.Reader, headers http.Header) (*http.Response, error)

Patch sends a Patch request.

func (*AppClient) Post

func (ac *AppClient) Post(path string, body io.Reader, headers http.Header) (*http.Response, error)

Post sends a POST request.

func (*AppClient) Put

func (ac *AppClient) Put(path string, body io.Reader, headers http.Header) (*http.Response, error)

Put sends a Put request.

func (*AppClient) RefreshInstance

func (ac *AppClient) RefreshInstance() error

RefreshInstance refresh the instance list.

type Instance

type Instance struct {
	HostName string
	App      string

	IPAddr     string
	Port       int
	SecurePort int

	// Default 60s
	HeartBeatInterval time.Duration
	// contains filtered or unexported fields
}

Instance defines a simple eureka instance.

func NewInstance

func NewInstance(app string, serviceUrls ...string) (*Instance, error)

NewInstance returns a new Instance with no port enabled.

func NewInstanceWithPort

func NewInstanceWithPort(app string, port int, serviceUrls ...string) (*Instance, error)

NewInstanceWithPort returns a new Instance with one port enabled.

func NewInstanceWithSecurePort

func NewInstanceWithSecurePort(app string, securePort int, serviceUrls ...string) (*Instance, error)

NewInstanceWithSecurePort returns a new Instance with one secure port(HTTPS) enabled.

func (*Instance) GetAppClient

func (ins *Instance) GetAppClient(app string) *AppClient

GetAppClient returns a new AppClient by app name.

func (*Instance) GetAppClientWithTimeout

func (ins *Instance) GetAppClientWithTimeout(app string, timeout time.Duration) *AppClient

GetAppClientWithTimeout returns a new AppClient with timeout by app name.

func (*Instance) IsRunning

func (ins *Instance) IsRunning() bool

IsRunning tells whether the instance is still running.

func (*Instance) Run

func (ins *Instance) Run() error

Run make the instance register to the eureka server. Note that this method will not block.

Also it listens for SIGINT and SIGTERM signal. By Receiving them, it will deregister ths instance from the eureka server.

func (*Instance) SetServiceUrls

func (ins *Instance) SetServiceUrls(serviceUrls ...string)

SetServiceUrls sets the service urls of eureka server.

func (*Instance) Stop

func (ins *Instance) Stop() error

Stop stops the instance. Deregister the instance from the eureka server.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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