Season Peng

我在这里记录着美好


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 搜索

配置中心

发表于 2019-11-25 | 分类于 微服务
字数统计: 376

配置中心

阅读全文 »

限流算法

发表于 2019-11-23 | 分类于 微服务
字数统计: 869

限流算法

阅读全文 »

HashMap

发表于 2019-11-13 | 分类于 Java知识
字数统计: 1.4k

HashMap

阅读全文 »

ConcurrentHashMap

发表于 2019-11-12 | 分类于 JDK
字数统计: 2.3k

ConcurrentHashMap

java7

java7中的ConcurrentHashMap主要使用分段锁保证线程安全。只能有一个线程对segment中的table[]进行写操作,读操作不加锁。

阅读全文 »

BigDecimal

发表于 2019-11-06 | 分类于 Java知识
字数统计: 288

BigDecimal和BigInteger

在商业上进行计算的时候,如果使用float、double型变量,会造成丢失精度的问题。这种情况下应该使用BigDecimal。

阅读全文 »

Pigeon

发表于 2019-11-05 | 分类于 微服务
字数统计: 833

Pigeon

调用者会有以下的配置,ReferenceBean是代理类,被代理的是远程服务。

阅读全文 »

责任链模式

发表于 2019-11-03 | 分类于 设计模式
字数统计: 667

责任链模式

责任链模式的定义:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系, 将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。这里就不再过多的介绍什么是责任链模式,主要来说说java中如何编写。主要从下面3个框架中的代码中介绍。

  • servlet中的filter
  • dubbo中的filter
  • mybatis中的plugin 这3个框架在实现责任链方式不尽相同。

servlet中的Filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public final class ApplicationFilterChain implements FilterChain {
private int pos = 0; //当前执行filter的offset
private int n; //当前filter的数量
private ApplicationFilterConfig[] filters; //filter配置类,通过getFilter()方法获取Filter
private Servlet servlet

@Override
public void doFilter(ServletRequest request, ServletResponse response) {
if (pos < n) {
ApplicationFilterConfig filterConfig = filters[pos++];
Filter filter = filterConfig.getFilter();
filter.doFilter(request, response, this);
} else {
// filter都处理完毕后,执行servlet
servlet.service(request, response);
}
}
}

代码还算简单,结构也比较清晰,定义一个Chain,里面包含了Filter列表和servlet,达到在调用真正servlet之前进行各种filter逻辑。

Dubbo中的Filter

Dubbo在创建Filter的时候是另外一个方法,通过把Filter封装成 Invoker的匿名类,通过链表这样的数据结构来完成责任链,核心代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static <T> Invoker<T> buildInvokerChain(final Invoker<T> invoker, String key, String group) {
Invoker<T> last = invoker;
//只获取满足条件的Filter
List<Filter> filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);
if (filters.size() > 0) {
for (int i = filters.size() - 1; i >= 0; i --) {
final Filter filter = filters.get(i);
final Invoker<T> next = last;
last = new Invoker<T>() {
...
public Result invoke(Invocation invocation) throws RpcException {
return filter.invoke(next, invocation);
}
...
};
}
}
return last;
}

Mybatis中的Plugin

Mybatis可以配置各种Plugin,无论是官方提供的还是自己定义的,Plugin和Filter类似,就在执行Sql语句的时候做一些操作。Mybatis的责任链则是通过动态代理的方式,使用Plugin代理实际的Executor类。(这里实际还使用了组合模式,因为Plugin可以嵌套代理),核心代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Plugin implements InvocationHandler{
private Object target;
private Interceptor interceptor;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (满足代理条件) {
return interceptor.intercept(new Invocation(target, method, args));
}
return method.invoke(target, args);
}

//对传入的对象进行代理,可能是实际的Executor类,也可能是Plugin代理类
public static Object wrap(Object target, Interceptor interceptor) {

Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}
}

Reference

https://www.cnblogs.com/z-test/p/9319116.html

Guava限流

发表于 2019-10-31 | 分类于 微服务
字数统计: 486

Guava限流

在Guava中提供的限流算法是令牌桶算法。默认是SmoothBursty模式。

阅读全文 »

algorithms

发表于 2019-10-20 | 分类于 算法
字数统计: 5.2k

摊还分析法

阅读全文 »

MESI

发表于 2019-10-19 | 分类于 Java知识
字数统计: 1.3k

MESI

CPU运算速度很快,内存运算速度很慢,它们在运算速度上有着较大的差距,如果让CPU等待内存处理完数据再进行下一步操作,会造成CPU资源浪费,为了解决这个问题,CPU高速缓存应运而生。

阅读全文 »
12345

Season Peng

45 日志
21 分类
61 标签
© 2014 — 2020 Season Peng
由 Hexo 强力驱动
|
主题 — NexT.Mist