在上一篇文章【ApiBoot Logging使用SpringCloud Openfeign透?jìng)麈溌沸畔?/a>】中我們?cè)敿?xì)的講解了ApiBoot Logging
整合SpringCloud
通過(guò)Openfeign
進(jìn)行透?jìng)麈溌沸畔?,包?code>traceId(鏈路編號(hào))、parentSpanId
(上級(jí)單元編號(hào))等信息。ApiBoot Logging
不僅僅可以使用Openfeign
傳遞鏈路信息,還支持RestTemplate
方式,本篇文章來(lái)詳細(xì)的講解下具體的使用方式。
搭建Logging Admin
我們需要搭建Logging Admin
服務(wù),用于接收業(yè)務(wù)服務(wù)
上報(bào)的請(qǐng)求日志信息,請(qǐng)參考【將ApiBoot Logging采集的日志上報(bào)到Admin】文章內(nèi)容.
添加ApiBoot統(tǒng)一版本
由于本章采用是Maven 多模塊
的方式構(gòu)建源碼,所以我們只需要將ApiBoot
統(tǒng)一版本的依賴配置在root
項(xiàng)目的pom.xml
內(nèi),如下所示:
1.82.1.5.RELEASEorg.minbox.frameworkapi-boot-dependencies${api.boot.version}pomimport
接下來(lái)我們營(yíng)造本篇文章的模擬場(chǎng)景
,查詢用戶基本信息時(shí)一并查詢出用戶的賬號(hào)余額。
創(chuàng)建賬戶服務(wù)
創(chuàng)建一個(gè)名為account-service
的SpringBoot
項(xiàng)目。
添加相關(guān)依賴
在項(xiàng)目pom.xml
配置文件內(nèi)添加相關(guān)依賴,如下所示:
org.springframework.bootspring-boot-starter-weborg.minbox.frameworkapi-boot-starter-logging
配置上報(bào)的Logging Admin
在application.yml
配置文件內(nèi)添加請(qǐng)求日志上報(bào)的Logging Admin
地址,如下所示:
spring:
application:
name: account-service
server:
port: 9090
api:
boot:
logging:
# 控制臺(tái)打印請(qǐng)求日志
show-console-log: true
# 美化請(qǐng)求日志
format-console-log-json: true
# Logging Admin地址
admin:
server-address: 127.0.0.1:8081
注意:server-address
配置參數(shù)不需要添加http://
前綴
啟用Logging Client
添加完成依賴后我們通過(guò)@EnableLoggingClient
注解來(lái)啟用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ù)啟動(dòng)成功.", "賬戶");
}
}
@EnableLoggingClient
注解就實(shí)例化部分ApiBoot Logging
內(nèi)部所需要的類,將實(shí)例放置到Spring IOC
容器內(nèi)。
查詢賬戶余額代碼實(shí)現(xiàn)
我們創(chuàng)建一個(gè)名為AccountController
的控制器來(lái)提供查詢賬戶的余額信息,代碼實(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)編寫(xiě)完成,下面我們來(lái)編寫(xiě)用戶服務(wù)
。
創(chuàng)建用戶服務(wù)
我們來(lái)創(chuàng)建一個(gè)名為user-service
的SpringBoot
項(xiàng)目。
添加相關(guān)依賴
在項(xiàng)目pom.xml
配置文件內(nèi)添加相關(guān)依賴,如下所示:
org.springframework.bootspring-boot-starter-weborg.minbox.frameworkapi-boot-starter-logging
配置上報(bào)的Logging Admin
本章我們使用指定Logging Admin
地址的方式配置,修改application.yml
配置文件如下所示:
spring:
application:
name: user-service
server:
port: 9091
api:
boot:
logging:
# 控制臺(tái)打印請(qǐng)求日志
show-console-log: true
# 美化請(qǐng)求日志
format-console-log-json: true
# Logging Admin地址
admin:
server-address: 127.0.0.1:8081
啟用Logging Client
添加完依賴后我們需要在XxxApplication
入口類上添加@EnableLoggingClient
注解來(lái)啟用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ù)啟動(dòng)成功.", "用戶");
}
}
實(shí)例化RestTemplate對(duì)象
在user-service
需要訪問(wèn)賬戶服務(wù)
獲取當(dāng)前用戶的余額,所以我們需要在user-service
內(nèi)實(shí)例化RestTemplate
,這樣我們才可以通過(guò)RestTemplate
訪問(wèn)獲取用戶賬戶余額信息,我們直接在UserServiceApplication
類內(nèi)添加實(shí)例,如下所示:
/**
* 實(shí)例化RestTemplate
*
* @return {@link RestTemplate}
*/
@Bean
@ConditionalOnMissingBean
public RestTemplate restTemplate() {
return new RestTemplate();
}
注解解釋:
-
@ConditionalOnMissingBean
:這是SpringBoot
條件注入其中的一個(gè)注解,表示當(dāng)IOC
容器內(nèi)不存在RestTemplate
類型的實(shí)例時(shí)才會(huì)去執(zhí)行restTemplate()
方法創(chuàng)建對(duì)象。
查詢用戶信息代碼實(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 用戶編號(hào)
* @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;
}
}
},>
我們所需要的兩個(gè)服務(wù)都已經(jīng)編寫(xiě)完成,下面我們來(lái)測(cè)試RestTemplate
是可以透?jìng)?code>ApiBoot Logging的鏈路信息?
運(yùn)行測(cè)試
依次啟動(dòng)logging-admin
> user-service
> account-service
。
測(cè)試點(diǎn):透?jìng)麈溌沸畔?/h3>
我們使用curl
命令訪問(wèn)user-service
提供的地址/user
,如下所示:
? ~ curl http://localhost:9091/user/1
{"id":1,"name":"恒宇少年","balance":1233.22}
下面我看來(lái)看下logging-admin
控制臺(tái)接收到的請(qǐng)求日志。
接收user-service請(qǐng)求日志
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請(qǐng)求日志
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)我們?cè)L問(wèn)
user-service
服務(wù)內(nèi)的/user
路徑時(shí),因?yàn)槭堑谝淮卧L問(wèn)ApiBoot Logging
會(huì)主動(dòng)創(chuàng)建traceId
(鏈路編號(hào))、spanId
(單元編號(hào)),因?yàn)闆](méi)有上級(jí)單元
所以parentSpanId
為null
. -
而通過(guò)查看
account-service
服務(wù)上報(bào)的請(qǐng)求日志時(shí),可以看到ApiBoot Logging
相關(guān)的鏈路信息是通過(guò)HttpHeader
的方式進(jìn)行傳遞的-
minbox-logging-x-trace-id
->鏈路編號(hào)
-
minbox-logging-x-parent-span-id
->上級(jí)單元編號(hào)
-
敲黑板,劃重點(diǎn)
ApiBoot Logging
在內(nèi)部自動(dòng)化實(shí)現(xiàn)了RestTemplate
的攔截器配置,所以我們只需要?jiǎng)?chuàng)建實(shí)例就可以,而不需要主動(dòng)去配置攔截器信息,具體源碼請(qǐng)?jiān)L問(wèn)org.minbox.framework.logging.client.http.rest.LoggingRestTemplateInterceptor
查看。
不管你一次請(qǐng)求跨度幾個(gè)服務(wù),都可以將請(qǐng)求入口
生成的鏈路信息
進(jìn)行依次傳遞,而上下級(jí)關(guān)系則是根據(jù)parentSpanId
、spanId
進(jìn)行綁定的。
審核編輯 黃昊宇
-
JAVA
+關(guān)注
關(guān)注
20文章
2983瀏覽量
106568 -
MySQL
+關(guān)注
關(guān)注
1文章
841瀏覽量
27371 -
人臉識(shí)別
+關(guān)注
關(guān)注
76文章
4055瀏覽量
83427 -
Template
+關(guān)注
關(guān)注
0文章
8瀏覽量
9518
發(fā)布評(píng)論請(qǐng)先 登錄
請(qǐng)問(wèn)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ù)載均衡上報(bào)日志
ApiBoot Logging忽略路徑不進(jìn)行采集日志的教程
修改ApiBoot Logging日志采集前綴的教程
NuMicro M031BT-利用藍(lán)牙透傳實(shí)現(xiàn)個(gè)人健康信息應(yīng)用的好幫手

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

評(píng)論