时间:2021-07-01 10:21:17 帮助过:2人阅读
2. 建立自己的TraceRepository和Trace
package com.kevin.config.client; import java.util.List; import java.util.Map; public interface CustomeTraceRepository { List<CustomTrace> findAll(); void add(Map<String, Object> arg0); }
package com.kevin.config.client; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Map; public class CustomTraceRepositoryImpl implements CustomeTraceRepository { private int capacity = 100; private boolean reverse = true; private final List<CustomTrace> traces = new LinkedList(); public void setReverse(boolean reverse) { List arg1 = this.traces; synchronized (this.traces) { this.reverse = reverse; } } public void setCapacity(int capacity) { List arg1 = this.traces; synchronized (this.traces) { this.capacity = capacity; } } public List<CustomTrace> findAll() { List arg0 = this.traces; System.out.println(this.traces.size()); synchronized (this.traces) { return Collections.unmodifiableList(new ArrayList(this.traces)); } } public void add(Map<String, Object> map) { CustomTrace trace = new CustomTrace(new Date(), map); List arg2 = this.traces; synchronized (this.traces) { while (this.traces.size() >= this.capacity) { this.traces.remove(this.reverse ? this.capacity - 1 : 0); } if (this.reverse) { this.traces.add(0, trace); } else { this.traces.add(trace); } } } }
package com.kevin.config.client; import java.util.Date; import java.util.Map; import org.springframework.util.Assert; public final class CustomTrace { private final Date timestamp; private final Map<String, Object> info; public CustomTrace(Date timestamp, Map<String, Object> info) { Assert.notNull(timestamp, "Timestamp must not be null"); Assert.notNull(info, "Info must not be null"); this.timestamp = timestamp; this.info = info; } public Date getTimestamp() { return this.timestamp; } public Map<String, Object> getInfo() { return this.info; } }
3. 建立自己的监听器,监听SentApplicationEvent和AckRemoteApplicationEvent
package com.kevin.config.client; import java.util.LinkedHashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.cloud.bus.event.AckRemoteApplicationEvent; import org.springframework.cloud.bus.event.SentApplicationEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class TraceListener { private static Log log = LogFactory.getLog(TraceListener.class); private CustomeTraceRepository repository; public TraceListener(CustomeTraceRepository repository) { this.repository = repository; } @EventListener public void onAck(AckRemoteApplicationEvent event) { this.getReceivedTrace(event); } @EventListener public void onSend(SentApplicationEvent event) { this.getSentTrace(event); } protected Map<String, Object> getSentTrace(SentApplicationEvent event) { LinkedHashMap map = new LinkedHashMap(); map.put("signal", "spring.cloud.bus.sent"); map.put("type", event.getType().getSimpleName()); map.put("id", event.getId()); map.put("origin", event.getOriginService()); map.put("destination", event.getDestinationService()); if (log.isDebugEnabled()) { log.debug(map); } this.repository.add(map); return map; } protected Map<String, Object> getReceivedTrace(AckRemoteApplicationEvent event) { LinkedHashMap map = new LinkedHashMap(); map.put("signal", "spring.cloud.bus.ack"); map.put("event", event.getEvent().getSimpleName()); map.put("id", event.getAckId()); map.put("origin", event.getOriginService()); map.put("destination", event.getAckDestinationService()); if (log.isDebugEnabled()) { log.debug(map); } this.repository.add(map); return map; } }
4. 让spring管理建立的repository
@Bean public CustomTraceRepositoryImpl customeTraceRepository() { return new CustomTraceRepositoryImpl(); }
5 .最后暴露一个接口从repository获取数据
@RequestMapping(value = "/trace") public List<CustomTrace> trace(HttpServletRequest request) throws IOException { return customeTraceRepository().findAll(); }
至此,就可以通过这个接口获取事件追踪记录,本例子中repository默认存储100条,如果超出就会覆盖首尾的记录,如果有其他特殊需求可以自行实现。
注意:在建立自己的repository和trace的时候类的名字最好不要和spring里面的一样,否则会报一些莫名的错误,具体原因没有细究,反正我是改了名字就可以。
springcloudBus 2.x 事件追踪
标签:work size receive set text str request value ring