SpringCloud(二)负载均衡服务调用Ribbon、服务接口调用OpenFeign案例详解

news2024/9/27 9:30:08

五、负载均衡服务调用Ribbon

技术版本
Spring Cloud版本Hoxton.SR1
Spring Boot版本2.2.2RELEASE
Cloud Alibaba版本2.1.0.RELEASE

Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。

简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法和服务调用。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们很容易使用Ribbon实现自定义的负载均衡算法。

官网:https://github.com/Netflix/ribbon/wiki/Getting-Started

5.1 简介

  • LB负载均衡(Load Balance)是什么
    简单的说就是将用户的请求平摊的分配到多个服务上,从而达到系统的HA(高可用)。常见的负载均衡有软件Nginx,LVS,硬件 F5等。

  • Ribbon本地负载均衡客户端 VS Nginx服务端负载均衡区别
    Nginx是服务器负载均衡,客户端所有请求都会交给nginx,然后由nginx实现转发请求。即负载均衡是由服务端实现的。

    Ribbon本地负载均衡,在调用微服务接口时候,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术。Ribbon概括起来,就是负载均衡+RestTemplate调用

在这里插入图片描述

Ribbon在工作时分成两步

  • 第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server.

  • 第二步再根据用户指定的策略,在从server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略:比如轮询、随机和根据响应时间加权。

在做Eureka集群配置的时候,并没有引入如下Ribbon的配置,就已经实现了负载均衡。

<dependency>    
    <groupId>org.springframework.cloud</groupId>    
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>        

这是因为spring-cloud-starter-netflix-eureka-client自带了spring-cloud-starter-ribbon引用

<!--添加eureka-client-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

在这里插入图片描述

  • RestTemplate调用

getForObject方法/getForEntity方法

getForObject方法: 返回对象为响应体中数据转化成的对象,基本上可以理解为Json

getForEntity方法: 返回对象为ResponseEntity对象,包含了响应中的一些重要信息,比如响应头、响应状态码、响应体等

	@GetMapping("/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }


    @GetMapping("/get/entity/{id}")
    public CommonResult<Payment> getPaymentEntity(@PathVariable("id") Long id) {

        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);

        if(entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else {
            return new CommonResult(444,"操作失败");
        }
    }

postForObject/postForEntity

	@GetMapping("/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL +"/payment/create",payment,CommonResult.class);
    }


    @GetMapping("/entity/create")
    public CommonResult<Payment> createEntity(Payment payment) {
        return restTemplate.postForEntity(PAYMENT_URL +"/payment/create",payment,CommonResult.class).getBody();
    }

5.2 核心组件IRule

5.2.1 IRule实现的负载均衡算法

在这里插入图片描述

  • com.netflix.loadbalancer.RoundRobinRule

    ​ 轮询

  • com.netflix.loadbalancer.RandomRule

    ​ 随机

  • com.netflix.loadbalancer.RetryRule

    ​ 先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内会进行重试,获取可用的服务

  • WeightedResponseTimeRule

    ​ 对RoundRobinRule的扩展,响应速度越快的实例选择权重越大,越容易被选择

  • BestAvailableRule

    ​ 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务

  • AvailabilityFilteringRule

    ​ 先过滤掉故障实例,再选择并发较小的实例

  • ZoneAvoidanceRule

    ​ 默认规则,复合判断server所在区域的性能和server的可用性选择服务器

5.2.2 如何改变负载算法

修改cloud-consumer-order80

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
package com.yyds.myrule;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MySelfRule {
    @Bean
    public IRule myRule(){
        // 随机负载
        return new RandomRule();
    }
}

package com.yyds.springcloud;

import com.yyds.myrule.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

@SpringBootApplication
@EnableEurekaClient
// 添加
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class, args);
        System.out.println("***************OrderMain80启动成功********************");
    }
}

注意:自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,达不到特殊化定制的目的了。

在这里插入图片描述

注意:一些新spring cloud的版本的Eureka starter已经不在集成Ribbon所以新版本不会在使用@RibbonClient(name = "",configuration = "" ) 以及@RibbonClients使用@LoadBalancerClient来进行对@LoadBalanced的配置修改。可以查阅新版本的修改方法。

5.3 负载均衡算法

5.3.1 原理

负载均衡算法:rest接口第几次请求数 % 服务器集群总数量 = 实际调用服务器位置下标  ,每次服务重启动后rest接口计数从1开始。
 
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
 
如:   List [0] instances = 127.0.0.1:8002
    List [1] instances = 127.0.0.1:8001
 
8001 + 8002 组合成为集群,它们共计2台机器,集群总数为2, 按照轮询算法原理:
 
当总请求数为1时: 1 % 2 =1 对应下标位置为1 ,则获得服务地址为127.0.0.1:8001
当总请求数位2时: 2 % 2 =0 对应下标位置为0 ,则获得服务地址为127.0.0.1:8002
当总请求数位3时: 3 % 2 =1 对应下标位置为1 ,则获得服务地址为127.0.0.1:8001
如此类推......

5.3.2 手动实现

  • 支付服务提供者8001和8002接口添加
	@GetMapping(value = "/lb")
    public String getPaymentLB() {
        return serverPort;
    }
  • 在订单服务消费者80中

自定义LoadBalancer接口

package com.yyds.springcloud.myloadbalancer;

import org.springframework.cloud.client.ServiceInstance;

import java.util.List;

public interface LoadBalancer {
    ServiceInstance instances(List<ServiceInstance> serviceInstances);
}

自定义LoadBalancer实现类

package com.yyds.springcloud.myloadbalancer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


@Component
@Slf4j
public class MyLoadBalancer implements LoadBalancer {
    
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    public  final int getAndIncrement(){
        int current;
        int next;
        do{
            current = this.atomicInteger.get();
            next = current >= 2147483647 ? 0 : current + 1;
        }while (!this.atomicInteger.compareAndSet(current,next));
        log.info("--------------next="+ next);
        return next;
    }

    @Override
    public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
        int index = getAndIncrement() % serviceInstances.size();
        return serviceInstances.get(index);
    }
}

ApplicationContextBean去掉注解@LoadBalanced(注意,此时原来接口报异常)

@Configuration
public class ApplicationContextConfig {

    @Bean
    // @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

Controller中添加接口,使用自定义负载均衡策略

	//可以获取注册中心上的服务列表
    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private LoadBalancer loadBalancer;


    @GetMapping("/lb")
    public String getPaymentLB() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0) {
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }

测试 http://localhost/consumer/payment/lb

在这里插入图片描述

5.3.3 RoundRobinRule源码

package com.netflix.loadbalancer;

import com.netflix.client.config.IClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * The most well known and basic load balancing strategy, i.e. Round Robin Rule.
 *
 * @author stonse
 * @author Nikos Michalakis <nikos@netflix.com>
 *
 */
public class RoundRobinRule extends AbstractLoadBalancerRule {

    private AtomicInteger nextServerCyclicCounter;
    private static final boolean AVAILABLE_ONLY_SERVERS = true;
    private static final boolean ALL_SERVERS = false;

    private static Logger log = LoggerFactory.getLogger(RoundRobinRule.class);

    public RoundRobinRule() {
        nextServerCyclicCounter = new AtomicInteger(0);
    }

    public RoundRobinRule(ILoadBalancer lb) {
        this();
        setLoadBalancer(lb);
    }

    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            log.warn("no load balancer");
            return null;
        }

        Server server = null;
        int count = 0;
        while (server == null && count++ < 10) {
            List<Server> reachableServers = lb.getReachableServers();
            List<Server> allServers = lb.getAllServers();
            int upCount = reachableServers.size();
            int serverCount = allServers.size();

            if ((upCount == 0) || (serverCount == 0)) {
                log.warn("No up servers available from load balancer: " + lb);
                return null;
            }
            // 利用下标,从服务提供者中选取服务提供者
            int nextServerIndex = incrementAndGetModulo(serverCount);
            server = allServers.get(nextServerIndex);

            if (server == null) {
                /* Transient. */
                Thread.yield();
                continue;
            }

            if (server.isAlive() && (server.isReadyToServe())) {
                return (server);
            }

            // Next.
            server = null;
        }

        if (count >= 10) {
            log.warn("No available alive servers after 10 tries from load balancer: "
                    + lb);
        }
        return server;
    }

    /**
     * Inspired by the implementation of {@link AtomicInteger#incrementAndGet()}.
     *
     * @param modulo The modulo to bound the value of the counter.
     * @return The next value.
     */
    private int incrementAndGetModulo(int modulo) {
        // 使用AtomicInteger的CAS
        for (;;) {
            int current = nextServerCyclicCounter.get();
            int next = (current + 1) % modulo;
            if (nextServerCyclicCounter.compareAndSet(current, next))
                return next;
        }
    }

    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
    }
}

六、服务接口调用OpenFeign

6.1 简介

  • Feign能干什么

Feign旨在使编写Java Http客户端变得更容易。
前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它,即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。

  • Feign集成了Ribbon

利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用

  • Feign和OpenFeign

​ Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务

​ OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

6.2 OpenFeign使用案例

新建cloud-consumer-feign-order80

6.2.1 pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.yyds</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</artifactId>

    <dependencies>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>


        <!--添加eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--引入自己定义的api通用包-->
        <dependency>
            <groupId>com.yyds</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

6.2.2 yml配置

server:
  port: 80

spring:
  application:
    name: cloud-order-feign-service

# 添加下面配置信息
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
#      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  # 集群版本
    register-with-eureka: true # 是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true

6.2.3 主启动类

package com.yyds.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients // 添加此注解
public class OpenFeignOrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OpenFeignOrderMain80.class, args);
        System.out.println("***************OpenFeignOrderMain80启动成功********************");
    }
}

6.2.4 业务类

业务逻辑接口+@FeignClient配置调用provider服务

package com.yyds.springcloud.service;

import com.yyds.springcloud.entities.CommonResult;
import com.yyds.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping("/payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) ;
}
package com.yyds.springcloud.controller;



import com.yyds.springcloud.entities.CommonResult;
import com.yyds.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yyds.springcloud.service.PaymentFeignService;

import javax.annotation.Resource;


@RestController
@RequestMapping("/consumer/payment")
@Slf4j
public class OrderController {

    @Resource
    private PaymentFeignService service;

    @GetMapping("/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
       return service.getPaymentById(id);
    }

}

6.2.5 测试

http://localhost/consumer/payment/get/1

{"code":200,"message":"查询成功,serverPort+8002","data":{"id":1,"serial":"00001a"}}

6.3 更多用法

6.3.1 超时功能

默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。

#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
  #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

6.3.2 日志打印功能

Feign 提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节。

日志的级别如下:

	NONE:默认的,不显示任何日志;

	BASIC:仅记录请求方法、URL、响应状态码及执行时间;

	HEADERS:除了 BASIC 中定义的信息之外,还有请求和响应的头信息;

	FULL:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据。

配置日志Bean

package com.yyds.springcloud.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {
    
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
}

Yml文件

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.yyds.springcloud.service.PaymentFeignService: debug

在这里插入图片描述

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

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

相关文章

DOS经典软件,落下帷幕,新型国产平台,蓬勃发展

提起DOS时代&#xff0c;总让人难以忘怀&#xff0c;陷入深深回忆中&#xff0c;风靡一时的许多软件&#xff0c;如今早已不在&#xff0c;这几款被称为DOS必装的软件&#xff0c;更是让人惋惜。 你还记得这图吗&#xff1f;堪称DOS系统最经典的软盘复制与映像生成软件&#xf…

八十九——一三三

八十九、JavaScript——数组的简介 一、数组 数组(Array) - 数组也是一中复合数据类型&#xff0c;在数组可以存储多个不同类型的数据 - 数组中存储的是有序的数据&#xff0c;数组中的每个数据有一个唯一的索引 可以通过索引来操作获取数据 - 数据中存储的数据叫元素 - 索引&…

从 MVC 架构到三层(3-Tier)架构

一、MVC 存在的痛点问题 对于业务逻辑不甚复杂的场景&#xff0c;MVC 尚能胜任。但随着前端 MVVM&#xff08;Model-View-View-Model&#xff09;开发模式的兴起&#xff0c;尤其是前端框架如 Vue、React 的普及&#xff0c;服务端的 MVC 设计模式使用场景变得越来越少&#x…

NetSuite Intercompany Framework 101

今朝&#xff0c;谈一谈Intercompany Framework&#xff0c;这是一个彰显NetSuite市场野心的基础功能框架。从20.2开始逐渐浮出水面&#xff0c;虽然经过过往的几个版本&#xff0c;不断推出组成功能&#xff0c;但目前仍然未见其全貌。 作为顾问&#xff0c;你必须关注它&…

详解JavaScript的形参,实参以及传参

文章目录 前言一、参数是什么&#xff1f;二、形参和实参 1.形参 2.实参三、传参 1.参数传递的对应关系2.两个传参的例子 总结前言 编程初学者在接触JavaScript这门语言时&#xff0c;很难搞懂里面的逻辑&#xff0c;这就会导致入门慢&#xff0c;入门难。这种难度一般…

Banana Pi BPI-R3 评测:详细信息、功能

Banana Pi BPI-R3 路由板著名的 Banana Pi 品牌背后的公司 Sinovoip Co., Ltd 刚刚宣布了一款名为 Banana Pi BPI-R3 的带有两个 SFP 端口的新型开源路由器。它可能是市场上首批具有内置光接口的单板路由器之一。这种出色的产品对于连接到快速光纤互联网的用户特别有益&#xf…

【PyTorch学习4】《PyTorch深度学习实践》——线性回归(Linear Regression)

目录一、实现框架二、程序实现三、代码讲解1.self.linear torch.nn.Linear(1, 1)2.model(x_data)3.criterion torch.nn.MSELoss(reductionsum)&#xff0c;loss criterion(y_pred, y_data)一、实现框架 1、Prepare dataset 2、Design model using Class (inherit from nn.M…

【Leedcode】顺序表必备的三道面试题(附图解)

顺序表必备的三道面试题&#xff08;附图解&#xff09; 文章目录顺序表必备的三道面试题&#xff08;附图解&#xff09;前言一、第一题1.题目2.思路图解3.源码二、第二题1.题目2.思路图解3.源码三、第三题1.题目2.思路图解3.源码总结前言 本文给大家介绍三道顺序表学习过程中…

【项目精选】javaEE土地档案管理系统(源码+论文+视频)

技术&#xff1a;java、jsp、struts、spring、hibernate 数据库&#xff1a;oracle 集成开发工具&#xff1a;eclipse 点击下载源码 本土地项目管理系统在可行性研究的基础上&#xff0c;是为了进一步明确土地项目管理系统的软件需求&#xff0c;以便安排项目规划和进度&#x…

ARM+LINUX嵌入式学习路线

嵌入式学习是一个循序渐进的过程&#xff0c;如果是希望向嵌入式软件方向发展的话&#xff0c;目前最常见的是嵌入式Linux方向&#xff0c;关注这个方向&#xff0c;大概分3个阶段&#xff1a; 1、嵌入式linux上层应用&#xff0c;包括QT的GUI开发 2、嵌入式linux系统开发 3、…

Python|每日一练|字符串|递归|链表|单选记录:字符串转换整数 (atoi)|阶乘后的零| K 个一组翻转链表

1、字符串转换整数 (atoi)&#xff08;字符串&#xff09; 请你来实现一个 myAtoi(string s) 函数&#xff0c;使其能将字符串转换成一个 32 位有符号整数&#xff08;类似 C/C 中的 atoi 函数&#xff09;。 函数 myAtoi(string s) 的算法如下&#xff1a; 读入字符串并丢弃…

Golang 方法笔记

Golang中的方法是作用在指定的数据类型上的&#xff0c;因此自定义类型都可以有方法。方法定义func (recevier type) methodName (参数列表) (返回值列表) {方法体return 返回值}基本申明和调用type A struct {Num int}func (a A) test() {fmt.Println(a.Num)}说明&#xff1a;…

Python快速上手系列--邮件发送--详解篇

本章就来一起学习一下跑完自动化脚本后如何自动的发送邮件到指定的邮箱。zmail操作&#xff1a;1. 导包 import zmail2. 邮件内容&#xff0c;包含&#xff1a;主题(subject)、正文(content_text)、附件(attachments)3. 发件人信息&#xff0c;包含&#xff1a;发件人账号&…

什么牌的运动耳机比较好、运动耳机排行榜10强

现在运动健身的潮流持续不下&#xff0c;而且人们长期坐于办公室办公&#xff0c;严重影响身体的健康&#xff0c;这时不论是去健身房锻炼&#xff0c;还是户外跑步都是非常必要的了&#xff0c;而蓝牙耳机作为运动必备的一款数码产品&#xff0c;更是受到了大家的青睐&#xf…

2023/02/18 ES6对象属性的解读

1 属性的可枚举性和遍历 <script>const obj {userName: zhaoshuai-lc,userAge: 26,userSex: male}let res Object.getOwnPropertyDescriptors(obj);console.log(res); </script>描述对象的 [ enumerable ] 属性, 称为“可枚举性”, 如果该属性为 [ false ], 就表…

如何实现外网访问内网ip?公网端口映射或内网映射来解决

本地搭建服务器应用&#xff0c;在局域网内可以访问&#xff0c;但在外网不能访问。如何实现外网访问内网ip&#xff1f;主要有两种方案&#xff1a;路由器端口映射和快解析内网映射。根据自己本地网络环境&#xff0c;结合是否有公网IP&#xff0c;是否有路由权限&#xff0c;…

0.4如何使用cmake来管理项目

如何使用cmake来管理项目 【opencv源码解析0.1】VS如何优雅的配置opencv环境 【opencv源码解析0.2】如何编译opencv库源码 【opencv源码解析0.3】调试opencv源码以及使用cmake来管理项目 前面几篇文章我们都是围绕Visual Studio 2019这个IDE来展开的&#xff0c;IDE为我们做了…

【OJ】小熊猫的肉质品

&#x1f4da;Description: 自从可爱的小熊猫来到浙商大后便再也不想吃那些膳食纤维了&#xff0c;比如&#xff1a;竹子。所以&#xff0c;骞哥&#xfeff;不得不帮助国宝寻找一些肉类来维持能量&#xff0c;使得小熊猫不至于饿死在全球某工商。但是&#xff0c;你知道的淘…

Spring Cloud Alibaba--seata微服务详解之分布式事务(三)

上篇讲述gateway的部署和使用&#xff0c;gateway统一管理和转发了HTTP请求&#xff0c;在互联网中大型项目一定存在复杂的业务关系&#xff0c;尤其在商城类软件中如淘宝、PDD等商城&#xff0c;尤其在秒杀场景中&#xff0c;并发量可以到达千万级别&#xff0c;此时数据库就会…

第五十六章 树状数组(一)

第五十六章 树状数组一、前缀和的缺陷二、树状数组1、作用2、算法分析3、算法实现&#xff08;1&#xff09;lowbits()&#xff08;2&#xff09;插入&#xff08;3&#xff09;查询三、例题1、问题题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1提示2、代码一、前缀和…