Decorative image frame

leetcode回顾——number-of-islands

题目

给定一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例 1:

输入:

11110
11010
11000
00000

输出: 1

Read More...

Nacos疑问之为什么我服务明明下线了却还是可以调用到?

疑问

之前在参与nacos的开发过程中,有不少同学都在问,为什么我在nacos console中将服务进行下线了,但是这个被下线的服务还是可以被调用到,这不太符合官方宣称的秒级上下线特点呀。经过进一步询问发现,那些存在说实例下线后依旧可以对外提供服务的问题,有一个共同的特点——都有rabbion这个负载均衡的组件。因此本文将从两个方面探讨这个问题:nacos的秒级上下线的实现方式以及rabbion的实例更新机制导致实例上下线感知延迟

Nacos 秒级上下线

Read More...

Nacos 是如何剔除非健康实例的

Nacos Client 心跳上报

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
26
27
28
29
30
31
32
33
34
35
36
37
38
class BeatProcessor implements Runnable {

@Override
public void run() {
try {
for (Map.Entry<String, BeatInfo> entry : dom2Beat.entrySet()) {
BeatInfo beatInfo = entry.getValue();
if (beatInfo.isScheduled()) {
continue;
}
beatInfo.setScheduled(true);
executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS);
}
} catch (Exception e) {
NAMING_LOGGER.error("[CLIENT-BEAT] Exception while scheduling beat.", e);
} finally {
executorService.schedule(this, clientBeatInterval, TimeUnit.MILLISECONDS);
}
}
}

class BeatTask implements Runnable {

BeatInfo beatInfo;

public BeatTask(BeatInfo beatInfo) {
this.beatInfo = beatInfo;
}

@Override
public void run() {
long result = serverProxy.sendBeat(beatInfo);
beatInfo.setScheduled(false);
if (result > 0) {
clientBeatInterval = result;
}
}
}
Read More...

Spring中Bean的Scope周期的详解

Bean的使用范围有几种?

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
26
27
28
29
30
31
32
33
34
35
36
37
38
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {

/**
* Alias for {@link #scopeName}.
* @see #scopeName
*/
@AliasFor("scopeName")
String value() default "";

/**
* Specifies the name of the scope to use for the annotated component/bean.
* <p>Defaults to an empty string ({@code ""}) which implies
* {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SINGLETON}.
* @since 4.2
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
* @see ConfigurableBeanFactory#SCOPE_SINGLETON
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
* @see #value
*/
@AliasFor("value")
String scopeName() default "";

/**
* Specifies whether a component should be configured as a scoped proxy
* and if so, whether the proxy should be interface-based or subclass-based.
* <p>Defaults to {@link ScopedProxyMode#DEFAULT}, which typically indicates
* that no scoped proxy should be created unless a different default
* has been configured at the component-scan instruction level.
* <p>Analogous to {@code <aop:scoped-proxy/>} support in Spring XML.
* @see ScopedProxyMode
*/
ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;

}

通过Spring的注解Scope可以知道,对于Bean的使用方位有四种——singletonprototypesessionrequest,那么这几种Bean的使用范围是如何实现的呢?首先要看Spring中的一个重要的接口——BeanDefinition

Read More...

nacos是如何知道哪些客户端订阅了某项服务的

为什么会存在订阅者?

因为存在某些需求,我的服务不想注册到服务发现中心去,我只是想安安静静的做一个索取者,我只想获取可以调用的服务而已

nacos是如何知道哪些客户端订阅了服务的

在nacos-client中,如果要获取服务实例,那么会调用这个方法

1
2
// String queryList(String serviceName, String clusters, int udpPort, boolean healthyOnly)
serverProxy.queryList(serviceName, clusters, 0, false);

在这个方法中看到了一个参数udpPort,等等会用到;这个方法最终调用的HTTP-URL为/v1/ns/instance/list,接着去看nacos-naming-server对应的该controller做了什么事情

Read More...

Nacos 是如何同时实现AP与CP的

两种一致性策略如何在nacos中共存

或许会有疑问,为什么早先的cp模式的Zookeeper或者AP模式的Eureka,都只有支持CAP理论下大家常用的AP实现或者CP实现,而nacos却能够两个都实现呢?

其实CAP理论,仅仅是针对分布式下数据的一致性而言,如果你对于数据的一致性要求不高,可忍受最终一致性,那么AP模式的Eureka就可以满足你了,如果说你对数据的一致性要求很高,那么就使用CP模式的Zookeeper,而追其根本,并不是说EurekaAP的,或者说ZookeeperCP的,而是他们存储的数据的一致性,满足AP或者CP,因此也就不难实现在一个组件中实现AP模式与CP模式共存

1
2
3
4
5
6
7
8
9
@Service("consistencyDelegate")
public class DelegateConsistencyServiceImpl implements ConsistencyService {

@Autowired
private PersistentConsistencyService persistentConsistencyService;

@Autowired
private EphemeralConsistencyService ephemeralConsistencyService;
}
Read More...

记一次Java程序CPU占用率100%的问题排查、定位、解决

错误告知

今天收到阿里云的警告邮件,告知我的实例当前CPU负载过高,当时就奇怪了,怎么实例负载过高了?于是登录的服务器查看当前CPU以及系统负载情况,果真CPU当前负载为百分之百,并且是自己部署上去的Java应用程序,于是开始进行错误信息定位

错误定位

  • 使用jps or lsof -i:{port} 得到目前Java程序的PID
  • 使用命令ps -mp <PID> -o THREAD,tid,time进行打印所有的线程信息状态
  • java pid
  • 筛选出CPU占用率较高的线程tid
  • 使用命令printf "%x\n" tid得到线程的16进制表示
  • printf
  • 使用jstack <PID> > xxx.txt将当前Java程序的线程栈打印出来,然后根据上面得到的线程的16进制表示,定位到具体的线程进行具体分析
  • jstack

代码定位

Read More...

牛客网——剑指offer之数据流中的中位数

题目

如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。

题解

这道题可以采用二分查找插入位置来做,因为是数据流的形式,我只要确保上次一插入操作的结果有序,那么利用二分查找插入位置,每次排序的时间复杂度均为O(log(N))

Read More...