开发规范


自动刷新配置数据

<p>自动刷新配置数据 新建一个maven工程,修改pom文件</p> <parent> <groupId>com.ailk.ecs</groupId> <artifactId>ecs-framework-parent</artifactId> <version>1.0.4</version> </parent> <pre><code>&lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;com.ailk.ecs&lt;/groupId&gt; &lt;artifactId&gt;ecs-framework&lt;/artifactId&gt; &lt;version&gt;${parent.version}&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;mainClass&gt;com.ailk.ecs.Application&lt;/mainClass&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt;</code></pre> <p>配置文件 需增加一个名为bootstrap.yml的配置文件</p> <p>spring: application: name: test #指定服务名称 profiles: active: prod #指定环境,是生产还是测试 cloud: zookeeper: enabled: true # true:开启zookeeper外部化配置, false:读取本地配置; 需要将config.enabled,config.watcher.enabled同时设置 connect-string: 10.143.130.64:32181 #zk地址 config: enabled: true watcher: enabled: true autorefresh.package: com.ailk #指定扫描的包路径,可以不填,多个逗号分隔,default:com.ailk 使用 直接从zk里面获取配置值</p> <p>如果不加注解@RefreshScope只会启动时从zk获取,zk里面的值修改后,这个类不会生效 该value在zk里面的path(图片示例的参数)为:/config/test,prod/d/test 通过zk通知,然后从库中获取</p> <p>新建一个类,这个类在配置文件bootstrap.yml中指定的spring.cloud.zookeeper.config.autorefresh.package包路径下, 同时要实现接口CuratorWatcher, 然后添加注解@AutoRefresh,并指定path路径,如指定的value值为d.test,那么对应的zk的path路径则为/config/test,prod/d/test</p>

页面列表

ITEM_HTML