1. Archaiusとは何ですか?
Archaiusは、実際にはApache Common Configuration Libraryのカプセル化と拡張であり、Javaベースの構成APIのセットを提供します。主な機能は次のとおりです。
- 構成は動的に調整できます
- 構成サポートタイプ(Int、Long、Booleanなど)
- 高性能とスレッドセーフ
- 構成をプルするためのフレームワークを提供します。これにより、変更された構成を構成ソースから動的にプルできます。
- 構成が変更されたときに自動的に呼び出されるコールバックメカニズムをサポートします
- JMX MBeanをサポートします。構成を表示し、JConsoleを介して構成を変更できます。
コアAchaiusは、構成概念の組み合わせ(複合構成)と呼ばれ、単純な階層構成として理解できます。階層優先度と高優先度レベルの構成は、低優先度の構成を上書きします。各レベルは、ローカル構成ファイル、JDBCデータソース、リモートRESTAPIなどの構成ソースから構成を取得できます。構成ソースは、実行時に変更を動的にプルすることもできます。たとえば、上の図では、永続DB構成は、構成をリレーショナルデータベースに格納することを指し、対応する構成ソースはデータベースから定期的に変更をプルします。構成の最終値は、最上位の構成によって決定されます。たとえば、複数のレベルに特定の構成項目が含まれている場合、アプリケーションに表示される最終値は、構成レベルの最上位の値です。構成レイヤーの順序を調整できます
プル構成のコアインターフェイスはPolledConfigurationSource
、Achaiusが2つのサブカテゴリ(JDBCConfigurationSource(データベースに基づく)とURLConfigurationSource(URLに基づく))を提供することです。以下では、データベースベースの方法で動的プロパティを取得します。
public interface PolledConfigurationSource {
/**
* Poll the configuration source to get the latest content.
*
* @param initial true if this operation is the first poll.
* @param checkPoint Object that is used to determine the starting point if the result returned is incremental.
* Null if there is no check point or the caller wishes to get the full content.
* @return The content of the configuration which may be full or incremental.
* @throws Exception If any exception occurs when fetching the configurations.
*/
public PollResult poll(boolean initial, Object checkPoint) throws Exception;
}
2.データベースに基づいて動的属性を取得します
1)、archaius依存関係を追加します
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.6.0</version>
</dependency>
2)、データベーステーブルを作成します
CREATE TABLE `my_site_properties` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`property_key` varchar(50) DEFAULT NULL COMMENT '属性的key',
`property_value` varchar(255) DEFAULT NULL COMMENT '属性的值',
PRIMARY KEY (`id`),
UNIQUE KEY `my_site_properties_u1` (`property_key`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
3)、構成クラス
@Configuration
public class DynamicPropertyFactoryConfig {
@Autowired
private DataSource dataSource;
@PostConstruct
public void init() {
PolledConfigurationSource source = new JDBCConfigurationSource(dataSource,
"select distinct property_key, property_value from my_site_properties",
"property_key",
"property_value");
//首次任务立即执行,1秒间隔
FixedDelayPollingScheduler scheduler = new FixedDelayPollingScheduler(0, 1000, false);
DynamicConfiguration configuration = new DynamicConfiguration(source,
scheduler);
ConfigurationManager.install(configuration);
}
}
4)、動的構成を取得します
@RestController
@RequestMapping("/api/demo1")
public class Demo1Controller {
DynamicStringProperty defaultProp = DynamicPropertyFactory.getInstance().getStringProperty(
"demo", "default");
@GetMapping
public String getDynamicStringProperty() {
return defaultProp.getValue();
}
}
実現効果:動的構成を取得するようにインターフェースを要求すると、データベースのproperty_keyはデモに対応するproperty_valueになります。データベースの値を変更してインターフェースを再度要求すると、最新のものを取得できます。値。
3.カスタム構成ソースとポーリングスケジューラ
1)、カスタム構成ソース
PolledConfigurationSourceインターフェースを実装する
public class DynamicConfigurationSource implements PolledConfigurationSource {
@Override
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("demo2", UUID.randomUUID().toString());
return PollResult.createFull(map);
}
}
タイミングスケジューラをカスタマイズする場合は、抽象クラスAbstractPollingSchedulerを実装できます。
2)、構成クラス
@Configuration
public class DynamicPropertyFactoryConfig {
@PostConstruct
public void init() {
PolledConfigurationSource source = new DynamicConfigurationSource();
//首次任务立即执行,1秒间隔 DynamicConfigurationSource中使用UUID 相当于demo2的配置每1秒刷新一次
FixedDelayPollingScheduler scheduler = new FixedDelayPollingScheduler(0, 1000, false);
DynamicConfiguration configuration = new DynamicConfiguration(source,
scheduler);
ConfigurationManager.install(configuration);
}
}
3)動的構成を取得する
@RestController
@RequestMapping("/api/demo2")
public class Demo2Controller {
DynamicStringProperty defaultProp = DynamicPropertyFactory.getInstance().getStringProperty(
"demo2", "default");
@GetMapping
public String getDynamicStringProperty() {
return defaultProp.getValue();
}
}
実現効果:動的構成を取得し、結果をUUIDとして返し、毎秒変更するようにインターフェイスに要求します
参照:
https://blog.csdn.net/yang75108/article/details/86990136
https://github.com/Netflix/archaius/wiki
https://www.cnblogs.com/lexiaofei/p/7169483.html