0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

ApiBoot Logging使用Rest Template透傳鏈路信息

電子設(shè)計 ? 來源:電子設(shè)計 ? 作者:電子設(shè)計 ? 2020-12-10 22:03 ? 次閱讀

在上一篇文章【ApiBoot Logging使用SpringCloud Openfeign透傳鏈路信息】中我們詳細(xì)的講解了ApiBoot Logging整合SpringCloud通過Openfeign進(jìn)行透傳鏈路信息,包括traceId(鏈路編號)、parentSpanId(上級單元編號)等信息。
ApiBoot Logging不僅僅可以使用Openfeign傳遞鏈路信息,還支持RestTemplate方式,本篇文章來詳細(xì)的講解下具體的使用方式。

搭建Logging Admin

我們需要搭建Logging Admin服務(wù),用于接收業(yè)務(wù)服務(wù)上報的請求日志信息,請參考【將ApiBoot Logging采集的日志上報到Admin】文章內(nèi)容.

添加ApiBoot統(tǒng)一版本

由于本章采用是Maven 多模塊的方式構(gòu)建源碼,所以我們只需要將ApiBoot統(tǒng)一版本的依賴配置在root項目的pom.xml內(nèi),如下所示:

1.82.1.5.RELEASEorg.minbox.frameworkapi-boot-dependencies${api.boot.version}pomimport

接下來我們營造本篇文章的模擬場景,查詢用戶基本信息時一并查詢出用戶的賬號余額。

創(chuàng)建賬戶服務(wù)

創(chuàng)建一個名為account-serviceSpringBoot項目。

添加相關(guān)依賴

在項目pom.xml配置文件內(nèi)添加相關(guān)依賴,如下所示:

org.springframework.bootspring-boot-starter-weborg.minbox.frameworkapi-boot-starter-logging

配置上報的Logging Admin

application.yml配置文件內(nèi)添加請求日志上報的Logging Admin地址,如下所示:

spring:
  application:
    name: account-service
server:
  port: 9090

api:
  boot:
    logging:
      # 控制臺打印請求日志
      show-console-log: true
      # 美化請求日志
      format-console-log-json: true
      # Logging Admin地址
      admin:
        server-address: 127.0.0.1:8081
注意:server-address配置參數(shù)不需要添加http://前綴

啟用Logging Client

添加完成依賴后我們通過@EnableLoggingClient注解來啟用ApiBoot Logging,在AccountServiceApplication類上添加如下所示:

/**
 * 賬戶服務(wù)
 *
 * @author 恒宇少年
 */
@SpringBootApplication
@EnableLoggingClient
public class AccountServiceApplication {
    /**
     * logger instance
     */
    static Logger logger = LoggerFactory.getLogger(AccountServiceApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(AccountServiceApplication.class, args);
        logger.info("{}服務(wù)啟動成功.", "賬戶");
    }
}
@EnableLoggingClient注解就實(shí)例化部分ApiBoot Logging內(nèi)部所需要的類,將實(shí)例放置到Spring IOC容器內(nèi)。

查詢賬戶余額代碼實(shí)現(xiàn)

我們創(chuàng)建一個名為AccountController控制器來提供查詢賬戶的余額信息,代碼實(shí)現(xiàn)如下所示:

/**
 * 賬戶服務(wù)實(shí)現(xiàn)
 *
 * @author 恒宇少年
 */
@RestController
@RequestMapping(value = "/account")
public class AccountController {

    /**
     * 示例,內(nèi)存賬戶列表
     */
    static final HashMap ACCOUNTS = new HashMap() {{
        put(1, 1233.22);
        put(2, 69269.22);
    }};

    /**
     * 獲取指定賬戶的余額
     *
     * @param accountId
     * @return
     */
    @GetMapping(value = "/{accountId}")
    public Double getBalance(@PathVariable("accountId") Integer accountId) {
        return ACCOUNTS.get(accountId);
    }
},>
至此我們的賬戶服務(wù)已經(jīng)編寫完成,下面我們來編寫用戶服務(wù)。

創(chuàng)建用戶服務(wù)

我們來創(chuàng)建一個名為user-serviceSpringBoot項目。

添加相關(guān)依賴

在項目pom.xml配置文件內(nèi)添加相關(guān)依賴,如下所示:

org.springframework.bootspring-boot-starter-weborg.minbox.frameworkapi-boot-starter-logging

配置上報的Logging Admin

本章我們使用指定Logging Admin地址的方式配置,修改application.yml配置文件如下所示:

spring:
  application:
    name: user-service
server:
  port: 9091

api:
  boot:
    logging:
      # 控制臺打印請求日志
      show-console-log: true
      # 美化請求日志
      format-console-log-json: true
      # Logging Admin地址
      admin:
        server-address: 127.0.0.1:8081

啟用Logging Client

添加完依賴后我們需要在XxxApplication入口類上添加@EnableLoggingClient注解來啟用ApiBoot Logging,如下所示:

/**
 * 用戶服務(wù)
 *
 * @author 恒宇少年
 */
@SpringBootApplication
@EnableLoggingClient
public class UserServiceApplication {
    /**
     * logger instance
     */
    static Logger logger = LoggerFactory.getLogger(UserServiceApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
        logger.info("{}服務(wù)啟動成功.", "用戶");
    }
}

實(shí)例化RestTemplate對象

user-service需要訪問賬戶服務(wù)獲取當(dāng)前用戶的余額,所以我們需要在user-service內(nèi)實(shí)例化RestTemplate,這樣我們才可以通過RestTemplate訪問獲取用戶賬戶余額信息,我們直接在UserServiceApplication類內(nèi)添加實(shí)例,如下所示:

    /**
     * 實(shí)例化RestTemplate
     *
     * @return {@link RestTemplate}
     */
    @Bean
    @ConditionalOnMissingBean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

注解解釋:

  • @ConditionalOnMissingBean:這是SpringBoot條件注入其中的一個注解,表示當(dāng)IOC容器內(nèi)不存在RestTemplate類型的實(shí)例時才會去執(zhí)行restTemplate()方法創(chuàng)建對象。

查詢用戶信息代碼實(shí)現(xiàn)

/**
 * 用戶基本信息控制器
 *
 * @author 恒宇少年
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    /**
     * 示例,用戶列表
     */
    static final HashMap USERS = new HashMap() {{
        put(1, new User(1, "恒宇少年"));
        put(2, new User(2, "于起宇"));
    }};
    /**
     * 注入RestTemplate
     */
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 獲取用戶基本信息
     *
     * @param userId 用戶編號
     * @return
     */
    @GetMapping(value = "/{userId}")
    public User getUserInfo(@PathVariable("userId") Integer userId) {
        ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:9090/account/{accountId}", Double.class, userId);
        Double balance = responseEntity.getBody();
        User user = USERS.get(userId);
        if (ObjectUtils.isEmpty(user)) {
            throw new RuntimeException("用戶:" + userId + ",不存在.");
        }
        user.setBalance(balance);
        return user;
    }

    @Data
    public static class User {
        private Integer id;
        private String name;
        private Double balance;

        public User(Integer id, String name) {
            this.id = id;
            this.name = name;
        }
    }
},>

我們所需要的兩個服務(wù)都已經(jīng)編寫完成,下面我們來測試RestTemplate是可以透傳ApiBoot Logging的鏈路信息?

運(yùn)行測試

依次啟動logging-admin > user-service > account-service。

測試點(diǎn):透傳鏈路信息

我們使用curl命令訪問user-service提供的地址/user,如下所示:

? ~ curl http://localhost:9091/user/1
{"id":1,"name":"恒宇少年","balance":1233.22}

下面我看來看下logging-admin控制臺接收到的請求日志。

接收user-service請求日志

Receiving Service: 【user-service -> 127.0.0.1】, Request Log Report,Logging Content:[
    {
        "endTime":1573032865311,
        "httpStatus":200,
        "requestBody":"",
        "requestHeaders":{
            "host":"localhost:9091",
            "user-agent":"curl/7.64.1",
            "accept":"*/*"
        },
        "requestIp":"0:0:0:0:0:0:0:1",
        "requestMethod":"GET",
        "requestParam":"{}",
        "requestUri":"/user/1",
        "responseBody":"{/"id/":1,/"name/":/"恒宇少年/",/"balance/":1233.22}",
        "responseHeaders":{},
        "serviceId":"user-service",
        "serviceIp":"127.0.0.1",
        "servicePort":"9091",
        "spanId":"f8cff018-42d5-481f-98df-c19b7196b3c3",
        "startTime":1573032865130,
        "timeConsuming":181,
        "traceId":"16ad1dd4-beaa-4110-b4b7-fc7d952d9a57"
    }
]

接收account-service請求日志

Receiving Service: 【account-service -> 127.0.0.1】, Request Log Report,Logging Content:[
    {
        "endTime":1573032865309,
        "httpStatus":200,
        "parentSpanId":"f8cff018-42d5-481f-98df-c19b7196b3c3",
        "requestBody":"",
        "requestHeaders":{
            "minbox-logging-x-parent-span-id":"f8cff018-42d5-481f-98df-c19b7196b3c3",
            "minbox-logging-x-trace-id":"16ad1dd4-beaa-4110-b4b7-fc7d952d9a57",
            "host":"localhost:9090",
            "connection":"keep-alive",
            "accept":"application/json, application/*+json",
            "user-agent":"Java/1.8.0_211"
        },
        "requestIp":"127.0.0.1",
        "requestMethod":"GET",
        "requestParam":"{}",
        "requestUri":"/account/1",
        "responseBody":"1233.22",
        "responseHeaders":{},
        "serviceId":"account-service",
        "serviceIp":"127.0.0.1",
        "servicePort":"9090",
        "spanId":"63b18b40-5718-431c-972f-78956ce78380",
        "startTime":1573032865307,
        "timeConsuming":2,
        "traceId":"16ad1dd4-beaa-4110-b4b7-fc7d952d9a57"
    }
]
  • 當(dāng)我們訪問user-service服務(wù)內(nèi)的/user路徑時,因為是第一次訪問ApiBoot Logging會主動創(chuàng)建traceId(鏈路編號)、spanId(單元編號),因為沒有上級單元所以parentSpanIdnull.
  • 而通過查看account-service服務(wù)上報的請求日志時,可以看到ApiBoot Logging相關(guān)的鏈路信息是通過HttpHeader的方式進(jìn)行傳遞的

    • minbox-logging-x-trace-id -> 鏈路編號
    • minbox-logging-x-parent-span-id -> 上級單元編號

敲黑板,劃重點(diǎn)

ApiBoot Logging在內(nèi)部自動化實(shí)現(xiàn)了RestTemplate的攔截器配置,所以我們只需要創(chuàng)建實(shí)例就可以,而不需要主動去配置攔截器信息,具體源碼請訪問org.minbox.framework.logging.client.http.rest.LoggingRestTemplateInterceptor查看。

不管你一次請求跨度幾個服務(wù),都可以將請求入口生成的鏈路信息進(jìn)行依次傳遞,而上下級關(guān)系則是根據(jù)parentSpanId、spanId進(jìn)行綁定的。

審核編輯 黃昊宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • JAVA
    +關(guān)注

    關(guān)注

    19

    文章

    2943

    瀏覽量

    104110
  • MySQL
    +關(guān)注

    關(guān)注

    1

    文章

    789

    瀏覽量

    26286
  • 人臉識別
    +關(guān)注

    關(guān)注

    76

    文章

    3998

    瀏覽量

    81364
  • Template
    +關(guān)注

    關(guān)注

    0

    文章

    8

    瀏覽量

    9456
收藏 人收藏

    評論

    相關(guān)推薦

    請問AT支持UDP嗎?

    做了測試,SDK V1.00 AT文件燒寫后,設(shè)定為TCP時,是可以了, 但是設(shè)定為UDP后,傳出現(xiàn)發(fā)送不成功,但是接收是可以的,是不是AT不支持UDP
    發(fā)表于 07-17 08:28

    最新推出低功耗藍(lán)牙模塊

    注意啦!最新推出藍(lán)牙模塊,模塊可以省去藍(lán)牙底層的軟硬件的時間投入及成本投入,以最短的開發(fā)周期整合現(xiàn)有的方案基于NRF51822的
    發(fā)表于 11-05 13:47

    物聯(lián)網(wǎng)中的數(shù)據(jù)

    摘要:在農(nóng)業(yè)工業(yè)等場景應(yīng)用中,存在很多場景是有線網(wǎng)絡(luò)無法達(dá)到的,借助物聯(lián)網(wǎng)技術(shù)使用無線數(shù)據(jù)采集與傳輸能很好的解決這一個問題,在這個數(shù)據(jù)傳輸過程中為了保證數(shù)據(jù)的可靠性和準(zhǔn)確性,通常會涉及到“數(shù)據(jù)
    發(fā)表于 08-29 19:27

    物聯(lián)網(wǎng)中的數(shù)據(jù)

    摘要:在農(nóng)業(yè)工業(yè)等場景應(yīng)用中,存在很多場景是有線網(wǎng)絡(luò)無法達(dá)到的,借助物聯(lián)網(wǎng)技術(shù)使用無線數(shù)據(jù)采集與傳輸能很好的解決這一個問題,在這個數(shù)據(jù)傳輸過程中為了保證數(shù)據(jù)的可靠性和準(zhǔn)確性,通常會涉及到“數(shù)據(jù)
    發(fā)表于 09-03 14:17

    什么是?

    的概念概念具體到此次實(shí)踐
    發(fā)表于 12-24 07:13

    什么是藍(lán)牙

    一.什么是藍(lán)牙是最簡單也是最常見的通訊方式(如下圖所示),也叫串口
    發(fā)表于 12-06 06:15

    ApiBoot Logging Admin可視化界面管理日志教程

    ApiBoot Logging Admin支持界面可視化查看請求日志信息,初期版本支持查看上報日志的服務(wù)列表、最新的日志等功能,還可以整
    的頭像 發(fā)表于 12-10 22:03 ?439次閱讀

    ApiBoot Logging使用Spring Cloud Openfeign信息

    ApiBoot Logging可以無縫整合SpringCloud來采集請求日志,目前支持RestTemplate、Openfeign兩種方式,我們本章來講解下在使用Openfeign...
    的頭像 發(fā)表于 12-10 22:05 ?473次閱讀

    ApiBoot Logging整合Spring Cloud Eureka負(fù)載均衡上報日志

    ApiBoot Logging支持整合服務(wù)注冊中心(Eureka、Consul、Nacos Discovery、Zookeeper...)進(jìn)行上報請求日志,Logging Client會從服務(wù)注...
    的頭像 發(fā)表于 12-10 22:08 ?350次閱讀

    ApiBoot Logging忽略路徑不進(jìn)行采集日志的教程

    ApiBoot Logging支持排除指定路徑不參與日志的采集,當(dāng)我們的服務(wù)集成actuator時,會不斷的重復(fù)調(diào)用內(nèi)置的路徑導(dǎo)致大量采集到一些無關(guān)業(yè)...
    的頭像 發(fā)表于 12-10 22:18 ?342次閱讀

    修改ApiBoot Logging日志采集前綴的教程

    ApiBoot Logging支持指定單個或者多個路徑的前綴進(jìn)行采集,也就是我們可以指定/user/**或者/order/**下的單個或者同時指定多個路徑進(jìn)行...
    的頭像 發(fā)表于 12-10 22:20 ?380次閱讀

    minbox-logging日志分析框架

    gitee-minbox-logging.zip
    發(fā)表于 04-27 10:07 ?1次下載
    minbox-<b class='flag-5'>logging</b><b class='flag-5'>鏈</b><b class='flag-5'>路</b>日志分析框架

    NuMicro M031BT-利用藍(lán)牙實(shí)現(xiàn)個人健康信息應(yīng)用的好幫手

    NuMicro M031BT-利用藍(lán)牙實(shí)現(xiàn)個人健康信息應(yīng)用的好幫手
    的頭像 發(fā)表于 08-10 11:31 ?480次閱讀
    NuMicro M031BT-利用藍(lán)牙<b class='flag-5'>透</b><b class='flag-5'>傳</b>實(shí)現(xiàn)個人健康<b class='flag-5'>信息</b>應(yīng)用的好幫手

    輕松搞懂和非的區(qū)別

    和非是數(shù)據(jù)通信中的兩種不同模式,各自有其適用場景和優(yōu)勢。模式簡單、高效,適用于數(shù)據(jù)完
    的頭像 發(fā)表于 06-05 12:03 ?5686次閱讀
    輕松搞懂<b class='flag-5'>透</b><b class='flag-5'>傳</b>和非<b class='flag-5'>透</b><b class='flag-5'>傳</b>的區(qū)別

    網(wǎng)絡(luò)和非是什么區(qū)別是什么

    :適用簡易、立即的數(shù)據(jù)傳輸,留意數(shù)據(jù)的完整性和低延遲。非:適用必須進(jìn)行數(shù)據(jù)解決優(yōu)化的狀況,提供良好的可靠性和效果。 在挑選傳輸技術(shù)時,務(wù)必根據(jù)實(shí)際的業(yè)務(wù)需求、數(shù)據(jù)種類、安全規(guī)
    的頭像 發(fā)表于 08-07 16:19 ?347次閱讀