首頁>技術>

【實時推送演示】

一、程式連線Apollo配置

1、在專案pom中增加Apollo客戶端依賴

app: id: ${app_ip:gray_apollo}apollo: cluster: ${apollo_cluster:default} bootstrap: enabled: ${apollo_bootstrap:true} name spaces: ${apollo_namespaces:grayapollo} meta: ${apollo_meta:http://127.0.0.1:8080}

3、啟動專案後,可以在Apollo配置中心看到注入一個例項

​ 二、@value型別,程式演示

1、演示程式碼

package com.zhanghan.grayapollo.controller;import com.zhanghan.grayapollo.util.wrapper.WrapMapper;import com.zhanghan.grayapollo.util.wrapper.Wrapper;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestController@Api(value = "演示Apollo控制器",tags = {"演示Apollo控制器"})public class DynamicController { @Value("${zh.int}") private Integer zhInt; @Autowired private RedisTemplate<String, String> strRedisTemplate; @ApiOperation(value="測試通過@value注入資料",tags = {"演示Apollo控制器"}) @RequestMapping(value = "/test/value", method = RequestMethod.POST) public Wrapper testValue() { Map<String, Object> map = new HashMap(); map.put("zhTest", zhInt); return WrapMapper.ok(map); }}

2、啟動專案後,在swagger中訪問

​ 3、在Apollo上修改zh.int的值

​ 4、在Apollo上釋出此次修改

​ 5、檢視專案日誌(發現值已經推送到程式)

​ 6、再次通過swagger訪問此介面(驚奇發現程式值已修改為8)

​ 7、小結:

以上為大家演示,在不用重啟程式情況下,讓配置檔案改變@value讀取值進行更改。

三、專案地址

灰度實戰:github dangnianchuntian/gray

【總結】

1、本節第一部分為大家演示Apollo動態更新@value值,這個功能應用特別實用,比如一些閾值的設定等等。

2、下一篇為大家演示如何接收Apollo動態更新連第三方中介軟體的連線池(以redis連線池為例)。

72

Java

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • Linux驅動程式基石之mmap