eureka

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

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

Go to latest
Published: Jul 5, 2021 License: MIT Imports: 11 Imported by: 2

README

eureka-client

介绍

Eureka客户端Go封装 依赖:https://github.com/HikoQiu/go-eureka-client; https://github.com/HikoQiu/go-feign;

软件架构

封装了 go-eureka-client 和 go-feign

安装教程

go get -u gitee.com/wxlao/eureka-client

使用说明
// 引入包
import "gitee.com/wxlao/eureka-client"

// 新客户端
client1 := eureka.New("post-service", "http://192.168.1.100:1111/eureka,http://192.168.1.101:1111/eureka");

client2 := eureka.New(
    "post-service",
    "http://192.168.1.100:1111/eureka",
    eureka.WithPort(80),
    eureka.WithIP("192.168.1.102"),
    eureka.WithLogFunc(func(level int, format string, a ...interface{}) {
        var logFunc *zerolog.Event
        switch level {
        case 1:
            logFunc = log.Debug()
        case 2:
            logFunc = log.Info()
        case 3:
            logFunc = log.Error()
        }
        if logFunc != nil {
            funcName, file, line, _ := runtime.Caller(2)
            fullFuncName := runtime.FuncForPC(funcName).Name()
            arr := strings.Split(fullFuncName, "/")
            arrFile := strings.Split(file, "/")

            logFunc.Str("file", arrFile[len(arrFile)-1]).Int("line", line).Str("func", arr[len(arr)-1]).Msgf(format, a...)
        }
    })
);

// 调用服务
resp, err := client1.Get("http://post-service/api/v1/list-post")
resp, err := client2.Post("http://post-service/api/v1/add", body)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Register() error
	Get(rawURL string, opts ...Option) (*http.Response, error)
	Post(rawURL string, body *bytes.Reader, opts ...Option) (*http.Response, error)
}

Client 整合客户端

func New

func New(serviceName string, zone string, opts ...Option) Client

New 新建客户端

serviceName: 服务名,如:post-service
zone: eureka服务地址,多个逗号","分隔离,如:http://192.168.1.100:1111/eureka,http://192.168.1.101:1111/eureka
opts: 可选配置,指定端口:eureka.WithPort(80),指定IP:eureka.WithIP("192.168.1.102"), 指定logger:eureka.WithLogFunc(func...)
调用例子:
	client1 := eureka.New("post-service", "http://192.168.1.100:1111/eureka,http://192.168.1.101:1111/eureka");
	client2 := eureka.New(
		"post-service",
		"http://192.168.1.100:1111/eureka",
		eureka.WithPort(80),
		eureka.WithIP("192.168.1.102"),
		eureka.WithLogFunc(func...)
	);

type LogFunc

type LogFunc = func(level int, format string, a ...interface{})

LogFunc 日志方法

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option 配置

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders 指定请求头 默认{"Content-Type": "application/json"}

func WithIP

func WithIP(ip string) Option

WithIP 指定IP,默认当前主机IP

func WithKeepAliveDuration

func WithKeepAliveDuration(keepAliveDuration time.Duration) Option

WithKeepAliveDuration 实例健康检测时常,默认1分钟

func WithLogFunc

func WithLogFunc(logFunc LogFunc) Option

WithLogFunc 指定logger

例子
func(level int, format string, a ...interface{}) {
	var logFunc *zerolog.Event
	switch level {
	case 1:
		logFunc = log.Debug()
	case 2:
		logFunc = log.Info()
	case 3:
		logFunc = log.Error()
	}
	if logFunc != nil {
		funcName, file, line, _ := runtime.Caller(2)
		fullFuncName := runtime.FuncForPC(funcName).Name()
		arr := strings.Split(fullFuncName, "/")
		arrFile := strings.Split(file, "/")

		logFunc.Str("file", arrFile[len(arrFile)-1]).Int("line", line).Str("func", arr[len(arr)-1]).Msgf(format, a...)
	}
}

func WithPort

func WithPort(port int) Option

WithPort 指定端口,默认80

Jump to

Keyboard shortcuts

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