Skip to content

Commit e925be1

Browse files
author
zhengyangyong
committed
add log service in infrastructure zone
Signed-off-by: zhengyangyong <yangyong.zheng@huawei.com>
1 parent a591f30 commit e925be1

File tree

11 files changed

+209
-3
lines changed

11 files changed

+209
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.apache.servicecomb.scaffold.log.api;
2+
3+
import java.util.Date;
4+
5+
public class LogDTO {
6+
private String userName;
7+
8+
private String serviceName;
9+
10+
private String operationName;
11+
12+
private Date invokeTime;
13+
14+
public String getUserName() {
15+
return userName;
16+
}
17+
18+
public String getServiceName() {
19+
return serviceName;
20+
}
21+
22+
public String getOperationName() {
23+
return operationName;
24+
}
25+
26+
public Date getInvokeTime() {
27+
return invokeTime;
28+
}
29+
30+
public LogDTO() {
31+
}
32+
33+
public LogDTO(String userName, String serviceName, String operationName, Date invokeTime) {
34+
this.userName = userName;
35+
this.serviceName = serviceName;
36+
this.operationName = operationName;
37+
this.invokeTime = invokeTime;
38+
}
39+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.apache.servicecomb.scaffold.log.api;
2+
3+
public interface LogService {
4+
boolean record(LogDTO log);
5+
}

edge-service/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<artifactId>slf4j-log4j12</artifactId>
2525
</dependency>
2626

27+
<dependency>
28+
<groupId>org.apache.servicecomb.scaffold</groupId>
29+
<artifactId>common</artifactId>
30+
</dependency>
31+
2732
<!--增加使用Apollo作为配置中心-->
2833
<dependency>
2934
<groupId>com.netflix.archaius</groupId>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.apache.servicecomb.scaffold.edge.filter;
2+
3+
import java.util.Date;
4+
5+
import org.apache.commons.lang3.StringUtils;
6+
import org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncRestTemplate;
7+
import org.apache.servicecomb.scaffold.edge.EdgeFilter;
8+
import org.apache.servicecomb.scaffold.log.api.LogDTO;
9+
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
10+
import org.springframework.http.HttpEntity;
11+
12+
import io.vertx.ext.web.RoutingContext;
13+
14+
public class LogFilter implements EdgeFilter {
15+
16+
private static final String LOG_SERVICE_NAME = "infrastructure:log-service";
17+
18+
private final CseAsyncRestTemplate restTemplate = new CseAsyncRestTemplate();
19+
20+
@Override
21+
public int getOrder() {
22+
return 1;
23+
}
24+
25+
@Override
26+
public void processing(String serviceName, String operationName, RoutingContext context)
27+
throws InvocationException {
28+
//Log记录失败应不影响业务逻辑,异步请求无需等待
29+
String userName = context.request().headers().get(AuthenticationFilter.EDGE_AUTHENTICATION_NAME);
30+
if (StringUtils.isNotEmpty(userName)) {
31+
HttpEntity<LogDTO> request = new HttpEntity<>(
32+
new LogDTO(userName, serviceName, operationName, new Date()));
33+
try {
34+
restTemplate.postForEntity("cse://" + LOG_SERVICE_NAME + "/record", request, Boolean.class);
35+
} catch (Exception ignored) {
36+
}
37+
}
38+
}
39+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
org.apache.servicecomb.scaffold.edge.filter.AuthenticationFilter
1+
org.apache.servicecomb.scaffold.edge.filter.AuthenticationFilter
2+
org.apache.servicecomb.scaffold.edge.filter.LogFilter

log-service/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>scaffold</artifactId>
7+
<groupId>org.apache.servicecomb.scaffold</groupId>
8+
<version>0.0.8-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>log-service</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.apache.servicecomb.scaffold</groupId>
17+
<artifactId>common</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.servicecomb</groupId>
25+
<artifactId>spring-boot-starter-provider</artifactId>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<!--package executable jar-->
32+
<plugin>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-maven-plugin</artifactId>
35+
<executions>
36+
<execution>
37+
<goals>
38+
<goal>repackage</goal>
39+
</goals>
40+
<configuration>
41+
<outputDirectory>${project.build.directory}/bin</outputDirectory>
42+
<classifier>exec</classifier>
43+
</configuration>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
<plugin>
48+
<groupId>org.commonjava.maven.plugins</groupId>
49+
<artifactId>directory-maven-plugin</artifactId>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
54+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.apache.servicecomb.scaffold.log;
2+
3+
import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@EnableServiceComb
9+
public class LogApplication {
10+
public static void main(String[] args) {
11+
SpringApplication.run(LogApplication.class, args);
12+
}
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.apache.servicecomb.scaffold.log;
2+
3+
import org.apache.servicecomb.provider.rest.common.RestSchema;
4+
import org.apache.servicecomb.scaffold.log.api.LogDTO;
5+
import org.apache.servicecomb.scaffold.log.api.LogService;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
12+
import com.fasterxml.jackson.core.JsonProcessingException;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
15+
@RestSchema(schemaId = "log")
16+
@RequestMapping(path = "/")
17+
public class LogServiceImpl implements LogService {
18+
19+
private static final Logger LOGGER = LoggerFactory.getLogger(LogServiceImpl.class);
20+
21+
private static final ObjectMapper OBJ_MAPPER = new ObjectMapper();
22+
23+
@Override
24+
@PostMapping(path = "record")
25+
public boolean record(@RequestBody LogDTO log) {
26+
try {
27+
//简化处理,只是打印一下日志
28+
LOGGER.info(OBJ_MAPPER.writeValueAsString(log));
29+
} catch (JsonProcessingException ignored) {
30+
}
31+
return true;
32+
}
33+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#ServiceComb²»ÐèÒªtomcatÈÝÆ÷
2+
spring.main.web-environment=false
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
APPLICATION_ID: infrastructure
2+
service_description:
3+
name: log-service
4+
version: 0.0.1
5+
properties:
6+
allowCrossApp: true
7+
servicecomb:
8+
service:
9+
registry:
10+
address: http://127.0.0.1:30100
11+
rest:
12+
address: 0.0.0.0:9091
13+
server:
14+
thread-count: 8

0 commit comments

Comments
 (0)