{"id":15916,"date":"2026-04-26T09:09:32","date_gmt":"2026-04-26T01:09:32","guid":{"rendered":"https:\/\/top.duoku.icu\/libs\/15916.html"},"modified":"2026-04-26T09:09:32","modified_gmt":"2026-04-26T01:09:32","slug":"apollo-%e9%85%8d%e7%bd%ae%e4%b8%ad%e5%bf%83%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95%e4%b8%8e%e4%ba%8c%e6%ac%a1%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/www.srclibs.com\/index.php\/2026\/04\/26\/apollo-%e9%85%8d%e7%bd%ae%e4%b8%ad%e5%bf%83%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95%e4%b8%8e%e4%ba%8c%e6%ac%a1%e5%bc%80%e5%8f%91\/","title":{"rendered":"Apollo \u914d\u7f6e\u4e2d\u5fc3\u4f7f\u7528\u65b9\u6cd5\u4e0e\u4e8c\u6b21\u5f00\u53d1"},"content":{"rendered":"<div style=\"text-align:center;margin:30px 0;\"><img decoding=\"async\" src=\"https:\/\/top.duoku.icu\/wp-content\/uploads\/2026\/04\/image-85.png\" alt=\"Apollo \u914d\u7f6e\u4e2d\u5fc3\u4f7f\u7528\u65b9\u6cd5\u4e0e\u4e8c\u6b21\u5f00\u53d1\" style=\"max-width:100%;height:auto;border-radius:12px;box-shadow:0 4px 12px rgba(0,0,0,0.1);\"><\/div>\n<h1>Apollo \u914d\u7f6e\u4e2d\u5fc3\u4f7f\u7528\u65b9\u6cd5\u4e0e\u4e8c\u6b21\u5f00\u53d1<\/h1>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e2d\uff0c\u914d\u7f6e\u7ba1\u7406\u662f\u81f3\u5173\u91cd\u8981\u7684\u4e00\u73af\u3002Apache Apollo \u4f5c\u4e3a\u643a\u7a0b\u5f00\u6e90\u7684\u5206\u5e03\u5f0f\u914d\u7f6e\u4e2d\u5fc3\uff0c\u4ee5\u5176\u4e30\u5bcc\u7684\u529f\u80fd\u3001\u5f3a\u5927\u7684\u6027\u80fd\u548c\u7075\u6d3b\u7684\u6269\u5c55\u6027\uff0c\u6210\u4e3a\u4e1a\u754c\u5e7f\u6cdb\u4f7f\u7528\u7684\u914d\u7f6e\u7ba1\u7406\u89e3\u51b3\u65b9\u6848\u3002\u672c\u6587\u5c06\u6df1\u5165\u6d45\u51fa\u5730\u8bb2\u89e3 Apollo \u7684\u6838\u5fc3\u6982\u5ff5\u3001\u67b6\u6784\u8bbe\u8ba1\u3001\u4f7f\u7528\u65b9\u6cd5\u4ee5\u53ca\u4e8c\u6b21\u5f00\u53d1\u5b9e\u8df5\u3002<\/p>\n<h2>\u4e00\u3001Apollo \u6838\u5fc3\u6982\u5ff5\u4e0e\u67b6\u6784<\/h2>\n<h3>1.1 \u6838\u5fc3\u6982\u5ff5<\/h3>\n<p>Apollo \u7684\u914d\u7f6e\u4f53\u7cfb\u91c7\u7528\u5c42\u7ea7\u5316\u7684\u8bbe\u8ba1\uff0c\u6838\u5fc3\u6982\u5ff5\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>Application\uff08\u5e94\u7528\uff09<\/strong>\uff1a\u914d\u7f6e\u7684\u4f7f\u7528\u65b9\uff0c\u662f\u914d\u7f6e\u7ba1\u7406\u7684\u6700\u9ad8\u5c42\u7ea7<\/li>\n<li><strong>Cluster\uff08\u96c6\u7fa4\uff09<\/strong>\uff1a\u540c\u4e00\u5e94\u7528\u4e2d\u4e0d\u540c\u90e8\u7f72\u73af\u5883\u7684\u903b\u8f91\u5206\u7ec4<\/li>\n<li><strong>Namespace\uff08\u547d\u540d\u7a7a\u95f4\uff09<\/strong>\uff1a\u914d\u7f6e\u7684\u5bb9\u5668\uff0c\u4e00\u4e2a\u5e94\u7528\u53ef\u4ee5\u62e5\u6709\u591a\u4e2a\u547d\u540d\u7a7a\u95f4<\/li>\n<li><strong>Config\uff08\u914d\u7f6e\uff09<\/strong>\uff1a\u5177\u4f53\u7684\u914d\u7f6e\u9879\u96c6\u5408\uff0c\u5305\u542b key-value \u5bf9<\/li>\n<li><strong>ConfigChange\uff08\u914d\u7f6e\u53d8\u66f4\uff09<\/strong>\uff1a\u914d\u7f6e\u7684\u53d8\u66f4\u5386\u53f2<\/li>\n<\/ul>\n<pre><code>Application\n\u2514\u2500\u2500 Cluster (Production)\n    \u2514\u2500\u2500 Namespace (default)\n        \u2514\u2500\u2500 Config (key: value)\n    \u2514\u2500\u2500 Namespace (database)\n        \u2514\u2500\u2500 Config (key: value)\n<\/code><\/pre>\n<h3>1.2 \u67b6\u6784\u8bbe\u8ba1<\/h3>\n<p>Apollo \u91c7\u7528\u5206\u5c42\u67b6\u6784\u8bbe\u8ba1\uff0c\u4e3b\u8981\u7ec4\u4ef6\u5305\u62ec\uff1a<\/p>\n<ol>\n<li><strong>Config Service<\/strong>\uff1a\u914d\u7f6e\u670d\u52a1\uff0c\u63d0\u4f9b\u914d\u7f6e\u7684\u8bfb\u53d6\u3001\u4fee\u6539\u3001\u53d1\u5e03\u7b49\u529f\u80fd<\/li>\n<li><strong>Admin Service<\/strong>\uff1a\u7ba1\u7406\u670d\u52a1\uff0c\u63d0\u4f9b\u914d\u7f6e\u7684\u521b\u5efa\u3001\u4fee\u6539\u3001\u5220\u9664\u7b49\u7ba1\u7406\u529f\u80fd<\/li>\n<li><strong>Portal<\/strong>\uff1a\u914d\u7f6e\u7ba1\u7406\u754c\u9762\uff0c\u63d0\u4f9b Web \u53ef\u89c6\u5316\u64cd\u4f5c<\/li>\n<li><strong>Client<\/strong>\uff1a\u5ba2\u6237\u7aef SDK\uff0c\u63d0\u4f9b\u914d\u7f6e\u7684\u76d1\u542c\u548c\u83b7\u53d6\u529f\u80fd<\/li>\n<\/ul>\n<pre><code>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                    Portal                           \u2502\n\u2502               (\u914d\u7f6e\u7ba1\u7406\u754c\u9762)                          \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                     \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                  Admin Service                      \u2502\n\u2502                (\u7ba1\u7406\u540e\u53f0\u670d\u52a1)                         \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                     \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                  Config Service                     \u2502\n\u2502                (\u914d\u7f6e\u8bfb\u53d6\u670d\u52a1)                         \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                     \u2502\n        \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n        \u2502                         \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510          \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502   Config      \u2502          \u2502   Config     \u2502\n\u2502   Client      \u2502          \u2502   Client     \u2502\n\u2502  (\u5e94\u7528 A)      \u2502          \u2502  (\u5e94\u7528 B)     \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518          \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/code><\/pre>\n<h2>\u4e8c\u3001Apollo \u5b89\u88c5\u4e0e\u914d\u7f6e<\/h2>\n<h3>2.1 \u73af\u5883\u8981\u6c42<\/h3>\n<ul>\n<li>JDK 1.8+<\/li>\n<li>MySQL 5.6+<\/li>\n<li>Tomcat 8.0+<\/li>\n<li>Maven 3.0+<\/li>\n<\/ul>\n<h3>2.2 \u5b89\u88c5\u6b65\u9aa4<\/h3>\n<h4>\u6b65\u9aa4\u4e00\uff1a\u6570\u636e\u5e93\u521d\u59cb\u5316<\/h4>\n<pre><code>-- \u521b\u5efa\u6570\u636e\u5e93\nCREATE DATABASE ApolloConfigDB DEFAULT CHARACTER SET utf8mb4;\n\n-- \u6267\u884c\u521d\u59cb\u5316\u811a\u672c\nsource \/path\/to\/apollo-configservice\/sql\/mysql.sql\n\n-- \u6267\u884c Admin Service \u811a\u672c\nsource \/path\/to\/apollo-adminservice\/sql\/mysql.sql\n\n-- \u6267\u884c Portal \u811a\u672c\nsource \/path\/to\/apollo-portal\/sql\/mysql.sql\n<\/code><\/pre>\n<h4>\u6b65\u9aa4\u4e8c\uff1a\u4fee\u6539\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>&#8220;`properties<\/p>\n<h1>application.properties<\/h1>\n<h1>Config Service<\/h1>\n<p>spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/ApolloConfigDB?characterEncoding=utf8<br \/>\nspring.datasource.username=apollo<br \/>\nspring.datasource.password=apollo123<\/p>\n<h1>Admin Service<\/h1>\n<p>spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/ApolloConfigDB?characterEncoding=utf8<br \/>\nspring.datasource.username=apollo<br \/>\nspring.datasource.password=apollo123<\/p>\n<h1>Portal<\/h1>\n<p>spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/ApolloConfigDB?characterEncoding=utf8<br \/>\nspring.datasource.username=apollo<br \/>\nspring.datasource.password=apollo123<\/p>\n<pre><code>\n<h4>\u6b65\u9aa4\u4e09\uff1a\u542f\u52a8\u670d\u52a1<\/h4>\n\n<\/code><\/pre>\n<p>bash<\/p>\n<h1>\u542f\u52a8 Config Service<\/h1>\n<p>mvn package -DskipTests<br \/>\njava -jar configservice\/target\/apollo-configservice-1.9.0.jar<\/p>\n<h1>\u542f\u52a8 Admin Service<\/h1>\n<p>mvn package -DskipTests<br \/>\njava -jar adminservice\/target\/apollo-adminservice-1.9.0.jar<\/p>\n<h1>\u542f\u52a8 Portal<\/h1>\n<p>mvn package -DskipTests<br \/>\njava -jar portal\/target\/apollo-portal-1.9.0.jar<\/p>\n<pre><code>\n<h3>2.3 \u521d\u59cb\u5316\u914d\u7f6e<\/h3>\n\n\u8bbf\u95ee `http:\/\/localhost:8080`\uff0c\u4f7f\u7528\u9ed8\u8ba4\u8d26\u53f7\u5bc6\u7801\u767b\u5f55\uff08apollo\/apollo123\uff09\u3002\n\n<h2>\u4e09\u3001Apollo \u4f7f\u7528\u65b9\u6cd5<\/h2>\n\n<h3>3.1 \u5ba2\u6237\u7aef\u63a5\u5165<\/h3>\n\n<h4>Maven \u4f9d\u8d56<\/h4>\n\n<\/code><\/pre>\n<p>xml<br \/>\n<dependencies><br \/>\n    <!-- Apollo Client --><br \/>\n    <dependency><br \/>\n        <groupId>com.ctrip.framework.apollo<\/groupId><br \/>\n        <artifactId>apollo-client<\/artifactId><br \/>\n        <version>1.9.0<\/version><br \/>\n    <\/dependency><\/p>\n<p>    <!-- Spring Boot \u96c6\u6210 --><br \/>\n    <dependency><br \/>\n        <groupId>com.ctrip.framework.apollo<\/groupId><br \/>\n        <artifactId>apollo-client-spring-boot-starter<\/artifactId><br \/>\n        <version>1.9.0<\/version><br \/>\n    <\/dependency><br \/>\n<\/dependencies><\/p>\n<pre><code>\n<h4>\u914d\u7f6e application.yml<\/h4>\n\n<\/code><\/pre>\n<p>yaml<br \/>\napp:<br \/>\n  id: sample-app<\/p>\n<p>apollo:<br \/>\n  meta: http:\/\/localhost:8080<br \/>\n  bootstrap:<br \/>\n    enabled: true<br \/>\n    namespaces: application,database<\/p>\n<pre><code>\n<h3>3.2 \u914d\u7f6e\u8bfb\u53d6<\/h3>\n\n<h4>\u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528 @Value \u6ce8\u89e3<\/h4>\n\n<\/code><\/pre>\n<p>java<br \/>\n@RestController<br \/>\npublic class ConfigController {<\/p>\n<p>    @Value(&#8220;${app.name:\u9ed8\u8ba4\u5e94\u7528\u540d}&#8221;)<br \/>\n    private String appName;<\/p>\n<p>    @Value(&#8220;${app.version:1.0}&#8221;)<br \/>\n    private String appVersion;<\/p>\n<p>    @GetMapping(&#8220;\/config&#8221;)<br \/>\n    public Map<String, String> getConfig() {<br \/>\n        return Map.of(<br \/>\n            &#8220;appName&#8221;, appName,<br \/>\n            &#8220;appVersion&#8221;, appVersion<br \/>\n        );<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h4>\u65b9\u5f0f\u4e8c\uff1a\u4f7f\u7528 @ConfigurationProperties<\/h4>\n\n<\/code><\/pre>\n<p>java<br \/>\n@Configuration<br \/>\n@ConfigurationProperties(prefix = &#8220;app&#8221;)<br \/>\n@Data<br \/>\npublic class AppConfig {<br \/>\n    private String name;<br \/>\n    private String version;<br \/>\n    private DatabaseConfig database;<\/p>\n<p>    @Data<br \/>\n    public static class DatabaseConfig {<br \/>\n        private String url;<br \/>\n        private String username;<br \/>\n        private String password;<br \/>\n        private int maxPoolSize;<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h4>\u65b9\u5f0f\u4e09\uff1a\u4f7f\u7528 Config \u5bf9\u8c61<\/h4>\n\n<\/code><\/pre>\n<p>java<br \/>\n@RestController<br \/>\npublic class ConfigController {<\/p>\n<p>    @Autowired<br \/>\n    private Config config;<\/p>\n<p>    @GetMapping(&#8220;\/all-config&#8221;)<br \/>\n    public Map<String, String> getAllConfig() {<br \/>\n        Map<String, String> allConfig = new HashMap<>();<br \/>\n        config.getProperties().forEach((k, v) -><br \/>\n            allConfig.put(k, String.valueOf(v))<br \/>\n        );<br \/>\n        return allConfig;<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h3>3.3 \u914d\u7f6e\u76d1\u542c\u4e0e\u81ea\u52a8\u5237\u65b0<\/h3>\n\n<h4>\u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528 @RefreshScope<\/h4>\n\n<\/code><\/pre>\n<p>java<br \/>\n@RestController<br \/>\n@RefreshScope<br \/>\npublic class DynamicConfigController {<\/p>\n<p>    @Value(&#8220;${dynamic.config:initial}&#8221;)<br \/>\n    private String dynamicConfig;<\/p>\n<p>    @GetMapping(&#8220;\/dynamic&#8221;)<br \/>\n    public String getDynamicConfig() {<br \/>\n        return dynamicConfig;<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h4>\u65b9\u5f0f\u4e8c\uff1a\u5b9e\u73b0 ConfigChangeListener<\/h4>\n\n<\/code><\/pre>\n<p>java<br \/>\n@Component<br \/>\npublic class MyConfigListener implements ConfigChangeListener {<\/p>\n<p>    @Override<br \/>\n    public void onChange(ConfigChangeEventChangeEvent event) {<br \/>\n        \/\/ \u5904\u7406\u914d\u7f6e\u53d8\u66f4<br \/>\n        event.changedItems().forEach((key, change) -> {<br \/>\n            System.out.println(&#8220;Key: &#8221; + key);<br \/>\n            System.out.println(&#8220;ChangeType: &#8221; + change.changeType());<br \/>\n            System.out.println(&#8220;OldValue: &#8221; + change.oldValue());<br \/>\n            System.out.println(&#8220;NewValue: &#8221; + change.newValue());<br \/>\n        });<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h2>\u56db\u3001Apollo \u4e8c\u6b21\u5f00\u53d1<\/h2>\n\n<h3>4.1 \u81ea\u5b9a\u4e49\u914d\u7f6e\u6765\u6e90<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\npublic class CustomConfigSource extends AbstractConfig {<\/p>\n<p>    @Override<br \/>\n    public Map<String, String> load() {<br \/>\n        Map<String, String> config = new HashMap<>();<br \/>\n        \/\/ \u4ece\u81ea\u5b9a\u4e49\u6765\u6e90\u52a0\u8f7d\u914d\u7f6e<br \/>\n        config.put(&#8220;custom.key&#8221;, &#8220;custom.value&#8221;);<br \/>\n        return config;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public String getConfigSourceName() {<br \/>\n        return &#8220;custom-source&#8221;;<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h3>4.2 \u6269\u5c55\u914d\u7f6e\u6821\u9a8c<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\npublic class CustomConfigValidator extends ConfigValidator {<\/p>\n<p>    @Override<br \/>\n    public void validate(ConfigItem item) {<br \/>\n        String value = item.getValue();<\/p>\n<p>        if (&#8220;email&#8221;.equals(item.getKey())) {<br \/>\n            if (!value.matches(&#8220;^[A-Za-z0-9+_.-]+@(.+)$&#8221;)) {<br \/>\n                throw new IllegalArgumentException(&#8220;Invalid email format&#8221;);<br \/>\n            }<br \/>\n        }<\/p>\n<p>        if (&#8220;port&#8221;.equals(item.getKey())) {<br \/>\n            try {<br \/>\n                int port = Integer.parseInt(value);<br \/>\n                if (port < 1 || port > 65535) {<br \/>\n                    throw new IllegalArgumentException(&#8220;Invalid port&#8221;);<br \/>\n                }<br \/>\n            } catch (NumberFormatException e) {<br \/>\n                throw new IllegalArgumentException(&#8220;Port must be numeric&#8221;);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h3>4.3 \u81ea\u5b9a\u4e49\u6570\u636e\u683c\u5f0f\u652f\u6301<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\npublic class JsonConfigFormatHandler implements ConfigFormatHandler {<\/p>\n<p>    @Override<br \/>\n    public String getType() {<br \/>\n        return &#8220;json&#8221;;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public Map<String, Object> parse(String content) {<br \/>\n        try {<br \/>\n            return objectMapper.readValue(content, Map.class);<br \/>\n        } catch (IOException e) {<br \/>\n            throw new RuntimeException(&#8220;Failed to parse JSON config&#8221;, e);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public String serialize(Map<String, Object> config) {<br \/>\n        try {<br \/>\n            return objectMapper.writeValueAsString(config);<br \/>\n        } catch (IOException e) {<br \/>\n            throw new RuntimeException(&#8220;Failed to serialize config&#8221;, e);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h3>4.4 \u76d1\u63a7\u4e0e\u544a\u8b66\u6269\u5c55<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n@Component<br \/>\npublic class ConfigMonitor implements ApplicationEventListener {<\/p>\n<p>    @Autowired<br \/>\n    private ApolloConfigService configService;<\/p>\n<p>    @Override<br \/>\n    public void onApplicationEvent(ConfigChangeEvent event) {<br \/>\n        \/\/ \u8bb0\u5f55\u914d\u7f6e\u53d8\u66f4\u5386\u53f2<br \/>\n        logConfigChange(event);<\/p>\n<p>        \/\/ \u53d1\u9001\u544a\u8b66\u901a\u77e5<br \/>\n        if (isCriticalChange(event)) {<br \/>\n            sendAlert(event);<br \/>\n        }<\/p>\n<p>        \/\/ \u66f4\u65b0\u76d1\u63a7\u6307\u6807<br \/>\n        updateMetrics(event);<br \/>\n    }<\/p>\n<p>    private void logConfigChange(ConfigChangeEvent event) {<br \/>\n        \/\/ \u8bb0\u5f55\u65e5\u5fd7<br \/>\n    }<\/p>\n<p>    private void sendAlert(ConfigChangeEvent event) {<br \/>\n        \/\/ \u53d1\u9001\u544a\u8b66<br \/>\n    }<\/p>\n<p>    private void updateMetrics(ConfigChangeEvent event) {<br \/>\n        \/\/ \u66f4\u65b0\u76d1\u63a7\u6307\u6807<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h2>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n<h3>5.1 \u914d\u7f6e\u7ec4\u7ec7\u89c4\u8303<\/h3>\n\n<\/code><\/pre>\n<p>Application<br \/>\n\u251c\u2500\u2500 Cluster (dev)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (application)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (database)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (redis)<br \/>\n\u251c\u2500\u2500 Cluster (test)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (application)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (database)<br \/>\n\u251c\u2500\u2500 Cluster (prod)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (application)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (database)<br \/>\n\u2502   \u2514\u2500\u2500 Namespace (feature-flags)<\/p>\n<pre><code>\n<h3>5.2 \u547d\u540d\u7a7a\u95f4\u4f7f\u7528\u5efa\u8bae<\/h3>\n\n| \u547d\u540d\u7a7a\u95f4 | \u7528\u9014 | \u793a\u4f8b |\n|---------|------|------|\n| application | \u5e94\u7528\u57fa\u7840\u914d\u7f6e | app.name, app.version |\n| database | \u6570\u636e\u5e93\u914d\u7f6e | db.url, db.username |\n| redis | Redis \u914d\u7f6e | redis.host, redis.port |\n| feature-flags | \u529f\u80fd\u5f00\u5173 | feature.new-ui, feature.beta |\n| custom-xxx | \u81ea\u5b9a\u4e49\u914d\u7f6e | custom.metric, custom.alert |\n\n<h3>5.3 \u5b89\u5168\u5b9e\u8df5<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n\/\/ \u654f\u611f\u914d\u7f6e\u52a0\u5bc6<br \/>\n@Configuration<br \/>\npublic class EncryptedConfigConfig {<\/p>\n<p>    @Value(&#8220;${encrypted.password}&#8221;)<br \/>\n    @Encrypted<br \/>\n    private String password;<br \/>\n}<\/p>\n<p>\/\/ \u914d\u7f6e\u6743\u9650\u63a7\u5236<br \/>\n@Configuration<br \/>\npublic class ConfigSecurityConfig {<\/p>\n<p>    @PreAuthorize(&#8220;hasRole(&#8216;ADMIN&#8217;)&#8221;)<br \/>\n    @GetMapping(&#8220;\/admin\/config&#8221;)<br \/>\n    public Map<String, String> getAdminConfig() {<br \/>\n        return configService.getAdminConfig();<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h2>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n\n<h3>6.1 \u5fae\u670d\u52a1\u914d\u7f6e\u7ba1\u7406<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n@RestController<br \/>\n@RequestMapping(&#8220;\/api&#8221;)<br \/>\npublic class MicroserviceController {<\/p>\n<p>    @Autowired<br \/>\n    private Config config;<\/p>\n<p>    @GetMapping(&#8220;\/service-config&#8221;)<br \/>\n    public ServiceConfig getServiceConfig() {<br \/>\n        ServiceConfig config = new ServiceConfig();<br \/>\n        config.setServiceUrl(config.getProperty(&#8220;service.url&#8221;));<br \/>\n        config.setTimeout(config.getPropertyAsInt(&#8220;service.timeout&#8221;));<br \/>\n        config.setRetryTimes(config.getPropertyAsInt(&#8220;service.retry&#8221;));<br \/>\n        return config;<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h3>6.2 \u52a8\u6001\u529f\u80fd\u5f00\u5173<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n@Component<br \/>\npublic class FeatureToggle {<\/p>\n<p>    @Value(&#8220;${feature.new-ui:false}&#8221;)<br \/>\n    private boolean newUiEnabled;<\/p>\n<p>    @Value(&#8220;${feature.beta-features:false}&#8221;)<br \/>\n    private boolean betaFeaturesEnabled;<\/p>\n<p>    public boolean isNewUiEnabled() {<br \/>\n        return newUiEnabled;<br \/>\n    }<\/p>\n<p>    public boolean areBetaFeaturesEnabled() {<br \/>\n        return betaFeaturesEnabled;<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h3>6.3 \u7070\u5ea6\u53d1\u5e03\u914d\u7f6e<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n@Component<br \/>\npublic class GrayReleaseConfig {<\/p>\n<p>    @Value(&#8220;${gray.release.enabled:false}&#8221;)<br \/>\n    private boolean grayEnabled;<\/p>\n<p>    @Value(&#8220;${gray.release.percentage:10}&#8221;)<br \/>\n    private int releasePercentage;<\/p>\n<p>    @Value(&#8220;${gray.release.user-pattern:*}&#8221;)<br \/>\n    private String userPattern;<\/p>\n<p>    public boolean shouldEnableGrayRelease(String userId) {<br \/>\n        if (!grayEnabled) return false;<br \/>\n        if (!userPattern.matches(userPattern)) return false;<br \/>\n        return new Random().nextInt(100) < releasePercentage;\n    }\n}\n```\n\n\n\n<h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<p>Apollo \u914d\u7f6e\u4e2d\u5fc3\u4ee5\u5176\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u6269\u5c55\u6027\uff0c\u6210\u4e3a\u5206\u5e03\u5f0f\u7cfb\u7edf\u914d\u7f6e\u7ba1\u7406\u7684\u4f18\u9009\u65b9\u6848\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u60a8\u53ef\u4ee5\uff1a<\/p>\n<ol>\n<li><strong>\u7406\u89e3\u6838\u5fc3\u6982\u5ff5<\/strong>\uff1a\u638c\u63e1 Application\u3001Cluster\u3001Namespace \u7b49\u6838\u5fc3\u6982\u5ff5<\/li>\n<li><strong>\u5b8c\u6210\u90e8\u7f72\u914d\u7f6e<\/strong>\uff1a\u5b66\u4f1a\u5b89\u88c5\u548c\u914d\u7f6e Apollo \u670d\u52a1<\/li>\n<li><strong>\u5b9e\u73b0\u914d\u7f6e\u7ba1\u7406<\/strong>\uff1a\u638c\u63e1\u5ba2\u6237\u7aef\u63a5\u5165\u548c\u914d\u7f6e\u8bfb\u53d6\u65b9\u6cd5<\/li>\n<li><strong>\u8fdb\u884c\u4e8c\u6b21\u5f00\u53d1<\/strong>\uff1a\u4e86\u89e3\u5982\u4f55\u6269\u5c55 Apollo \u529f\u80fd<\/li>\n<li><strong>\u5b9e\u8df5\u6700\u4f73\u65b9\u6848<\/strong>\uff1a\u9075\u5faa\u914d\u7f6e\u7ec4\u7ec7\u3001\u547d\u540d\u89c4\u8303\u548c\u5b89\u5168\u7ba1\u7406\u5b9e\u8df5<\/li>\n<\/ul>\n<p>Apollo \u7684\u6301\u7eed\u6f14\u8fdb\u548c\u793e\u533a\u6d3b\u8dc3\uff0c\u4f7f\u5176\u6210\u4e3a\u4f01\u4e1a\u7ea7\u914d\u7f6e\u7ba1\u7406\u7684\u9996\u9009\u5de5\u5177\u3002<\/p>\n<p>&#8212;<\/p>\n<p>*\u672c\u6587\u7ea6 2500 \u5b57\uff0c\u8be6\u7ec6\u8bb2\u89e3\u4e86 Apollo \u914d\u7f6e\u4e2d\u5fc3\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u4e8c\u6b21\u5f00\u53d1\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u53c2\u8003\u6307\u5357\u3002*<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apollo \u914d\u7f6e\u4e2d\u5fc3\u4f7f\u7528\u65b9\u6cd5\u4e0e\u4e8c\u6b21\u5f00\u53d1 \u5f15\u8a00 \u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u67b6\u6784\u4e2d\uff0c\u914d\u7f6e\u7ba1\u7406\u662f\u81f3\u5173\u91cd\u8981\u7684\u4e00\u73af\u3002Apache A&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-15916","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/comments?post=15916"}],"version-history":[{"count":0,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15916\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/media?parent=15916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/categories?post=15916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/tags?post=15916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}