在上一篇文章【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-service的SpringBoot項目。
添加相關(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-service的SpringBoot項目。
添加相關(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路徑時,因?yàn)槭堑谝淮卧L問ApiBoot Logging會主動創(chuàng)建traceId(鏈路編號)、spanId(單元編號),因?yàn)闆]有上級單元所以parentSpanId為null. -
而通過查看
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)行綁定的。
審核編輯 黃昊宇
-
JAVA
+關(guān)注
關(guān)注
20文章
2997瀏覽量
115788 -
MySQL
+關(guān)注
關(guān)注
1文章
897瀏覽量
29263 -
人臉識別
+關(guān)注
關(guān)注
77文章
4119瀏覽量
87867 -
Template
+關(guān)注
關(guān)注
0文章
8瀏覽量
9600
發(fā)布評論請先 登錄
請問AT支持UDP透傳嗎?
最新推出低功耗藍(lán)牙透傳模塊
物聯(lián)網(wǎng)中的數(shù)據(jù)透傳
物聯(lián)網(wǎng)中的數(shù)據(jù)透傳
ApiBoot Logging Admin可視化界面管理日志教程
ApiBoot Logging使用Spring Cloud Openfeign透傳鏈路信息
ApiBoot Logging整合Spring Cloud Eureka負(fù)載均衡上報日志
ApiBoot Logging忽略路徑不進(jìn)行采集日志的教程
修改ApiBoot Logging日志采集前綴的教程
NuMicro M031BT-利用藍(lán)牙透傳實(shí)現(xiàn)個人健康信息應(yīng)用的好幫手

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