Files
Turbine/README.md
2025-12-13 14:20:47 +08:00

3.1 KiB

Turbine

A modular service framework.

Registrar

The Registrar is the service discovery system of the DysonNetwork. Here are a port to the Golang in order to support other Golang services.

To use the system, try build with these API:

package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"

	"yourmodule/registry"
)

func main() {
	endpoints := []string{"localhost:2379"}

	registrar, err := registry.NewServiceRegistrar(endpoints)
	if err != nil {
		log.Fatalf("Error creating registrar: %v", err)
	}

	serviceName := "orders"
	host := "10.0.0.5"
	port := 5000
	ttl := int64(30)

	err = registrar.Register(serviceName, "http", "instance-1", host, port, ttl)
	if err != nil {
		log.Fatalf("Register error: %v", err)
	}
	log.Println("Service registered")

	// Wait for termination
	stop := make(chan os.Signal, 1)
	signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
	<-stop

	err = registrar.Deregister()
	if err != nil {
		log.Printf("Deregister error: %v", err)
	} else {
		log.Println("Service deregistered")
	}
}

Gateway

The gateway is the entry point for all requests. It uses the registrar to discover services in real-time from etcd and forwards requests to the appropriate service instances.

Features

  • Service Discovery: Automatically discovers http services from etcd.
  • Dynamic Routing: Maintains an in-memory routing table that is automatically updated when services are added or removed.
  • Request Proxying: Forwards incoming requests to the correct service instance based on the URL path (/<service-name>/...).
  • Load Balancing: Implements round-robin load balancing across service instances.
  • Route Overrides: Allows for custom routing rules to be defined in the configuration file.

Configuration

The gateway is configured via a settings.toml file located in the same directory.

# The address the gateway will listen on
listen = ":8080"

# ETCD configuration for service discovery
[etcd]
endpoints = ["127.0.0.1:2379"]
# Set to true if your etcd server does not use TLS
insecure = true

# Custom route overrides
# The key is the incoming path prefix.
# The value is the destination in the format "/<service_name>/<path_prefix>"
[routes]
"/websocket" = "/chatter/ws"

Config Service

The config service provides a centralized location for other services to fetch their configuration. This is useful for managing connection strings, feature flags, and other shared parameters without hardcoding them into each service.

Usage

The config service reads a shared_config.toml file from its own directory (pkg/config) and serves it as a JSON object over a simple HTTP endpoint.

To retrieve the configuration, other services can make a GET request to the gateway at /config. The gateway will route the request to an available instance of the config service.

Example with curl:

curl http://localhost:8080/config

Expected Response (JSON):

{
  "database": {
    "connection_string": "postgres://user:password@db-host:5432/mydatabase?sslmode=require"
  },
  "redis": {
    "address": "redis-host:6379"
  }
}