如何在SpringEureka 服务发现中注册我的go lang微服务

pgx2nnw8  于 5个月前  发布在  Go
关注(0)|答案(1)|浏览(70)

我想在Eureka 服务发现中注册我的外部go lang微服务,但我不知道我在哪里做错了。
这是我从我的结束尝试:

Go lang配置文件

package configuration

import (
    "net"
(https://i.stack.imgur.com/LdCPT.png)
    "github.com/ArthurHlt/go-eureka-client/eureka"
)

func EurekClientConfig() {
    // Get the IP address of the local machine
    addrs, err := net.InterfaceAddrs()
    if err != nil {
        panic(err)
    }
    var ipAddress string
    for _, addr := range addrs {
        if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
            ipAddress = ipnet.IP.String()
            break
        }
    }

    // Initialize the Eureka client with server URL
    client := eureka.NewClient([]string{"http://localhost:8761/eureka/"})
    // Create the instance information for the GoLang service

    // eureka.NewInstanceInfo(appName, ipAddr, hostname, port, securePort, ttl, secure)
    instance := eureka.NewInstanceInfo(
        "localhost",
        "RESTAURANT-SERVICE",
        ipAddress,
        8085,
        30,
        false,
    )

    // Register the instance with the Eureka server
    client.RegisterInstance("RESTAURANT-SERVICE", instance)
    client.SendHeartbeat(instance.App, instance.HostName)
}

字符串
Go编译这个文件完美,没有任何错误,但在Eureka 服务器,这无法注册。


的数据
Eureka 服务器application.properties文件

server.port=8761

spring.application.name=eureka-server

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.server.wait-time-in-ms-when-sync-empty=0
eureka.server.enable-self-preservation=false

eureka.instance.prefer-ip-address=true
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90

spring.cloud.config.import-check.enabled=false


我只是想在Eureka 服务器上注册我的go微服务。

suzh9iv8

suzh9iv81#

我尝试了这个,做了一些调试,发现这一行的错误.

client := eureka.NewClient([]string{"http://localhost:8761/eureka/"})

字符串
当您使用上述语句并尝试打印客户端的详细信息时,它将打印如下内容...

Printing Client Details..
&{{  [] 1s } 0xc000022a50 0xc000022a80 <nil> <nil> <nil>}


示例详细信息为,表示注册示例失败.

Printing Instance Details...
<nil> instance not found


但是,如果将NewClient行替换为..,它将注册您的示例

client := eureka.NewClient([]string{"http://localhost:8761/eureka"})


它将注册你的示例,你可以得到这样的细节。为了打印示例细节,我使用了这一行.

fmt.Println(client.GetInstance(instance.App, instance.HostName))


这个的输出…

&{localhost  http://localhost:8085/info  RESTAURANT-SERVICE 127.0.0.1 http://localhost:8085  UP 0xc000182280 0xc0001822d0 0xc000186480 0xc000184200 0xc0001a83c0 false 1700182376754 1700182376753 ADDED UNKNOWN 1 } <nil>


有什么区别?额外的斜杠“/”符号造成了差异,也许回购所有者没有处理好这一点。
有关更多详细信息,请参阅git repo的自述文档的第15行。
我还提供了我使用的代码。

package main

import (
    "fmt"

    "github.com/ArthurHlt/go-eureka-client/eureka"
)

func EurekaClientConfig() {

    // Initialize the Eureka client with server URL
    client := eureka.NewClient([]string{"http://localhost:8761/eureka"})
    fmt.Println("Printing Client Details..")
    fmt.Println(client)
    // eureka.NewInstanceInfo(hostname, appName, ipAddr, port, securePort, ttl, secure)
    instance := eureka.NewInstanceInfo(
        "localhost",
        "RESTAURANT-SERVICE",
        "127.0.0.1",
        8085,
        30,
        false,
    )

    // Register the instance with the Eureka server
    client.RegisterInstance("RESTAURANT-SERVICE", instance)
    client.SendHeartbeat(instance.App, instance.HostName)
    fmt.Println("Printing Instance Details...")
    fmt.Println(client.GetInstance(instance.App, instance.HostName))
}

func main() {
    EurekaClientConfig()
}

相关问题