Spring Cloud Alibaba - Nacos源码分析

news2024/10/5 14:26:18

目录

一、源码

1、为什么要分析源码

2、看源码的方法

二、Nacos服务注册与发现源码剖析

1、Nacos核心功能点

2、Nacos服务端/客户端原理

2.1、nacos-example

2.2、Nacos-Client测试类

3、项目中实例客户端注册


一、源码

1、为什么要分析源码

1. 提升技术功底:学习源码里面的优秀的设计思想,比如一些问题的解决问题思路,还有一些优秀的设计模式,提升自己的技术功底。
2. 深度掌握框架:源码看多了,对于一个新技术或者框架的掌握速度会有大幅度提升,看下框架的演示Demo就基本上知道了底层实现原理,学习框架的速度会非常快。
3. 快速定位问题:遇到问题,特别是框架源码的Bug问题,能够快速定位,这就是多看源码所带来的的好处和优势。
4. 提高面试成功率:面试一线互联网大厂,一般都会问题到框架源码级别的实现,如果掌握了源码,会大大提升面试成功几率和薪资待遇。
5.  参与开源社区:参与到开源项目的研发,结识更多大牛,对于自己以后的提升好处多多。

2、看源码的方法

1. 先使用:先看官方网站提供的文档,快速掌握框架的基本使用
2. 关注核心功能:在使用的过程中关注框架的核心功能,然后来观察这些核心功能的代码
3. 总结归纳:总结源码中的一些核心点,同时最好能够跟着源码来做出核心流程图,这样就可以把源码中的核心亮点找出并且标记,后续就可能会借鉴到实际工作项目中,同时要善于用Debug,来观看源码的执行过程,观察一些关键变量的值的变化。当我们把框架的所有功能点的源码都分析完成后,回到主流程在梳理一遍,最后在自己脑袋中形成一个闭环,这样源码的核心内容和主流程就基本上理解了。

二、Nacos服务注册与发现源码剖析

1、Nacos核心功能点

服务注册:Nacos Client会通过发送REST请求的方式向Nacos Server注册自己的服务,提供自身的元数据,比如ip地址、端口等信息。Nacos Server接收到注册请求后,就会把这些元数据信息存储在一个双层的内存Map中。 
服务心跳:在服务注册后,Nacos Client会维护一个定时心跳来持续通知Nacos Server,说明服务一直处于可用状态,防止被剔除。默认5s发送一次心跳
服务健康检查:Nacos Server会开启一个定时任务用来检查注册服务实例的健康情况,对于超过15s没有收到客户端心跳的实例会将它的healthy属性置为false(客户端服务发现时不会发现),如果某个实例超过30秒没有收到心跳,直接剔除该实例(被剔除的实例如果恢复发送心跳则会重新注册)
服务发现:服务消费者(Nacos Client)在调用服务提供者的服务时,会发送一个REST请求给Nacos Server,获取上面注册的服务清单,并且缓存在Nacos Client本地,同时会在Nacos Client本地开启一个定时任务定时拉取服务端最新的注册表信息更新到本地缓存
服务同步:Nacos Server集群之间会互相同步服务实例,用来保证服务信息的一致性。 

2、Nacos服务端/客户端原理

 

 Nacos源码下载

Nacos源码地址

设置单机运行
-Dnacos.standalone=true -Dnacos.home=C:\\nacos

 

 启动完访问:http://localhost:8848/nacos   

2.1、nacos-example

NamingExample

public class NamingExample {
    
    public static void main(String[] args) throws NacosException, InterruptedException {
        
        Properties properties = new Properties();
        properties.setProperty("serverAddr", System.getProperty("serverAddr", "localhost"));
        properties.setProperty("namespace", System.getProperty("namespace", "public"));
        
        NamingService naming = NamingFactory.createNamingService(properties);
        
        naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
        
        System.out.println("instances after register: " + naming.getAllInstances("nacos.test.3"));
        
        Executor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
                runnable -> {
                    Thread thread = new Thread(runnable);
                    thread.setName("test-thread");
                    return thread;
                });
        
        naming.subscribe("nacos.test.3", new AbstractEventListener() {
            
            //EventListener onEvent is sync to handle, If process too low in onEvent, maybe block other onEvent callback.
            //So you can override getExecutor() to async handle event.
            @Override
            public Executor getExecutor() {
                return executor;
            }
            
            @Override
            public void onEvent(Event event) {
                System.out.println("serviceName: " + ((NamingEvent) event).getServiceName());
                System.out.println("instances from event: " + ((NamingEvent) event).getInstances());
            }
        });
    
        naming.deregisterInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
        
        Thread.sleep(1000);
    
        System.out.println("instances after deregister: " + naming.getAllInstances("nacos.test.3"));
        
        Thread.sleep(1000);
    }
}

可以从这开始分析

2.2、Nacos-Client测试类

NacosNamingServiceTest
public class NacosNamingServiceTest {
    
    @Rule
    public ExpectedException expectedException = ExpectedException.none();
    
    private NacosNamingService client;
    
    private NamingClientProxy proxy;
    
    private InstancesChangeNotifier changeNotifier;
    
    @Before
    public void before() throws NoSuchFieldException, NacosException, IllegalAccessException {
        Properties prop = new Properties();
        prop.setProperty("serverAddr", "localhost");
        prop.put(PropertyKeyConst.NAMESPACE, "test");
        client = new NacosNamingService(prop);
        // inject proxy
        proxy = mock(NamingHttpClientProxy.class);
        Field serverProxyField = NacosNamingService.class.getDeclaredField("clientProxy");
        serverProxyField.setAccessible(true);
        serverProxyField.set(client, proxy);
        // inject notifier
        changeNotifier = mock(InstancesChangeNotifier.class);
        Field changeNotifierField = NacosNamingService.class.getDeclaredField("changeNotifier");
        changeNotifierField.setAccessible(true);
        changeNotifierField.set(client, changeNotifier);
    }
    
    @Test
    public void testRegisterInstance1() throws NacosException {
        //given
        String serviceName = "service1";
        String ip = "1.1.1.1";
        int port = 10000;
        //when
        client.registerInstance(serviceName, ip, port);
        //then
        verify(proxy, times(1))
                .registerService(eq(serviceName), eq(Constants.DEFAULT_GROUP), argThat(
                        instance -> instance.getIp().equals(ip) && instance.getPort() == port
                                && Math.abs(instance.getWeight() - 1.0) < 0.01f && instance.getClusterName()
                                .equals(Constants.DEFAULT_CLUSTER_NAME)));
    }
.....

实例信息

注册实例信息用Instance对象承载,注册的实例信息又分两部分:实例基础信息和元数据。

- instanceId:实例的唯一ID;
- ip:实例IP,提供给消费者进行通信的地址;
- port: 端口,提供给消费者访问的端口;
- weight:权重,当前实例的权限,浮点类型(默认1.0D);
- healthy:健康状况,默认true;
- enabled:实例是否准备好接收请求,默认true;
- ephemeral:实例是否为瞬时的,默认为true;
- clusterName:实例所属的集群名称;
- serviceName:实例的服务信息;
Instance类包含了实例的基础信息之外,还包含了用于**存储元数据的metadata**(描述数据的数据),类型为HashMap,从当前这个Demo中我们可以得知存放了两个数据:
- netType:顾名思义,网络类型,这里的值为external,也就是外网的意思;
- version:版本,Nacos的版本,这里是2.0这个大版本。
除了Demo中这些“自定义”的信息,在Instance类中还定义了一些默认信息,这些信息通过get方法提供:

@JsonInclude(Include.NON_NULL)
public class Instance implements Serializable {
    /**
     * unique id of this instance.
     */
    private String instanceId;
    
    /**
     * instance ip.
     */
    private String ip;
    
    /**
     * instance port.
     */
    private int port;
    
    /**
     * instance weight.
     */
    private double weight = 1.0D;
    
    /**
     * instance health status.
     */
    private boolean healthy = true;
    
    /**
     * If instance is enabled to accept request.
     */
    private boolean enabled = true;
    
    /**
     * If instance is ephemeral.
     *
     * @since 1.0.0
     */
    private boolean ephemeral = true;
    
    /**
     * cluster information of instance.
     */
    private String clusterName;
    
    /**
     * Service information of instance.
     */
    private String serviceName;
....
    public long getInstanceHeartBeatInterval() {
        return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_INTERVAL,
                Constants.DEFAULT_HEART_BEAT_INTERVAL);
    }
    
    public long getInstanceHeartBeatTimeOut() {
        return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_TIMEOUT,
                Constants.DEFAULT_HEART_BEAT_TIMEOUT);
    }
    
    public long getIpDeleteTimeout() {
        return getMetaDataByKeyWithDefault(PreservedMetadataKeys.IP_DELETE_TIMEOUT,
                Constants.DEFAULT_IP_DELETE_TIMEOUT);
    }
    
    public String getInstanceIdGenerator() {
        return getMetaDataByKeyWithDefault(PreservedMetadataKeys.INSTANCE_ID_GENERATOR,
                Constants.DEFAULT_INSTANCE_ID_GENERATOR);
    }
...
}

上面的get方法在需要元数据默认值时会被用到:
- preserved.heart.beat.interval:心跳间隙的key,默认为5s,也就是默认5秒进行一次心跳;
- preserved.heart.beat.timeout:心跳超时的key,默认为15s,也就是默认15秒收不到心跳,实例将会标记为不健康;
- preserved.ip.delete.timeout:实例IP被删除的key,默认为30s,也就是30秒收不到心跳,实例将会被移除;
- preserved.instance.id.generator:实例ID生成器key,默认为simple;
这些都是Nacos默认提供的值,也就是当前实例注册时会告诉Nacos Server说:我的心跳间隙、心跳超时等对应的值是多少,你按照这个值来判断我这个实例是否健康。
有了这些信息,我们基本是已经知道注册实例时需要传递什么参数,需要配置什么参数了。

NamingService接口

NamingService接口是Nacos命名服务对外提供的一个统一接口,看对应的源码就可以发现,它提供了大量实例相关的接口方法:

//服务实例注册
  void registerInstance(...) throws NacosException;
//服务实例注销
  void deregisterInstance(...) throws NacosException;
//获取服务实例列表
  List<Instance> getAllInstances(...) throws NacosException;
//查询健康服务实例
  List<Instance> selectInstances(...) throws NacosException;
//查询集群中健康的服务实例
 List<Instance> selectInstances(....List<String> clusters....)throws NacosException;
//使用负载均衡策略选择一个健康的服务实例
  Instance selectOneHealthyInstance(...) throws NacosException;
//订阅服务事件
  void subscribe(...) throws NacosException;
//取消订阅服务事件
  void unsubscribe(...) throws NacosException;
//获取所有(或指定)服务名称
  ListView<String> getServicesOfServer(...) throws NacosException;
//获取所有订阅的服务
   List<ServiceInfo> getSubscribeServices() throws NacosException;
//获取Nacos服务的状态
  String getServerStatus();
//主动关闭服务
void shutDown() throws NacosException;

在这些方法中提供了大量的重载方法,应用于不同场景和不同类型实例或服务的筛选,所以我们只需要在不同的情况下使用不同的方法即可。
NamingService的实例化是通过NamingFactory类和上面的Nacos服务信息,从代码中可以看出这里采用了反射机制来实例化NamingService,具体的实现类为NacosNamingService:

public class NamingFactory {
    
    /**
     * Create a new naming service.
     *
     * @param serverList server list
     * @return new naming service
     * @throws NacosException nacos exception
     */
    public static NamingService createNamingService(String serverList) throws NacosException {
        try {
            Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
            Constructor constructor = driverImplClass.getConstructor(String.class);
            return (NamingService) constructor.newInstance(serverList);
        } catch (Throwable e) {
            throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
        }
    }
    
    /**
     * Create a new naming service.
     *
     * @param properties naming service properties
     * @return new naming service
     * @throws NacosException nacos exception
     */
    public static NamingService createNamingService(Properties properties) throws NacosException {
        try {
            Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingService");
            Constructor constructor = driverImplClass.getConstructor(Properties.class);
            return (NamingService) constructor.newInstance(properties);
        } catch (Throwable e) {
            throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
        }
    }
}
NamingExample代码中使用了NamingService的registerInstance方法来进行服务实例的注册,该方法接收参数,服务名称,ip,port和实例对象。这个方法的最大作用是设置了当前实例的分组信息。我们知道,在Nacos中,通过Namespace、group、Service、Cluster等一层层的将实例进行环境的隔离。在这里设置了默认的分组为“DEFAULT_GROUP”。
public class NacosNamingService implements NamingService {
....
    @Override
    public void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException {
        registerInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName);
    }

    @Override
    public void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName)
            throws NacosException {
        Instance instance = new Instance();
        instance.setIp(ip);
        instance.setPort(port);
        instance.setWeight(1.0);
        instance.setClusterName(clusterName);
        registerInstance(serviceName, groupName, instance);
    }
...

紧接着调用的registerInstance方法如下,这个方法实现了两个功能:
第一,检查心跳时间设置的对不对(心跳默认为5秒)
​第二,通过NamingClientProxy这个代理来执行服务注册操作

    @Override
    public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        NamingUtils.checkInstanceIsLegal(instance);//检查心跳
        clientProxy.registerService(serviceName, groupName, instance);//通过代理执行服务注册操作
    }

通过clientProxy我们发现NamingClientProxy这个代理接口的具体实现是有NamingClientProxyDelegate来完成的,这个可以从NacosNamingService构造方法中来看出。

    public NacosNamingService(Properties properties) throws NacosException {
        init(properties);
    }

初始化在init方法中

    private void init(Properties properties) throws NacosException {
        final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
        
        ValidatorUtils.checkInitParam(nacosClientProperties);
        this.namespace = InitUtils.initNamespaceForNaming(nacosClientProperties);
        InitUtils.initSerialization();
        InitUtils.initWebRootContext(nacosClientProperties);
        initLogName(nacosClientProperties);
    
        this.notifierEventScope = UUID.randomUUID().toString();
        this.changeNotifier = new InstancesChangeNotifier(this.notifierEventScope);
        NotifyCenter.registerToPublisher(InstancesChangeEvent.class, 16384);
        NotifyCenter.registerSubscriber(changeNotifier);
        this.serviceInfoHolder = new ServiceInfoHolder(namespace, this.notifierEventScope, nacosClientProperties);
        //在这里进行了初始化,并看出使用的是NamingClientProxyDelegate来完成的
        this.clientProxy = new NamingClientProxyDelegate(this.namespace, serviceInfoHolder, nacosClientProperties, changeNotifier);
    }

根据上方的分析和源码的阅读,我们可以发现NamingClientProxy调用registerService实际上调用的就是NamingClientProxyDelegate的对应方法:

    @Override
    public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
        getExecuteClientProxy(instance).registerService(serviceName, groupName, instance);
    }

真正调用注册服务的并不是代理实现类,而是根据当前实例是否为瞬时对象,来选择对应的客户端代理来进行请求的:

如果当前实例为瞬时对象,则采用gRPC协议(NamingGrpcClientProxy)进行请求,否则采用http协议(NamingHttpClientProxy)进行请求。默认为瞬时对象,也就是说,2.0版本中默认采用了gRPC协议进行与Nacos服务进行交互。

    private NamingClientProxy getExecuteClientProxy(Instance instance) {
        return instance.isEphemeral() ? grpcClientProxy : httpClientProxy;
    }

关于gRPC协议(NamingGrpcClientProxy),我们主要关注一下registerService方法实现,这里其实做了两件事情

1. 缓存当前注册的实例信息用于恢复,缓存的数据结构为ConcurrentMap<String, Instance>,key为“serviceName@@groupName”,value就是前面封装的实例信息。
2. 另外一件事就是封装了参数,基于gRPC进行服务的调用和结果的处理。

    @Override
    public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
        NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName,
                instance);
        redoService.cacheInstanceForRedo(serviceName, groupName, instance);//缓存数据
        doRegisterService(serviceName, groupName, instance);//基于gRPC进行服务的调用
    }

3、项目中实例客户端注册

实际上我们在真实的生产环境中,我们要让某一个服务注册到Nacos中,我们首先要引入一个依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

在引入这个依赖以后,我们要找到SpringBoot自动装配文件META-INF/spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration,\
  com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration,\
  com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
  com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration,\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\
  com.alibaba.cloud.nacos.NacosServiceAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
  com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration

然后再通过SpingBoot的自动装配(首先找到)来加载EnableAutoConfiguration对应的类,然后这里我们就能看见很多Nacos相关的内容,那我们怎么能知道这个服务在注册的时候具体走的时候哪一个,其实一般这种文件我们都会找“Auto”关键子的文件来进行查看,然后我们现在要了解的是客户端的注册,所以我们要找“NacosServiceRegistryAutoConfiguration”。

@Configuration(
    proxyBeanMethods = false
)
@EnableConfigurationProperties
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(
    value = {"spring.cloud.service-registry.auto-registration.enabled"},
    matchIfMissing = true
)
@AutoConfigureAfter({AutoServiceRegistrationConfiguration.class, AutoServiceRegistrationAutoConfiguration.class, NacosDiscoveryAutoConfiguration.class})
public class NacosServiceRegistryAutoConfiguration {
    public NacosServiceRegistryAutoConfiguration() {
    }

    @Bean
    public NacosServiceRegistry nacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
        return new NacosServiceRegistry(nacosDiscoveryProperties);
    }

    @Bean
    @ConditionalOnBean({AutoServiceRegistrationProperties.class})
    public NacosRegistration nacosRegistration(ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers, NacosDiscoveryProperties nacosDiscoveryProperties, ApplicationContext context) {
        return new NacosRegistration((List)registrationCustomizers.getIfAvailable(), nacosDiscoveryProperties, context);
    }

    @Bean
    @ConditionalOnBean({AutoServiceRegistrationProperties.class})
    public NacosAutoServiceRegistration nacosAutoServiceRegistration(NacosServiceRegistry registry, AutoServiceRegistrationProperties autoServiceRegistrationProperties, NacosRegistration registration) {
        return new NacosAutoServiceRegistration(registry, autoServiceRegistrationProperties, registration);
    }
}

NacosAutoServiceRegistration继承了AbstractAutoServiceRegistration而这个类型实现了ApplicationListener接口,所以我们由此得出一般实现ApplicationListener接口的类型都会实现一个方法"onApplicationEvent",这个方法会在项目启动的时候触发

public class NacosAutoServiceRegistration extends AbstractAutoServiceRegistration<Registration> {
    private static final Logger log = LoggerFactory.getLogger(NacosAutoServiceRegistration.class);
    private NacosRegistration registration;

    public NacosAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry, AutoServiceRegistrationProperties autoServiceRegistrationProperties, NacosRegistration registration) {
        super(serviceRegistry, autoServiceRegistrationProperties);//-->enter
        this.registration = registration;
    }
....
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.bind(event);//-->
    }

    /** @deprecated */
    @Deprecated
    public void bind(WebServerInitializedEvent event) {
        ApplicationContext context = event.getApplicationContext();
        if (!(context instanceof ConfigurableWebServerApplicationContext) || !"management".equals(((ConfigurableWebServerApplicationContext)context).getServerNamespace())) {
            this.port.compareAndSet(0, event.getWebServer().getPort());
            this.start();//-->
        }
    }
//然后在start()方法中调用register()方法来注册服务
    public void start() {
        if (!this.isEnabled()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Discovery Lifecycle disabled. Not starting");
            }

        } else {
            if (!this.running.get()) {
                this.context.publishEvent(new InstancePreRegisteredEvent(this, this.getRegistration()));
                this.register();//-->
                if (this.shouldRegisterManagement()) {
                    this.registerManagement();
                }

                this.context.publishEvent(new InstanceRegisteredEvent(this, this.getConfiguration()));
                this.running.compareAndSet(false, true);
            }

        }
    }

分析到这里,我们已经知道了真实服务注册的入口和具体调用那个方法来注册,那我们再来分析一下register这个方法

    protected void register() {
        this.serviceRegistry.register(this.getRegistration());
    }

但是这里要注意serviceRegistry实际上是一个接口,所以我们来看一下它的具体实现类NacosServiceRegistry:

public class NacosServiceRegistry implements ServiceRegistry<Registration> {
...
    public void register(Registration registration) {
        if (StringUtils.isEmpty(registration.getServiceId())) {
            log.warn("No service to register for nacos client...");
        } else {
            NamingService namingService = this.namingService();
            String serviceId = registration.getServiceId();
            String group = this.nacosDiscoveryProperties.getGroup();
            //构建instance实例
            Instance instance = this.getNacosInstanceFromRegistration(registration);

            try {
                //向服务端注册此服务
                namingService.registerInstance(serviceId, group, instance);
                log.info("nacos registry, {} {} {}:{} register finished", new Object[]{group, serviceId, instance.getIp(), instance.getPort()});
            } catch (Exception var7) {
                log.error("nacos registry, {} register failed...{},", new Object[]{serviceId, registration.toString(), var7});
                ReflectionUtils.rethrowRuntimeException(var7);
            }

        }
    }
...

其实到这里大家应该已经明白Nacos客户端的服务注册过程了,但是其实再给大家补充一点,就是其实注册本身就是访问了Nacos提供的一个接口,我们可以在官网上看到

nacos API   注册实例

跟到这里和上面源码部分能衔接上了。可打断点Debug跟踪。

Spring Cloud Alibaba - Nacos

干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/598864.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Golang内存泄露场景与定位方式

个人博客 一、产生原因 Golang有自动垃圾回收机制&#xff0c;但是仍然可能会出现内存泄漏的情况。以下是Golang内存泄漏的常见可能原因&#xff1a; 循环引用&#xff1a;如果两个或多个对象相互引用&#xff0c;且没有其他对象引用它们&#xff0c;那么它们就会被垃圾回收机…

【计算机网络实验】BGP和OSPF协议仿真实验

实验内容  BGP和OSPF协议仿真实验 实验目的 &#xff08;1&#xff09;学习BGP协议的配置方法&#xff1b; &#xff08;2&#xff09;验证BGP协议的工作原理&#xff1b; &#xff08;3&#xff09;掌握网络自治系统的划分方法&#xff1b; &#xff08;4&#xff09;验证…

3分钟快速了解—App自动化测试是怎么实现H5测试的?

移动端 app 自动化框架很多&#xff0c;但是有一些框架因为不支持混合应用测试&#xff0c;一直没有完全流行。比较典型的是经典的 Python 框架 uiautomator2, 这个框架简单好用&#xff0c;没有 appium 那样复杂的 api 调用&#xff0c;受到不少 python 自动化工程师的青睐。 …

C++《stack和queue的一些OJ题目》

本文主要讲解C中stack和queue的一些OJ题目 文章目录 1、[最小栈](https://leetcode.cn/problems/min-stack/)2、[栈的压入、弹出序列](https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId13&&tqId11174&rp1&ru/activity/oj&qru/ta…

路径规划算法:基于乌鸦优化的路径规划算法- 附代码

路径规划算法&#xff1a;基于乌鸦优化的路径规划算法- 附代码 文章目录 路径规划算法&#xff1a;基于乌鸦优化的路径规划算法- 附代码1.算法原理1.1 环境设定1.2 约束条件1.3 适应度函数 2.算法结果3.MATLAB代码4.参考文献 摘要&#xff1a;本文主要介绍利用智能优化算法乌鸦…

驱动开发:内核解析PE结构节表

在笔者上一篇文章《驱动开发&#xff1a;内核解析PE结构导出表》介绍了如何解析内存导出表结构&#xff0c;本章将继续延申实现解析PE结构的PE头&#xff0c;PE节表等数据&#xff0c;总体而言内核中解析PE结构与应用层没什么不同&#xff0c;在上一篇文章中LyShark封装实现了K…

ChatGPT国内免费使用方法【国内免费使用地址】

当下人工智能技术的快速发展&#xff0c;聊天机器人成为了越来越多人们日常生活和工作中的必备工具。如何在国内免费使用ChatGPT聊天机器人&#xff0c;成为了热门话题。本文将为你详细介绍ChatGPT国内免费使用方法&#xff0c;让你轻松拥有聊天机器人助手&#xff0c;提高工作…

基于Halcon卡尺测量: Metrology批量测量矩形和圆

处理图如下: 原图如下: 主要思想: 1.准备一次性将图中12个圆和2个矩形都检测出来 2.初步确定12个圆的圆心坐标和半径,初步确定两个矩形的中心坐标,角度,长短边长 3.创建计量模型 4.定义计量模型尺寸 5.增加检测矩形和圆形的信息参数 6.应用计量模型,获取结果 7.显示…

什么是 IMU?惯性测量单元工作和应用

术语IMU代表“惯性测量单元”&#xff0c;我们用它来描述测量工具的集合。当安装在设备中时&#xff0c;这些工具可以捕获有关设备移动的数据。IMU 包含加速度计、陀螺仪和磁力计等传感器。 IMU 如何工作&#xff1f; IMU 可以测量各种因素&#xff0c;包括速度、方向、加速度、…

vue 单点登录的方法

vue 单点登录的方法 当我们在使用 vue开发项目时&#xff0c;一般都是只有一个用户帐号&#xff0c;如果要实现多个帐号的单点登录&#xff0c;可以使用 Session和 LocalStorage这两个技术。这两个技术在实现单点登录时&#xff0c;都需要有一个用户名和一个密码&#xff0c;而…

【C++】C++ 11 智能指针

【C】C 11 智能指针 文章目录 【C】C 11 智能指针1.为什么需要智能指针2. C中智能指针和指针的区别是什么&#xff1f;3. C中的智能指针有哪些&#xff1f;分别解决的问题以及区别&#xff1f;&#xff08;1&#xff09;auto_ptr&#xff08;C98的方案&#xff0c;C11已经弃用&…

JavaEE进阶(Mybatis)5/31

目录 1. SQL注入 2.concat&#xff08;&#xff09;用于like模糊查询 3.resultMap 4. 5.动态SQL 6.foreach标签 1. SQL注入 $问题会导致SQL注入 因为$是直接替换的&#xff0c;and的优先级高于or true or false #不存在SQL注入的问题&#xff0c;因为他是预编译的&…

坚持的工作好习惯

工作好习惯 目录概述需求&#xff1a; 设计思路实现思路分析1.工作好习惯的重要性2.谈下自己的工作方法2.希望有时也从别人那里也学习一下看看 参考资料和推荐阅读 Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;s…

windows xp 上 Task Scheduler服务 启动后停止,导致数据库自动备份无法编辑和使用问题解决

项目中使用了mysql数据库&#xff0c;大多数人都会选择Navicat来连接数据库&#xff0c;一般会设置计划来自动备份数据库&#xff0c;但我在编辑自动备份的计划时&#xff0c;弹出task scheduler服务无法启动&#xff0c;退出回到桌面&#xff0c;启动windows的服务&#xff0c…

牛客网C++面试宝典(一)C/C++基础之语言基础

此系列为在学习牛客网C面试宝典过程中记录的笔记&#xff0c;本篇记录第一章C/C基础部分的第一节&#xff1a;语言基础。 牛客网C面试宝典链接&#xff1a;https://www.nowcoder.com/issue/tutorial?tutorialId93&uuida34ed23d58b84da3a707c70371f59c21 文章目录 1.1 简述…

Docker 数据持久化方案详解

目录 一、Docker数据持久化概述 1.1联合文件系统 1.2容器的数据卷 1.2.1 什么是数据卷 1.2.2 数据卷特点 1.2.3 Docker提供三种方式将数据从宿主机挂载到容器 二、 Docker持久化方案 2.1 查看volume 基本命令使用方法 2.2 volume持久化方案 2.2.1volume简介 2.2.2.v…

【JavaSE】Java基础语法(四十一):TCP通信程序

文章目录 1. TCP发送数据2. TCP接收数据【应用】3. TCP程序练习4. TCP程序文件上传练习【应用】 1. TCP发送数据 Java中的TCP通信 Java对基于TCP协议的的网络提供了良好的封装&#xff0c;使用Socket对象来代表两端的通信端口&#xff0c;并通过Socket产生IO流来进行网络通信。…

Android 易忽略小知识

1.设置hint的字体大小 在Android xml文件中并没有直接设置hint字体大小的属性。如果hint文字的大小不希望跟正常字体的大小一样&#xff0c;就只能通过代码的方式来进行处理。 提供两种方式&#xff1a; //设置"用户名"提示文字的大小 EditText etUserName (Ed…

教育硬件“老玩家”进入智能手机新赛道,小度胜算几何?

从5月8日有传言称“百度旗下小度将进军智能手机市场”&#xff0c;到5月17日小度官宣将推出旗下新物种产品——小度青禾学习手机&#xff0c;小度在短短10天时间成为市场关注的焦点。 而5月22日&#xff0c;其也拿出了真正的成果&#xff0c;这部专门为青少年打造的学习手机正…

MySQL——在Linux环境下安装(在线安装)

MySQL的安装&#xff08;在线安装&#xff09; mysql的安装并不是比赛的内容&#xff0c;所以我们用比较方便的在线安装的方法&#xff0c;比起安装&#xff0c;我们更要知道如何去使用&#xff1a; 首先看一下自己有没有安装MySQL的服务&#xff0c;或者自己的服务器上有没有…