Bean生命周期主线流程:
https://www.processon.com/view/link/5eafa609f346fb177ba8091f
思考: 为什么整合Nacos注册中心后,服务启动就会自动注册,Nacos是如何实现自动服务注册的?
NacosAutoServiceRegistration
# 对ApplicationListener的扩展 AbstractAutoServiceRegistration#onApplicationEvent # 服务注册 》NacosServiceRegistry#register
监听 WebServerInitializedEvent 这个事件
介绍: https://www.jianshu.com/p/7b8f2a97c8f5
NacosWatch
#对SmartLifecycle的扩展 NacosWatch#start #订阅服务接收实例更改的事件 》NamingService#subscribe
扩展: Eureka Server端上下文的初始化是在SmartLifecycle#start中实现的
EurekaServerInitializerConfiguration
Eureka Server源码分析:
思考:为什么@Bean修饰的RestTemplate加上@LoadBalanced就能实现负载均衡功能?
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
LoadBalancerAutoConfiguration
对SmartInitializingSingleton的扩展,为所有用@LoadBalanced修饰的restTemplate(利用了@Qualifier)绑定实现了负载均衡逻辑的拦截器LoadBalancerInterceptor
LoadBalancerInterceptor
思考:为什么Feign接口可以通过@Autowired直接注入使用?Feign接口是如何交给Spring管理的?
@FeignClient(value = "mall-order",path = "/order")
public interface OrderFeignService {
@RequestMapping("/findOrderByUserId/{userId}")
R findOrderByUserId(@PathVariable("userId") Integer userId);
}
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
OrderFeignService orderFeignService;
@RequestMapping(value = "/findOrderByUserId/{id}")
public R findOrderByUserId(@PathVariable("id") Integer id) {
//feign调用
R result = orderFeignService.findOrderByUserId(id);
return result;
}
}
FeignClientsRegistrar
FeignClientFactorybean
SentinelDataSourceHandler
#Sentinel持久化读数据源设计,利用了SmartInitializingSingleton扩展点
SentinelDataSourceHandler#afterSingletonsInstantiated
# 注册一个FactoryBean类型的数据源
》SentinelDataSourceHandler#registerBean 注册bean定义,通过beanFactory.getBean创建bean
》》NacosDataSourceFactoryBean#getObject
# 利用FactoryBean获取到读数据源
》》new NacosDataSource(properties, groupId, dataId, converter)