neureka

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 14 Imported by: 0

README

Neureka

Eureka client library written in Go for CoreLock game

Object Definitions

These are the key structs used by the library:

  • AddressObject: Represents the host information of an application instance.
  • AppObject: Represents application information (simplified version of the data pulled from Eureka, stored in cache).
  • EurekaAppCache: A cache holding a list of applications and their available hosts for fast access.
  • EurekaClientConfig: Client configuration struct — contains everything needed to connect to and interact with the Eureka server.
  • AppResponse: Data structure representing the response returned by Eureka when requesting app data.
  • EurekaAppInfo: Structure representing an application’s metadata when fetched from Eureka.
  • EurekaAppInstance: Represents a full Eureka application instance.
  • EurekaRegisterRequest: Data structure used when sending a registration request to Eureka.

Implemented Methods

  • EurekaRegist: Register your service/application with Eureka.
  • EurekaHeartBeat: Send a heartbeat to renew the registration lease.
  • EurekaGetApp: Retrieve the current state of an application from Eureka.
  • EurekaDeleteApp: Remove/unregister an application instance from Eureka.

Logic Flow

  1. Initialize EurekaClientConfig with settings:

    • EurekaServerAddress: Address of the Eureka server.
    • Authorization: HTTP Auth header for the Eureka server (e.g., Basic KWJDhaDAWIDhndwa=).
    • AppName: Name of your Go service.
    • InstanceDomain: Domain name of your service instance. If empty, defaults to IP.
    • InstanceIp: The IP address of the service instance (external IP if applicable).
    • InstancePort: The port your Go service runs on.
    • InstanceHealthCheckUrl: Health check endpoint.
    • RenewalIntervalInSecs: Time in seconds for lease renewal.
    • DurationInSecs: How long Eureka retains the instance info after lease expiry.
    • AppRefreshSecs: How often (in seconds) to refresh the cache of known apps.
  2. Clean up previous instance (optional but recommended):

    • Call EurekaGetApp to check if already registered.
    • Call EurekaDeleteApp to remove stale registration.
  3. Register current instance using EurekaRegist.

  4. Start background heartbeat to renew registration via EurekaHeartBeat.

  5. Maintain a background cache of other registered applications using EurekaAppCache.

  6. Use GetAppUrl to retrieve service addresses by app name.

Quick Start

Automatic Flow

Use the built-in startup method Start() to handle:

  • Registration
  • Heartbeat
  • Service discovery cache
Steps:
  1. Prepare and populate EurekaClientConfig
  2. Call Start() for single Eureka service instance
  3. Or call StartBatch() if dealing with multiple services

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DropOldInstance

func DropOldInstance(cnf EurekaClientConfig)

delteOldApp 删除旧应用

func EurekaDeleteApp

func EurekaDeleteApp(ul, auth, name, id string) error

EurekaDeleteApp 删除已注册的应用实例

func EurekaHeartBeat

func EurekaHeartBeat(ul, auth, name, id string) error

EurekaHeartBeat 心跳续约

func EurekaRegist

func EurekaRegist(ul string, auth string, e EurekaAppInstance) error

EurekaRegist 注册新的服务

func GetAppUrl

func GetAppUrl(cfname string, name string) (string, error)

GetAppUrl

func GetInnerIp

func GetInnerIp() string

GetInnerIp 获取内网IP

func GetMs

func GetMs() int64

GetMs 获取毫秒时间戳

func HttpDelete

func HttpDelete(ul string, header http.Header, params url.Values, tmout int64) (*http.Response, error)

HttpDelete http Delete 请求

func HttpGet

func HttpGet(ul string, header http.Header, params url.Values, tmout int64) (*http.Response, error)

HttpGet http Get 请求

func HttpPost

func HttpPost(ul string, header http.Header, data []byte, tmout int64) (*http.Response, error)

HttpPost http Post 请求

func HttpPut

func HttpPut(ul string, header http.Header, data []byte, tmout int64) (*http.Response, error)

HttpPut http put 请求

func ShowApps

func ShowApps()

ShowApps

func Start

func Start(cnf EurekaClientConfig, debug bool) error

Start 启动

func StartBatch

func StartBatch(cnfs []EurekaClientConfig, debug bool) error

StartBatch 批量启动

func StartForKeeper

func StartForKeeper(cnf EurekaClientConfig, debug bool)

仅维护应用列表而启动

func StartForKeeperBatch

func StartForKeeperBatch(cnfs []EurekaClientConfig, debug bool)

批量仅维护应用列表而启动

Types

type AddressObject

type AddressObject struct {
	AppName   string
	Scheme    string
	Host      string
	Port      string
	HealthUrl string
}

AddressObject

func GetAnHost

func GetAnHost(cfname string, name string) (AddressObject, error)

GetAppHost

func NewAddress

func NewAddress(name, scheme, host, port, healthUrl string) AddressObject

NewAddress

func (*AddressObject) Check

func (a *AddressObject) Check() bool

Check

func (*AddressObject) Equl

func (a *AddressObject) Equl(addr AddressObject) bool

Equl

func (*AddressObject) Url

func (a *AddressObject) Url() string

Url

type AppAllResponse

type AppAllResponse struct {
	Applications Applications `json:"applications"`
}

AppAllResponse Eureka响应所有应用信息

func EurekaGetAppAll

func EurekaGetAppAll(ul, auth string) (AppAllResponse, error)

EurekaGetAppAll 拉取所有应用

type AppObject

type AppObject struct {
	Name  string
	Hosts []AddressObject
}

AppObject

func NewApp

func NewApp(name string) AppObject

NewApp

func (*AppObject) AddHost

func (a *AppObject) AddHost(scheme, host, port, healthUrl string) bool

AddHost

func (*AppObject) GetAddresses

func (a *AppObject) GetAddresses() []AddressObject

GetAddresses

func (*AppObject) GetAllUrls

func (a *AppObject) GetAllUrls() []string

GetAllUrls

func (*AppObject) GetAnHost

func (a *AppObject) GetAnHost() (AddressObject, error)

GetAnHost

func (*AppObject) GetAnUrl

func (a *AppObject) GetAnUrl() (string, error)

GetAnUrl

func (*AppObject) HasHost

func (a *AppObject) HasHost() bool

HasHost

func (*AppObject) RemoveUnhealthAddress

func (a *AppObject) RemoveUnhealthAddress(addrs []AddressObject)

RemoveUnhealthAddress

type AppResponse

type AppResponse struct {
	Application EurekaAppInfo `json:"application"`
}

AppResponse Eureka响应应用信息

func EurekaGetApp

func EurekaGetApp(ul, auth, name string) (AppResponse, error)

EurekaGetApp 拉取应用

type Applications

type Applications struct {
	Versions__delta string          `json:"versions__delta"`
	Apps__hashcode  string          `json:"apps__hashcode"`
	Application     []EurekaAppInfo `json:"application"`
}

type DataCenterInfo

type DataCenterInfo struct {
	Class string `json:"@class"`
	Name  string `json:"name"`
}

DataCenterInfo

type EurekaAppCache

type EurekaAppCache struct {
	L    sync.RWMutex
	Apps map[string]AppObject
}

EurekaAppCache

func (*EurekaAppCache) GetAllUrl

func (e *EurekaAppCache) GetAllUrl(cfname string, name string) (string, error)

GetAllUrl

func (*EurekaAppCache) GetAnHost

func (e *EurekaAppCache) GetAnHost(cfname string, name string) (AddressObject, error)

GetAnHost

func (*EurekaAppCache) GetAnUrl

func (e *EurekaAppCache) GetAnUrl(cfname string, name string) (string, error)

GetAnUrl

func (*EurekaAppCache) Save

func (e *EurekaAppCache) Save(cfname string, info EurekaAppInfo)

Save

func (*EurekaAppCache) ShowApps

func (e *EurekaAppCache) ShowApps()

ShowApps

type EurekaAppInfo

type EurekaAppInfo struct {
	Name     string              `json:"name"`
	Instance []EurekaAppInstance `json:"instance"`
}

EurekaAppInfo 应用信息

type EurekaAppInstance

type EurekaAppInstance struct {
	InstanceId                    string                 `json:"instanceId"`
	App                           string                 `json:"app"`
	AppGroupName                  string                 `json:"appGroupName"`
	IpAddr                        string                 `json:"ipAddr"`
	Sid                           string                 `json:"sid"`
	Port                          InstancePort           `json:"port"`
	SecurePort                    InstanceSecurePort     `json:"securePort"`
	HealthCheckUrl                string                 `json:"healthCheckUrl"`
	StatusPageUrl                 string                 `json:"statusPageUrl"`
	HomePageUrl                   string                 `json:"homePageUrl"`
	VipAddress                    string                 `json:"vipAddress"`
	SecureVipAddress              string                 `json:"secureVipAddress"`
	CountryId                     int                    `json:"countryId"`
	DataCenterInfo                DataCenterInfo         `json:"dataCenterInfo"`
	HostName                      string                 `json:"hostName"`
	Status                        string                 `json:"status"`
	Overriddenstatus              string                 `json:"overriddenstatus"`
	LeaseInfo                     LeaseInfo              `json:"leaseInfo"`
	IsCoordinatingDiscoveryServer string                 `json:"isCoordinatingDiscoveryServer"`
	Metadata                      map[string]interface{} `json:"metadata"`
	LastUpdatedTimestamp          string                 `json:"lastUpdatedTimestamp"`
	LastDirtyTimestamp            string                 `json:"lastDirtyTimestamp"`
	ActionType                    string                 `json:"actionType"`
}

EurekaAppInstance Eureka应用实例信息

func NewEurekaAppInstance

func NewEurekaAppInstance(cnf EurekaClientConfig) EurekaAppInstance

NewEurekaAppInstance 实例化一个Eureka实例

type EurekaClientConfig

type EurekaClientConfig struct {
	EurekaName               string   // Eureka名称,用于区分多个eureka服务端,应全局唯一
	EurekaServerAddress      string   // Eureka服务端接口地址
	Authorization            string   // Http Auth授权信息
	Apps                     []string // 需要的服务名列表
	AppName                  string   // 本服务名称
	InstanceDomain           string   // 本服务的域名地址 | 置空
	InstanceIp               string   // 本服务的ip地址 | 置空
	InstancePort             int      // 本服务的开放端口
	InstanceHomePageUrl      string   // 本服务的主页地址
	InstanceStatusUrl        string   // 本服务的状态检查地址
	InstanceHealthCheckUrl   string   // 本服务的健康检查地址
	RenewalIntervalInSecs    int64    // 本服务的心跳周期 单位秒
	DurationInSecs           int64    // 本服务的心跳失约后,注册信息保留时长,超时删除注册信息 单位秒
	AppRefreshSecs           int64    // 需要的应用列表里的应用服务信息刷新间隔 单位秒
	DropOldInstanceWhenStart bool     // 是否在启动时注册前删除该应用旧的注册信息
}

EurekaClientConfig Eureka客户端配置项

func NewEurekaConf

func NewEurekaConf(name string) EurekaClientConfig

NewEurekaConf 实例化一个eureka客户端配置

func (*EurekaClientConfig) HostName

func (e *EurekaClientConfig) HostName() string

HostName 获取主机名称

func (*EurekaClientConfig) Id

func (e *EurekaClientConfig) Id() string

Id 生成实例ID

func (*EurekaClientConfig) RefreshLocalIp

func (e *EurekaClientConfig) RefreshLocalIp()

RefreshLocalIp 刷新本地IP信息

type EurekaRegisterRequest

type EurekaRegisterRequest struct {
	Instance EurekaAppInstance `json:"instance"`
}

EurekaRegisterRequest Eureka注册请求结构

type InstancePort

type InstancePort struct {
	Enable string `json:"@enabled"`
	Value  int    `json:"$"`
}

InstancePort 实例端口

type InstanceSecurePort

type InstanceSecurePort struct {
	Enable string `json:"@enabled"`
	Value  int    `json:"$"`
}

InstanceSecurePort 实例安全端口

type LeaseInfo

type LeaseInfo struct {
	RenewalIntervalInSecs int64 `json:"renewalIntervalInSecs"`
	DurationInSecs        int64 `json:"durationInSecs"`
	RegistrationTimestamp int64 `json:"registrationTimestamp"`
	LastRenewalTimestamp  int64 `json:"lastRenewalTimestamp"`
	RenewalTimestamp      int64 `json:"renewalTimestamp"`
	EvictionTimestamp     int64 `json:"evictionTimestamp"`
	ServiceUpTimestamp    int64 `json:"serviceUpTimestamp"`
}

LeaseInfo

Jump to

Keyboard shortcuts

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