chore: 删除多个客户端模块
移除以下未完成的客户端模块: - C++ 客户端和 demo - .NET 客户端和 demo - Kotlin 客户端和 demo - Lua 客户端和 demo - PHP 客户端和 demo - Ruby 客户端和 demo - Rust 客户端和 demo 更新根目录 README.md,移除相关说明。
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
# Kenaito Config Center Ruby Client
|
||||
|
||||
Ruby 语言的配置中心客户端 SDK。
|
||||
|
||||
## 依赖
|
||||
|
||||
- Ruby 2.7+
|
||||
- 标准库(net/http, json, openssl, base64)
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
gem build kenaito-conf.gemspec
|
||||
gem install kenaito-conf-1.0.0.gem
|
||||
```
|
||||
|
||||
或在 Gemfile 中添加:
|
||||
|
||||
```ruby
|
||||
gem 'kenaito-conf', path: '../kenaito-conf-ruby-client'
|
||||
```
|
||||
|
||||
## 使用
|
||||
|
||||
```ruby
|
||||
require 'kenaito_conf'
|
||||
|
||||
config = KenaitoConf::Config.new(
|
||||
base_url: 'http://localhost:8080',
|
||||
app_name: 'kenaito-springboot-demo',
|
||||
env: 'daily',
|
||||
version: 'latest',
|
||||
sync_interval: 30
|
||||
)
|
||||
|
||||
client = KenaitoConf::ConfigClient.new(config)
|
||||
|
||||
client.on_change do |changed|
|
||||
changed.each do |k, v|
|
||||
puts "config changed: #{k} = #{v}"
|
||||
end
|
||||
end
|
||||
|
||||
client.start
|
||||
|
||||
value = client.get('database.host')
|
||||
all = client.get_all
|
||||
|
||||
client.stop
|
||||
```
|
||||
|
||||
## 配置参数
|
||||
|
||||
| 参数 | 说明 | 默认值 |
|
||||
|------|------|--------|
|
||||
| `base_url` | 配置中心地址 | 必填 |
|
||||
| `app_name` | 应用名称 | 必填 |
|
||||
| `env` | 环境 | `dev` |
|
||||
| `version` | 配置版本 | `latest` |
|
||||
| `token` | 认证令牌 | 空 |
|
||||
| `sync_interval` | 同步间隔(秒) | `60` |
|
||||
| `ignore_global` | 是否忽略全局配置 | `false` |
|
||||
| `cache_dir` | 本地缓存目录 | `.` |
|
||||
|
||||
## Demo
|
||||
|
||||
```bash
|
||||
cd kenaito-conf-ruby-client-demo
|
||||
ruby app.rb
|
||||
```
|
||||
@@ -1,18 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = 'kenaito-conf'
|
||||
spec.version = '1.0.0'
|
||||
spec.authors = ['odboy']
|
||||
spec.email = ['tianjun@odboy.cn']
|
||||
|
||||
spec.summary = 'Kenaito Config Center Ruby Client'
|
||||
spec.description = 'Ruby client for Kenaito Config Center'
|
||||
spec.homepage = 'https://gitea.odboy.cn/odboy/kenaito-conf'
|
||||
spec.license = 'MIT'
|
||||
|
||||
spec.required_ruby_version = '>= 2.7.0'
|
||||
|
||||
spec.files = Dir['lib/**/*']
|
||||
spec.require_paths = ['lib']
|
||||
end
|
||||
@@ -1,165 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'net/http'
|
||||
require 'json'
|
||||
|
||||
module KenaitoConf
|
||||
class Config
|
||||
attr_accessor :base_url, :app_name, :env, :version, :token, :sync_interval, :ignore_global, :cache_dir
|
||||
|
||||
def initialize(options = {})
|
||||
@base_url = options[:base_url] || ''
|
||||
@app_name = options[:app_name] || ''
|
||||
@env = options[:env] || 'dev'
|
||||
@version = options[:version] || 'latest'
|
||||
@token = options[:token] || ''
|
||||
@sync_interval = options[:sync_interval] || 60
|
||||
@ignore_global = options[:ignore_global] || false
|
||||
@cache_dir = options[:cache_dir] || '.'
|
||||
end
|
||||
end
|
||||
|
||||
class Cache
|
||||
CACHE_FILE = 'config-cache.json'
|
||||
|
||||
def initialize(cache_dir = '.')
|
||||
@cache_dir = cache_dir
|
||||
end
|
||||
|
||||
def write(data)
|
||||
File.write(File.join(@cache_dir, CACHE_FILE), JSON.pretty_generate(data))
|
||||
end
|
||||
|
||||
def read
|
||||
path = File.join(@cache_dir, CACHE_FILE)
|
||||
return nil unless File.exist?(path)
|
||||
JSON.parse(File.read(path))
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
class ConfigClient
|
||||
def initialize(config)
|
||||
@config = config
|
||||
@cache = Cache.new(config.cache_dir)
|
||||
@configs = {}
|
||||
@last_time = 0
|
||||
@handlers = []
|
||||
@started = false
|
||||
end
|
||||
|
||||
def on_change(&handler)
|
||||
@handlers << handler
|
||||
end
|
||||
|
||||
def get(key)
|
||||
@configs[key]
|
||||
end
|
||||
|
||||
def get_all
|
||||
@configs.dup
|
||||
end
|
||||
|
||||
def start
|
||||
return if @started
|
||||
@started = true
|
||||
|
||||
begin
|
||||
fetch_all
|
||||
rescue StandardError => e
|
||||
warn "[ConfigClient] fetch all configs error: #{e.message}, trying cache"
|
||||
cached = @cache.read
|
||||
if cached
|
||||
apply_configs(cached)
|
||||
warn "[ConfigClient] loaded #{@configs.size} configs from cache"
|
||||
else
|
||||
raise "fetch configs failed and no cache: #{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
Thread.new do
|
||||
loop do
|
||||
sleep @config.sync_interval
|
||||
begin
|
||||
fetch_changed
|
||||
rescue StandardError => e
|
||||
warn "[ConfigClient] sync error: #{e.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
@started = false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_all
|
||||
uri = URI("#{@config.base_url}/api/fetchAllConfigs")
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
http.read_timeout = 10
|
||||
|
||||
request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
|
||||
request.body = JSON.generate({
|
||||
appName: @config.app_name,
|
||||
env: @config.env,
|
||||
version: @config.version,
|
||||
ignoreGlobal: @config.ignore_global,
|
||||
token: @config.token
|
||||
})
|
||||
|
||||
response = http.request(request)
|
||||
raise "HTTP #{response.code}" unless response.code == '200'
|
||||
|
||||
data = JSON.parse(response.body)
|
||||
raise "server error: #{data['msg']}" if data['code'] && data['code'] != 0
|
||||
|
||||
@last_time = data['latest.fetch.timestamp'].to_i if data['latest.fetch.timestamp']
|
||||
|
||||
@cache.write(data)
|
||||
apply_configs(data)
|
||||
warn "[ConfigClient] fetched #{@configs.size} configs"
|
||||
end
|
||||
|
||||
def fetch_changed
|
||||
uri = URI("#{@config.base_url}/api/fetchChangedConfigs")
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
http.read_timeout = 10
|
||||
|
||||
request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
|
||||
request.body = JSON.generate({
|
||||
appName: @config.app_name,
|
||||
env: @config.env,
|
||||
version: @config.version,
|
||||
lastTime: @last_time,
|
||||
ignoreGlobal: @config.ignore_global,
|
||||
token: @config.token
|
||||
})
|
||||
|
||||
response = http.request(request)
|
||||
result = JSON.parse(response.body)
|
||||
raise "server error" if !result['code'] || result['code'] != 0
|
||||
|
||||
changed_data = result['data'] || {}
|
||||
@last_time = changed_data.delete('latest.fetch.timestamp').to_i if changed_data['latest.fetch.timestamp']
|
||||
|
||||
changed = {}
|
||||
changed_data.each do |k, v|
|
||||
changed[k] = v
|
||||
end
|
||||
|
||||
@configs.merge!(changed)
|
||||
@handlers.each { |h| h.call(changed) rescue warn "[ConfigClient] handler error: #{$!.message}" }
|
||||
end
|
||||
|
||||
def apply_configs(data)
|
||||
data.each do |k, v|
|
||||
next if k.start_with?('kenaito.') || k == 'latest.fetch.timestamp'
|
||||
next unless v.is_a?(String)
|
||||
@configs[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user