Categorygithub.com/billnote/fargo
modulepackage
1.5.2
Repository: https://github.com/billnote/fargo.git
Documentation: pkg.go.dev

# README

fargo

Netflix Eureka client in golang. Named for the show Eureka.

c = fargo.NewConn("http://127.0.0.1:8080/eureka/v2")
c.GetApps() // returns a map[String]fargo.Application

Testing

Tests can be executed using docker container. See the below section to build and start all the required containers. Once the Eureka containers are running, and fargo image is built then you can run the command as follows:

Run:

docker run --rm -v "$PWD":/go/src/github.com/hudl/fargo -w /go/src/github.com/hudl/fargo hudloss/fargo:master go test -v ./...

Note: If you are running bash for Windows add MSYS_NO_PATHCONV=1 at the beginning.

The tests may need to be run a couple of times to allow changes to propagate between the two Eureka servers. If the tests are failing, try running them again approximately 30 seconds later.

If you are adding new packages to godep you may want to update the hudloss/fargo image first.

Known Issues

Until this PR is in an official vagrant release, the Fedora 19 opscode box will fail when setting up private networks in vagrant 1.2.5 and later.

FAQ

Q: Is this a full client?

A: Not yet. It's very much a work in progress, and it's also being built with consideration for Go idioms, which means some Java-isms will never be included.

Q: Does it cache?

A: No, it does not support caching records.

Q: Can I integrate this into my Go app and have it manage hearbeats to Eureka?

A: Glad you asked, of course you can. Just grab an application (for this example, "TESTAPP")

// register a couple instances, and then set up to only heartbeat one of them
e, _ := fargo.NewConnFromConfigFile("/etc/fargo.gcfg")
app, _ := e.GetApp("TESTAPP")
// starts a goroutine that updates the application on poll interval
e.UpdateApp(&app)
for {
    for _, ins := range app.Instances {
        fmt.Printf("%s, ", ins.HostName)
    }
    fmt.Println(len(app.Instances))
    <-time.After(10 * time.Second)
}
// You'll see all the instances at first, and within a minute or two all the
// ones that aren't heartbeating will disappear from the list. Note that after
// calling `UpdateApp` there's no need to manually update

TODO

  • Actually do something with AWS availability zone info
  • Currently the load balancing is random, and does not give preference to servers within a zone.
  • Make releases available on gopkg.in

Hacking

Just Let Me Import Already

go get github.com/hudl/fargo


package main

import (
    "github.com/hudl/fargo"
)

func main() {
    e, _ := fargo.NewConnFromConfigFile("/etc/fargo.gcfg")
    e.AppWatchChannel
}

Adding Stuff

go test is your friend. I use the excellent goconvey library in addition to the standard lib's testing package. If you add something, write a test. If something is broken, write a test that reproduces your issue or post repro steps to the issues on this repository. The tests require that you have a eureka install and are designed to run against the included vagrant specs.

Verifying Releases

We're on semver and tag releases accordingly. The releases are signed and can be verified with git tag --verify vA.B.C.

Using Docker

Fargo is tested against two eureka versions, v1.1.147 and v1.3.1. To support testing, we provide Docker containers that supply Eureka locally. Here's how to get started.

  1. Clone Fargo
  2. If you don't have it, install Docker.
  3. Change into the docker directory of this repository.
# Build the image, change "1.3.1" to 1.1.147 for the older version
VERSION=1.3.1
docker build -f Dockerfile-v$VERSION -t hudloss/eureka:$VERSION .
# Run two copies of the image (the containers will communicate with each other)
docker run -d --name eureka1 hudloss/eureka:$VERSION
docker run -d --name eureka2 hudloss/eureka:$VERSION

docker build -f Dockerfile-fargo-master -t hudloss/fargo:master .

# Run fargo container which launches shell
docker run --name fargo -it hudloss/fargo:master
# Once shell is attached, checkout your branch and run `go test ./...`

Once all containers are running, please check that the IP addresses for eureka1 and eureka2 are 172.17.0.2 and 172.17.0.3. Eureka apps will be available at those IPs on port 8080 from inside containers. To expose the ports to your local machine, start containers using:

# forward port 8080 to local port 48000
docker run -d -p 48000:8080 --name eureka1 hudloss/eureka:1.1.147
# forward port 8080 to local port 49000
docker run -d -p 49000:8080 --name eureka2 hudloss/eureka:1.3.1

Contributors

  • Ryan S. Brown (ryansb)
  • Carl Quinn (cquinn)

MIT License

The MIT License (MIT)

Copyright (c) 2013 Hudl <@Hudl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

# Packages

No description provided by the author

# Functions

HTTPResponseStatusCode extracts the HTTP status code for the response from Eureka that motivated the supplied error, if any.
NewConn is a default connection with just a list of ServiceUrls.
NewConnFromConfig will, given a Config struct, return a connection based on those options.
NewConnFromConfigFile sets up a connection object based on a config in specified path.
NewInstanceFromConfig will, given a Config struct, return a Instance based on.
ReadConfig from a file location.
Shuffled requests randomizing the order of the sequence of instances returned, using the default shared rand.Source.
ShuffledWith requests randomizing the order of the sequence of instances returned, using the supplied source of random numbers.
ThatAreUp restricts the set of instances returned to only those with status UP.
WithStatus restricts the set of instances returned to only those with the given status.

# Constants

Datacenter names.
Supported statuses.
Datacenter names.
Supported statuses.
Supported statuses.
Supported statuses.
Supported statuses.

# Variables

EurekaURLSlugs is a map of resource names->Eureka URLs.
No description provided by the author

# Structs

AmazonMetadataType is information about AZ's, AMI's, and the AWS instance.
Application deserializeable from Eureka XML.
No description provided by the author
An AppSource holds a periodically updated copy of a Eureka application.
AppUpdate is the outcome of an attempt to get a fresh snapshot of a Eureka application's state, together with an error that may have occurred in that attempt.
Config is a base struct to be read by code.google.com/p/gcfg.
DataCenterInfo indicates which type of data center hosts this instance and conveys details about the instance's environment.
EurekaConnection is the settings required to make Eureka requests.
GetAppResponseJson wraps an Application for deserializing from Eureka JSON.
GetAppsResponse lets us deserialize the eureka/v2/apps response XML.
GetAppsResponseJson lets us deserialize the eureka/v2/apps response JSON—a wrapped GetAppsResponse.
Instance [de]serializeable [to|from] Eureka [XML|JSON].
InstanceMetadata represents the eureka metadata, which is arbitrary XML.
An InstanceSetSource holds a periodically updated set of instances registered with Eureka.
InstanceSetUpdate is the outcome of an attempt to get a fresh snapshot of a Eureka VIP address's set of instances, together with an error that may have occurred in that attempt.
LeaseInfo tells us about the renewal from Eureka, including how old it is.
RegisterInstanceJson lets us serialize the eureka/v2/apps/<ins> request JSON—a wrapped Instance.

# Type aliases

InstanceQueryOption is a customization supplied to instance query functions like GetInstancesByVIPAddress to tailor the set of instances returned.
StatusType is an enum of the different statuses allowed by Eureka.