restTemplate、skywalking
restTemplate
其他示例***********
https://www.cnblogs.com/xingminghui/articles/12005676.html
服务
@RequestMapping(value = "/ignoreMsg", method = {RequestMethod.GET})
public ResultMsg ignoreMsg(@ApiParam(value = "ids", required = true) @RequestParam List<String> ids) {
调用 http://localhost:8786/jms/mail/ignoreMsg?ids=123,123123,21
提供者
@PostMapping("/pageList")
public ResultMsg<PagerResult> pageList(@RequestBody TelephoneVoicePageRequest request) {
调用方
RestTemplate restTemplate = new RestTemplate();
String url = "http://xxx";
Map map = new HashMap<>();
map.put("pageNum", 1);
map.put("pageSize", 10);
map.put("totalPages", 0);
JSONObject s = new JSONObject(map);
System.out.println(s.toString());
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
headers.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9..5TYnV1wmgdBrNPRIkDYZbmFAVzd03vv7IvR0fllulSo");
HttpEntity<String> requestEntity = new HttpEntity<String>(s.toString(),headers);
// ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST,requestEntity,
// String.class);
// System.out.println(responseEntity.getBody());
// ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(url, HttpMethod.POST,requestEntity,
// JSONObject.class);
// System.out.println(responseEntity.getBody());
ResponseEntity<com.fasterxml.jackson.databind.JsonNode> responseEntity = restTemplate.exchange(url,
HttpMethod.POST,requestEntity, JsonNode.class);
System.out.println(responseEntity.getBody());
if (responseEntity.getStatusCode() == HttpStatus.OK) {
JsonNode obj = responseEntity.getBody().path("resultData").path("list");
obj.forEach(node -> {
System.out.println(node.path("handleUsername"));
});
}
}
调用方
HttpEntity<Map> requestEntity = new HttpEntity<Map>(map,headers);
@Bean
public RestTemplate restTemplate(){
RestTemplate rt = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
//60s
requestFactory.setConnectTimeout(60*1000);
requestFactory.setReadTimeout(60*1000);
rt.setRequestFactory(requestFactory);
return rt;
String code = "HcRZHl";
RestTemplate restTemplate = new RestTemplate();
String url = "http://127.0.0.1:8801/oauth/token";
url = url + "?code=" + code;
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("client_id", "N111110000693225666K00161_2.0.9@10018");
formData.add("client_secret","");
formData.add("redirect_uri", "http://www.baidu.com");
formData.add("grant_type", "authorization_code");
ResponseEntity<String> respon = restTemplate.postForEntity(
url,
formData,
String.class
);
System.out.println("***********respon:"+respon);
System.out.println(respon.getBody());
String url = "http://127.0.0.1:8080/apis/user/getUserInfoById?id=xxx";
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer 3c84bf11-787c-47e9-b4c1-71e9d0440a7f");
HttpEntity entity = new HttpEntity<>(null, headers);
ResponseEntity<JsonNode> response = restTemplate.exchange(url, HttpMethod.GET,entity, JsonNode.class);
System.out.println("***********response:"+response);
//{"resultCode":0,"resultMsg":"SUCCESS","resultData":{"birthday":null,"userAccount":""
if (response.getStatusCode() == HttpStatus.OK) {
JsonNode body = response.getBody();
String userAccount = body.path("resultData").path("userAccount").asText();
System.out.println("************userAccount:"+userAccount);
}
//{"code":1,"msg":"操作成功","data":[{"completedTotal":213,"total":3000,"uncompletedTotal":2787}]}
if(res.getStatusCode() == HttpStatus.OK){
JsonNode node = res.getBody().path("data");
JsonNode node2 = node.path(0);
hcpStatistic = JSON.parseObject(node2.toString(),HcpStatistic.class);
}
Unicode转码:
JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject("{'authority':'\\u6cb3\\u6d25\\u5e02\\u516c\\u5b89\\u5c40','start':'20110808'}" );
System.out.println(jsonObject);
File file = FileUtil.newFile("C:\\Users\\邢明辉\\Desktop" +"\\图片\\IDcard_back1.png");
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
FileSystemResource resource = new FileSystemResource(file);
param.add("image", resource);
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
headers.setContentLength(file.length());
headers.setContentDispositionFormData("media", file.getName());
headers.setAcceptCharset(Lists.newArrayList(StandardCharsets.UTF_8));
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
ResponseEntity<String> dataResult = restTemplate.postForEntity(identifyUrl, httpEntity, String.class);
//解析返回的数据
String resultBody = dataResult.getBody();
skywalking 应用性能监控
https://skywalking.apache.org/downloads/#Foundations 下载地址 不可放到有中文名的目录下 E:\soft\apache-skywalking-apm-10.0.1 双击启动 startup http://127.0.0.1:8080/ 设置环境变量,启动自己的应用服务 -javaagent:E:\soft\skywalking-agent\skywalking-agent.jar -Dskywalking.agent.service_name=xmh-service -Dskywalking.collector.backend_service=127.0.0.1:11800
Skywalking链路追踪 https://mp.weixin.qq.com/s/MOjSmYyEr5w2Gpl9Rv8Kew
<!-- skywalking融合logback -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>9.2.0</version>
</dependency>
<!-- 使用gRpc将日志发送到skywalking服务端 默认存储到内存中-->
<property name="pattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level [%thread] %logger %line [%X{traceIdxmh}] [%tid] - %msg%n"/>
<appender name="GRPC_LOG" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<Pattern>${pattern}</Pattern>
</layout>
</encoder>
</appender>
<root level="info">
<appender-ref ref="GRPC_LOG" />
</root>
以下是配置MDC,非必须,skywalking记录的是tid
@Bean
public FilterRegistrationBean indexEsgFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean(new TraceIdFilter());
return registration;
}
public class TraceIdFilter implements Filter {
private static final String UNIQUE_ID = "traceIdxmh";
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String traceId = httpServletRequest.getHeader(UNIQUE_ID);
if (StrUtil.isBlank(traceId)) {
traceId = UUID.randomUUID().toString().replace("-", "");
}
// 将traceId放入MDC中,这样我们就可以在日志中打印出来了。
System.out.println("TraceIdFilter.doFilter" + traceId);
MDC.put(UNIQUE_ID, traceId);
try {
chain.doFilter(request, response);
} finally {
MDC.remove(UNIQUE_ID);
}
}
}
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${pattern}</pattern>
</layout>
</encoder>
</appender>



浙公网安备 33010602011771号