# 从浅入深 学习 SpringCloud 微服务架构(四)Ribbon

2024-04-25 23:52

本文主要是介绍# 从浅入深 学习 SpringCloud 微服务架构(四)Ribbon,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

从浅入深 学习 SpringCloud 微服务架构(四)Ribbon

段子手168

一、ribbon 概述以及基于 ribbon 的远程调用。

1、ribbon 概述:

Ribbon 是 Netflixfa 发布的一个负载均衡器,有助于控制 HTTP 和 TCP客户端行为。

在 SpringCloud 中 Eureka 一般配合 Ribbon 进行使用,
Ribbon 提供了客户端负载均衡的功能,Ribbon 利用从 Eureka 中读取到的服务信息,
在调用服务节点提供的服务时,会合理的进行负载。

在 SpringCloud 中可以将注册中心和 Ribbon 配合使用,
Ribbon 自动的从注册中心中获取服务提供者的列表信息,并基于内置的负载均衡算法,请求服务。

2、Ribbon 的主要作用:

1)服务调用

基于 Ribbon 实现服务调用,是通过拉取到的所有服务列表组成(服务名-请求路径的)映射关系。
借助 RestTemplate 最终进行调用。

2)负载均衡

当有多个服务提供者时,Ribbon 可以根据负载均衡的算法自动的选择需要调用的服务地址。

3、修改 order_service 子工程(子模块)相关类

1)修改 order_service 子工程(子模块)中的 controller 类 OrderController.java

使用 基于 ribbon 的形式调用远程微服务,使用服务名称 service-product 替换 IP 地址 。

/***  C:\java-test\idea2019\spring_cloud_demo\order_service\src\main\java\djh\it\order\controller\OrderController.java**  2024-4-19 订单的 controller 类 OrderController.java*/
package djh.it.order.controller;import djh.it.order.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;import java.util.List;@RestController
@RequestMapping("/order")
public class OrderController {// 注入 restTemplate 对象@Autowiredprivate RestTemplate restTemplate;/***  注入 DiscoveryClient : springcloud 提供的获取元数据的工具类。*      调用方法获取服务的元数据信息。*/@Autowiredprivate DiscoveryClient discoveryClient;/***  使用 基于 ribbon 的形式调用远程微服务:*  1、使用 @LoadBalanced 注解 声明 RestTemplate*  2、使用服务名称 service-product 替换 IP 地址 。** @param id* @return*/@RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)public Product findById(@PathVariable Long id){Product product = null;//product = restTemplate.getForObject("http://127.0.0.1:9001/product/1", Product.class);product = restTemplate.getForObject("http://service-product/product/1", Product.class);return product;}/***  参数:商品的 id*      通过订单系统,调用商品服务根据id查询商品信息。*          1)需要配置商品对象。*          2)需要调用商品服务。*   可以使用 java 中的 urlconnection, HttpClient, OkHttp 进行远程调用。*   也可以使用 restTemplate 进行远程调用。** @param id* @return*/
//    @RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)
//    public Product findById(@PathVariable Long id){
//
//        //调用 discoveryClient 方法,已调用服务名称获取所有的元数据。
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//        for (ServiceInstance instance : instances) {
//            System.out.println(instance);
//        }
//
//        //获取唯一的一个元数据
//        ServiceInstance instance = instances.get(0);
//
//        Product product = null;
//
//        //根据元数据中的主机地址和端口号拼接请求微服务的 URL
//        product = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/product/1", Product.class);
//
//
//        /**
//         *  调用商品服务(将微服务的请求路径硬编码到 java 代码中)
//         *  存在问题:对微服务调用的负载均衡,加入API网关,配置的统一管理,链路追踪。
//         */
//        //product = restTemplate.getForObject("http://127.0.0.1:9001/product/1", Product.class);
//        return product;
//    }
}

2)修改 order_service 子工程(子模块)中的 启动类 OrderApplication.java

使用 基于 ribbon 的形式调用远程微服务,使用 @LoadBalanced 注解 声明 RestTemplate。


/***   C:\java-test\idea2019\spring_cloud_demo\order_service\src\main\java\djh\it\order\OrderApplication.java**   2024-4-19  启动类 OrderApplication.java*/
package djh.it.order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EntityScan("djh.it.order.domain")
@EnableEurekaClient  //激活 EurekaClient, 同 @EnableDiscoveryClient 注解相同。
public class OrderApplication {/***  使用 spring 提供的 RestTemplate 发送 http 请求到商品服务。*      1)创建 RestTemplate 对象交给容器管理。*      2)在使用的时候,调用其方法完成操作(getXX, postXX)。     **/@LoadBalanced@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}

3)运行 3个 启动类(order_service, product_service, eureka_service),进行测试

浏览器地址栏输入:http://localhost:9000 输出界面如下:
在这里插入图片描述

浏览器地址栏输入:http://localhost:9001/product/1 输出界面如下:
在这里插入图片描述

浏览器地址栏输入:http://localhost:9002/order/buy/1 输出界面如下:
在这里插入图片描述

二、ribbon:客户端负载均衡的概述

1、服务端的负载均衡:如:ngnix 软件系统, F5 硬件配置等。

2、客户端的负载均衡:如:ribbon 。

ribbon 是一个典型的客户端负载均衡器。
ribbon 会获取服务器的所有地址,根据内部的负载均衡算法,
获取本次请求的有效地址。

三、基于 ribbon 的负载均衡测试

1、准备两个商品微服务(product_service 子工程),端口分别为:9001,9011

1)修改 product_service 子工程(子模块)中的 controller 类 ProductController.java
/***  C:\java-test\idea2019\spring_cloud_demo\product_service\src\main\java\djh\it\product\controller\ProductController.java**  2024-4-17 商品的 controller 类 ProductController.java*/
package djh.it.product.controller;import djh.it.product.domain.Product;
import djh.it.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;//获取服务器端口号@Value("${server.port}")private String port;//获取服务器IP地址@Value("${spring.cloud.client.ip-address}")   //springcloud 自动获取当前应用的IP地址private String ip;@RequestMapping(value = "/{id}", method = RequestMethod.GET)public Product findById(@PathVariable Long id){Product product = productService.findById(id);product.setProductName("访问的服务地址是:" + ip + " : " + port);return product;}@RequestMapping(value = "", method = RequestMethod.POST)public String save (@RequestBody Product product){productService.save(product);return "保存成功";}}
2)在 idea 的 RunDashboard 运行面板,复制一个 ProductApplication 并命名为:ProductApplication(2) 把它也运行起来。

在 idea 中,找不到 RunDashboard 运行面板 时,可以看如下文章:

# IDEA2019 如何打开 Run Dashboard 运行仪表面板

修改 product_service 子工程(子模块)中的 application.yml 配置文件中的端口号为:9011。
并把名为:ProductApplication(2) 服务 也运行起来。

## C:\java-test\idea2019\spring_cloud_demo\product_service\src\main\resources\application.ymlserver:port: 9001  # 启动端口 命令行注入。
#  port: ${port:56010}  # 启动端口设置为动态传参,如果未传参数,默认端口为 56010
#  servlet:
#    context-path: /application1spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/
#      defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 高可用,注册多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。lease-renewal-interval-in-seconds: 10  # 设置向注册中心每10秒发送一次心跳,告诉注册中心此服务没有岩机,此参数默认是30秒。lease-expiration-duration-in-seconds: 20  # 设置每20秒如果注册中心没收到此服务的心跳,就认为此服务岩机了,此参数默认是90秒。

## C:\java-test\idea2019\spring_cloud_demo\product_service\src\main\resources\application.ymlserver:port: 9011  # 启动端口 命令行注入。
#  port: ${port:56010}  # 启动端口设置为动态传参,如果未传参数,默认端口为 56010
#  servlet:
#    context-path: /application1spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/
#      defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 高可用,注册多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。lease-renewal-interval-in-seconds: 10  # 设置向注册中心每10秒发送一次心跳,告诉注册中心此服务没有岩机,此参数默认是30秒。lease-expiration-duration-in-seconds: 20  # 设置每20秒如果注册中心没收到此服务的心跳,就认为此服务岩机了,此参数默认是90秒。
3)运行 3个 启动类(order_service, product_service, eureka_service),进行测试

浏览器地址栏输入:http://localhost:9000 输出界面如下:
在这里插入图片描述

浏览器地址栏输入:http://localhost:9001/product/1 输出界面如下:
在这里插入图片描述

浏览器地址栏输入:http://localhost:9011/product/1 输出界面如下:
在这里插入图片描述

浏览器地址栏输入:http://localhost:9002/order/buy/1 输出界面如下:
在这里插入图片描述

2、在订单系统中(order_service 子工程),远程以负载均衡的形式调用商品微服务。

1)在 order_service 子工程(子模块)的 controller 类 OrderController.java 中

使用 基于 ribbon 的形式调用远程微服务:使用服务名称 service-product 替换 IP 地址 。


/***  C:\java-test\idea2019\spring_cloud_demo\order_service\src\main\java\djh\it\order\controller\OrderController.java**  2024-4-19 订单的 controller 类 OrderController.java*/
package djh.it.order.controller;import djh.it.order.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;import java.util.List;@RestController
@RequestMapping("/order")
public class OrderController {// 注入 restTemplate 对象@Autowiredprivate RestTemplate restTemplate;/***  注入 DiscoveryClient : springcloud 提供的获取元数据的工具类。*      调用方法获取服务的元数据信息。*/@Autowiredprivate DiscoveryClient discoveryClient;/***  使用 基于 ribbon 的形式调用远程微服务:*  1、使用 @LoadBalanced 注解 声明 RestTemplate*  2、使用服务名称 service-product 替换 IP 地址 。** @param id* @return*/@RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)public Product findById(@PathVariable Long id){Product product = null;//product = restTemplate.getForObject("http://127.0.0.1:9001/product/1", Product.class);product = restTemplate.getForObject("http://service-product/product/1", Product.class);return product;}/***  参数:商品的 id*      通过订单系统,调用商品服务根据id查询商品信息。*          1)需要配置商品对象。*          2)需要调用商品服务。*   可以使用 java 中的 urlconnection, HttpClient, OkHttp 进行远程调用。*   也可以使用 restTemplate 进行远程调用。** @param id* @return*/
//    @RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)
//    public Product findById(@PathVariable Long id){
//
//        //调用 discoveryClient 方法,已调用服务名称获取所有的元数据。
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//        for (ServiceInstance instance : instances) {
//            System.out.println(instance);
//        }
//
//        //获取唯一的一个元数据
//        ServiceInstance instance = instances.get(0);
//
//        Product product = null;
//
//        //根据元数据中的主机地址和端口号拼接请求微服务的 URL
//        product = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/product/1", Product.class);
//
//
//        /**
//         *  调用商品服务(将微服务的请求路径硬编码到 java 代码中)
//         *  存在问题:对微服务调用的负载均衡,加入API网关,配置的统一管理,链路追踪。
//         */
//        //product = restTemplate.getForObject("http://127.0.0.1:9001/product/1", Product.class);
//        return product;
//    }
}
2)在 order_service 子工程(子模块)的 启动类 OrderApplication.java 中

使用 基于 ribbon 的形式调用远程微服务:使用 @LoadBalanced 注解 声明 RestTemplate


/***   C:\java-test\idea2019\spring_cloud_demo\order_service\src\main\java\djh\it\order\OrderApplication.java**   2024-4-19  启动类 OrderApplication.java*/
package djh.it.order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EntityScan("djh.it.order.domain")
@EnableEurekaClient  //激活 EurekaClient, 同 @EnableDiscoveryClient 注解相同。
public class OrderApplication {/***  使用 spring 提供的 RestTemplate 发送 http 请求到商品服务。*      1)创建 RestTemplate 对象交给容器管理。*      2)在使用的时候,调用其方法完成操作(getXX, postXX)。     **/@LoadBalanced  //使用 基于 ribbon 的形式调用远程微服务:使用 @LoadBalanced 注解 声明 RestTemplate@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}
3)再次运行 3个 启动类(order_service, product_service, eureka_service),进行测试

浏览器地址栏输入:http://localhost:9002/order/buy/1 多刷新几次,
会发现,输出界面采用轮询的方式 切换 9001 和 9011 端口的访问。如下:
7.png
8.png

四、ribbon:负载均衡策略

1、ribbon:负载均衡策略 分类

com.netf1ix.1oadbalancer.RoundRobinRule: 以轮询的方式进行负载均衡
com.netflix.loadbalancer.RandomRule: 随机策略
com.netflix.1oadbalancer.RetryRule: 重试策略。
com.netflix.1oadbalancer.weightedResponseTimeRule: 权重策略。会计算每个服务的权重,越高的被调用的可能性越大。
com.netflix.1oadbalancer.BestAvailableRule: 最佳策略。遍历所有的服务实例,过滤掉故障实例,并返回请求数最小的实例返回。
com.netf1ix.1oadbalancer.Avai1abilityFi1teringRule: 可用过滤策略。过滤掉故障和请求数超过阈值的服务实例,再从剩下的实力中轮询调用。

2、ribbon:负载均衡 策略选择:

1)如果每个机器配置一样,则建议不修改策略(推荐)
2)如果部分机器配置性能强,则可以改为 WeightedResponseTimeRule 权重策略。

3、可以在服务消费者的 application.yml 配置文件中修改负载均衡策略

shop-service-product:
ribbon :
NFLoadBalancerRuleclassName: com.netflix.loadbalancer.RandomRule

1)修改 order_service 子工程(子模块)的 application.yml 配置文件,

添加 修改 ribbon 负载均衡策略。


## C:\java-test\idea2019\spring_cloud_demo\order_service\src\main\resources\application.ymlserver:port: 9002  # 启动端口 命令行注入。
#  port: ${port:9002}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9002
#  servlet:
#    context-path: /application1spring:application:name: service-order  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册
# 添加 修改 ribbon 负载均衡策略:服务名 - ribbon - NFLoadBalancerRuleclassName :策略
service-product:ribbon:NFLoadBalancerRuleclassName: com.netflix.loadbalancer.RandomRule
2)再次运行 3个 启动类(order_service, product_service, eureka_service),进行测试

浏览器地址栏输入:http://localhost:9002/order/buy/1 多刷新几次,
会发现,输出界面采用 随机 的方式 切换 9001 和 9011 端口的访问。如下:
在这里插入图片描述

在这里插入图片描述

五、ribbon:请求重试

1、修改 order_service 子工程(子模块)的 pom.xml 配置文件,

添加 spring-retry 请求重试 依赖坐标。


<?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>spring_cloud_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>order_service</artifactId><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--            <version>5.1.32</version>--><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 引入 EurekaClient 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 引入 spring-retry 请求重试 依赖坐标 --><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency></dependencies></project>
<!-- C:\java-test\idea2019\spring_cloud_demo\order_service\pom.xml -->

2、修改 order_service 子工程(子模块)的 application.yml 配置文件,

添加 ribbon 请求重试设置,添加 日志打印设置。


## C:\java-test\idea2019\spring_cloud_demo\order_service\src\main\resources\application.ymlserver:port: 9002  # 启动端口 命令行注入。
#  port: ${port:9002}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9002
#  servlet:
#    context-path: /application1spring:application:name: service-order  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册logging:  # 使用日志level:root: debug# 添加 修改 ribbon 负载均衡策略:服务名 - ribbon - NFLoadBalancerRuleclassName :策略
service-product:ribbon:
#    NFLoadBalancerRuleclassName: com.netflix.loadbalancer.RandomRule  # 配置负载均衡重置为随机ConnectTimeout: 250  # Ribbon 的连接超时时间ReadTimeout: 1000  # Ribbon 的数据读取超时时间OkToRetryOnAllOperations: true  # 是否对所有操作都进行重试MaxAutoRetriesNextServer: 1  # 切换实例的重试次数MaxAutoRetries: 1  # 对当前实例的重试次数。

3、再次运行 3个 启动类(order_service, product_service, eureka_service),进行测试

浏览器地址栏输入:http://localhost:9002/order/buy/1 多刷新几次,
先发现是轮询访问 9001 和 9011 端口,

然后停掉一个,再次刷新访问
会发现,edia 控制台 Consule 界面 有等待,尝试请求重试 日志,如下:
在这里插入图片描述

上一节链接如下:
# 从浅入深 学习 SpringCloud 微服务架构(三)注册中心 Eureka(4)

这篇关于# 从浅入深 学习 SpringCloud 微服务架构(四)Ribbon的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/936140

相关文章

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

关于DNS域名解析服务

《关于DNS域名解析服务》:本文主要介绍关于DNS域名解析服务,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录DNS系统的作用及类型DNS使用的协议及端口号DNS系统的分布式数据结构DNS的分布式互联网解析库域名体系结构两种查询方式DNS服务器类型统计构建DNS域