SwiftEureka

0.1.9-alpha

Swift library for working with netflix/spring Eureka
putridparrot/SwiftEureka

What's New

Clean up

2022-11-21T21:04:57Z

SwiftEureka

Swift library for working with netflix/spring Eureka

This is, by design, a simple Eureka Client package. Polling of the server is not implemented, nor is caching etc.

The package uses async/await for public facing API's

Sample of creating an InstanceInfo and using the EurekaClient to register the instance with Eureka

API

Init

To initialize an instance of the EurekaClient we pass in the URL of the Eureka server and optionally supply a Logger. If a Logger is supplied then the EurekaClient will use it to log service call information. If no logger is supplied then nothing is logged (as I'm sure you'd expect).

We currently use the swift-log package for logging.

let client = EurekaClient(
    baseUrl: URL(string: "http://192.168.0.88:8761")!, 
    logger: Logger(label: "com.putridparrot.MyApp"))

Register

To register your application with Eureka, use the following register method, passing in the InstanceInfo for your application

var instance = InstanceInfo()
instance.hostName = "myhost"
instance.app = "SWIFT-EUREKA"
instance.vipAddress = "myservice"
instance.secureVipAddress = "myservice"
instance.ipAddr = "10.0.0.10"
instance.status = InstanceStatus.up
instance.port = Port(number: 8080, enabled: true)
instance.securePort = Port(number: 8443, enabled: true)
instance.healthCheckUrl = "http://myservice:8080/healthcheck"
instance.statusPageUrl = "http://myservice:8080/status"
instance.homePageUrl = "http://myservice:8080"

do {
    try await client.register(instanceInfo: instance)
}
catch {
   print("Unexpected error \(error)")
}

Unregister

To unregister and remove your application's details from Eureka, use the unregister method. You'll need to pass the appId as registered using instance.app in your InstanceInfo. Also the specific instanceId as per instance.hostName

do {
    try await client.unregister(appId: "SWIFT-EUREKA", instanceId: "myhost")
}
catch {
   print("Unexpected error \(error)")
}

findAll

If your application either need to call the Eureka server and get an array of the registered application, use findAll. This will return the registered application name's and all the instance info. for them.

do {
    let applications = try await client.findAll()

    print("List of Applications")
    for application in applications!.application {
        print(application.name)
    }}
catch {
   print("Unexpected error \(error)")
}

The return is an Applications type which contains some Eureka spoecific information along with an array of _Application_types. The Application type includes the application name and an array of registered InstanceInfo for the application name.

find

In most scenario's an application that wants to find instances within Eureka will simply wish to find by appId OR appId and instanceId. The find method allows us to do just that

do {
    let application = try await client.find(appId)
}
catch {
   print("Unexpected error \(error)")
}

sendHeartBeat

Send a heart beat to Eureka

do {
    try await client.sendHeartBeat(appId: "SWIFT-EUREKA", instanceId: "myhost")
}
catch {
   print("Unexpected error \(error)")
}

takeOutOfService

Mark your service as "out of service"

do {
    try await client.takeOutOfService(appId: "SWIFT-EUREKA", instanceId: "myhost")
}
catch {
   print("Unexpected error \(error)")
}

moveInstanceIntoService

Mark your service as UP returning it into service

do {
    try await client.moveInstanceIntoService(appId: "SWIFT-EUREKA", instanceId: "myhost")
}
catch {
   print("Unexpected error \(error)")
}

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Sun Oct 20 2024 07:32:29 GMT-0900 (Hawaii-Aleutian Daylight Time)