当前位置: 首页 > 编程日记 > 正文

阿里注册中心nacos使用整合Dubbo-原创

阿里注册中心nacos是今年开源的框架,一开始以为就是个zk。后面看了图才明白他对标的竟然是consul\eureka,最重要是完美支持dubbo。我想今年开源它也是别有用意 。(目前nacos0.7版本)

Dubbo 融合 Nacos 成为注册中心

Nacos 作为 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

预备工作

当您将 dubbo-registry-nacos 整合到您的 Dubbo 工程之前,请确保后台已经启动 Nacos 服务。如果您尚且不熟悉 Nacos 的基本使用的话,可先行参考 Nacos 快速入门:https://nacos.io/en-us/docs/quick-start.html

快速上手

Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单,大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。

增加 Maven 依赖

首先,您需要 dubbo-registry-nacos 的 Maven 依赖添加到您的项目中 pom.xml 文件中,并且强烈地推荐您使用 Dubbo 2.6.5

<dependencies>...<!-- Dubbo Nacos registry dependency --><dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>0.0.1</version> </dependency> <!-- Dubbo dependency --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.5</version> </dependency> <!-- Alibaba Spring Context extension --> <dependency> <groupId>com.alibaba.spring</groupId> <artifactId>spring-context-support</artifactId> <version>1.0.2</version> </dependency> ... </dependencies> 

当项目中添加 dubbo-registry-nacos 后,您无需显示地编程实现服务发现和注册逻辑,实际实现由该三方包提供,接下来配置 Naocs 注册中心。

配置注册中心

假设您 Dubbo 应用使用 Spring Framework 装配,将有两种配置方法可选,分别为:Dubbo Spring 外部化配置以及 Spring XML 配置文件以及 ,笔者强烈推荐前者。

Dubbo Spring 外部化配置

Dubbo Spring 外部化配置是由 Dubbo 2.5.8 引入的新特性,可通过 Spring Environment 属性自动地生成并绑定 Dubbo 配置 Bean,实现配置简化,并且降低微服务开发门槛。

假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,同时,该注册地址作为 Dubbo 外部化配置属性存储在 dubbo-config.properties 文件,如下所示:

## application
dubbo.application.name = your-dubbo-application## Zookeeper registry address
dubbo.registry.address = zookeeper://10.20.153.10:2181
...

假设您的 Nacos Server 同样运行在服务器 10.20.153.10 上,并使用默认 Nacos 服务端口 8848,您只需将 dubbo.registry.address 属性调整如下:

## 其他属性保持不变## Nacos registry address
dubbo.registry.address = nacos://10.20.153.10:8848
...

随后,重启您的 Dubbo 应用,Dubbo 的服务提供和消费信息在 Nacos 控制台中可以显示:

image-20181213103845976

如图所示,服务名前缀为 providers: 的信息为服务提供者的元信息,consumers: 则代表服务消费者的元信息。点击“详情”可查看服务状态详情:

image-20181213104145998

如果您正在使用 Spring XML 配置文件装配 Dubbo 注册中心的话,请参考下一节。

Spring XML 配置文件

同样,假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,并且装配 Spring Bean 在 XML 文件中,如下所示:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-provider-xml-demo" /> <!-- 使用 Zookeeper 注册中心 --> <dubbo:registry address="zookeeper://10.20.153.10:2181" /> ... </beans> 

与 Dubbo Spring 外部化配置 配置类似,只需要调整 address 属性配置即可:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-provider-xml-demo" /> <!-- 使用 Nacos 注册中心 --> <dubbo:registry address="nacos://10.20.153.10:8848" /> ... </beans> 

重启 Dubbo 应用后,您同样也能发现服务提供方和消费方的注册元信息呈现在 Nacos 控制台中:

image-20181213113049185

您是否绝对配置或切换 Nacos 注册中心超级 Easy 呢?如果您仍旧意犹未尽或者不甚明白的话,可参考以下完整的示例。

完整示例

以上图片中的元数据源于 Dubbo Spring 注解驱动示例以及 Dubbo Spring XML 配置驱动示例,下面将分别介绍两者,您可以选择自己偏好的编程模型。在正式讨论之前,先来介绍两者的预备工作,因为它们皆依赖 Java 服务接口和实现。同时,请确保本地(127.0.0.1)环境已启动 Nacos 服务。

示例接口与实现

首先定义示例接口,如下所示:

package com.alibaba.dubbo.demo.service;/*** DemoService** @since 2.6.5*/
public interface DemoService { String sayName(String name); } 

提供以上接口的实现类:

package com.alibaba.dubbo.demo.service;import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;import org.springframework.beans.factory.annotation.Value;/** * Default {@link DemoService} * * @since 2.6.5 */ @Service(version = "${demo.service.version}") public class DefaultService implements DemoService { @Value("${demo.service.name}") private String serviceName; public String sayName(String name) { RpcContext rpcContext = RpcContext.getContext(); return String.format("Service [name :%s , port : %d] %s(\"%s\") : Hello,%s", serviceName, rpcContext.getLocalPort(), rpcContext.getMethodName(), name, name); } } 

接口与实现准备妥当后,下面将采用注解驱动和 XML 配置驱动各自实现。

Spring 注解驱动示例

Dubbo 2.5.7 重构了 Spring 注解驱动的编程模型。

服务提供方注解驱动实现

  • 定义 Dubbo 提供方外部化配置属性源 - provider-config.properties
## application
dubbo.application.name = dubbo-provider-demo## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848## Dubbo Protocol
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1# Provider @Service version
demo.service.version=1.0.0
demo.service.name = demoService
  • 实现服务提供方引导类 - DemoServiceProviderBootstrap
package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource; import java.io.IOException; /** * {@link DemoService} provider demo */ @EnableDubbo(scanBasePackages = "com.alibaba.dubbo.demo.service") @PropertySource(value = "classpath:/provider-config.properties") public class DemoServiceProviderBootstrap { public static void main(String[] args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DemoServiceProviderBootstrap.class); context.refresh(); System.out.println("DemoService provider is starting..."); System.in.read(); } } 

其中注解 @EnableDubbo 激活 Dubbo 注解驱动以及外部化配置,其 scanBasePackages 属性扫描指定 Java 包,将所有标注 @Service 的服务接口实现类暴露为 Spring Bean,随即被导出 Dubbo 服务。

@PropertySource 是 Spring Framework 3.1 引入的标准导入属性配置资源注解,它将为 Dubbo 提供外部化配置。

服务消费方注解驱动实现

  • 定义 Dubbo 消费方外部化配置属性源 - consumer-config.properties
## Dubbo Application info
dubbo.application.name = dubbo-consumer-demo## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848# @Reference version
demo.service.version= 1.0.0

同样地,dubbo.registry.address 属性指向 Nacos 注册中心,其他 Dubbo 服务相关的元信息通过 Nacos 注册中心获取。

  • 实现服务消费方引导类 - DemoServiceConsumerBootstrap
package com.alibaba.dubbo.demo.consumer;import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource; import javax.annotation.PostConstruct; import java.io.IOException; /** * {@link DemoService} consumer demo */ @EnableDubbo @PropertySource(value = "classpath:/consumer-config.properties") public class DemoServiceConsumerBootstrap { @Reference(version = "${demo.service.version}") private DemoService demoService; @PostConstruct public void init() { for (int i = 0; i < 10; i++) { System.out.println(demoService.sayName("小马哥(mercyblitz)")); } } public static void main(String[] args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DemoServiceConsumerBootstrap.class); context.refresh(); context.close(); } } 

同样地,@EnableDubbo 注解激活 Dubbo 注解驱动和外部化配置,不过当前属于服务消费者,无需指定 Java 包名扫描标注 @Service 的服务实现。

@Reference 是 Dubbo 远程服务的依赖注入注解,需要服务提供方和消费端约定接口(interface)、版本(version)以及分组(group)信息。在当前服务消费示例中,DemoService 的服务版本来源于属性配置文件 consumer-config.properties

@PostConstruct 部分代码则说明当 DemoServiceConsumerBootstrap Bean 初始化时,执行十次 Dubbo 远程方法调用。

运行注解驱动示例

在本地启动两次 DemoServiceProviderBootstrap,注册中心将出现两个健康服务:

image-20181213123909636

再运行 DemoServiceConsumerBootstrap,运行结果如下:

Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)

运行无误,并且服务消费方使用了负载均衡策略,将十次 RPC 调用平均分摊到两个 Dubbo 服务提供方实例中。

Spring XML 配置驱动示例

Spring XML 配置驱动是传统 Spring 装配组件的编程模型。

服务提供方 XML 配置驱动

  • 定义服务提供方 XML 上下文配置文件 - /META-INF/spring/dubbo-provider-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-provider-xml-demo"/> <!-- 使用 Nacos 注册中心 --> <dubbo:registry address="nacos://127.0.0.1:8848"/> <!-- 用dubbo协议在随机端口暴露服务 --> <dubbo:protocol name="dubbo" port="-1"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.alibaba.dubbo.demo.service.DemoService" ref="demoService" version="2.0.0"/> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.alibaba.dubbo.demo.service.DefaultService"/> </beans> 
  • 实现服务提供方引导类 - DemoServiceProviderXmlBootstrap
package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** {@link DemoService} provider demo XML bootstrap*/
public class DemoServiceProviderXmlBootstrap {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();context.setConfigLocation("/META-INF/spring/dubbo-provider-context.xml");context.refresh();System.out.println("DemoService provider (XML) is starting...");System.in.read();}
}

服务消费方 XML 配置驱动

  • 定义服务消费方 XML 上下文配置文件 - /META-INF/spring/dubbo-consumer-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-consumer-xml-demo"/> <!-- 使用 Nacos 注册中心 --> <dubbo:registry address="nacos://127.0.0.1:8848"/> <!-- 引用服务接口 --> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.service.DemoService" version="2.0.0"/> </beans> 
  • 实现服务消费方引导类 - DemoServiceConsumerXmlBootstrap
package com.alibaba.dubbo.demo.consumer;import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/** * {@link DemoService} consumer demo XML bootstrap */ public class DemoServiceConsumerXmlBootstrap { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); context.setConfigLocation("/META-INF/spring/dubbo-consumer-context.xml"); context.refresh(); System.out.println("DemoService consumer (XML) is starting..."); DemoService demoService = context.getBean("demoService", DemoService.class); for (int i = 0; i < 10; i++) { System.out.println(demoService.sayName("小马哥(mercyblitz)")); } context.close(); } } 

运行 XML 配置驱动示例

同样地,先启动两个 DemoServiceProviderXmlBootstrap 引导类,观察 Nacos 注册中心服务提供者变化:

image-20181213125527201

XML 配置驱动的服务版本为 2.0.0,因此注册服务无误。

再运行服务消费者引导类 DemoServiceConsumerXmlBootstrap,观察控制台输出内容:

Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)
Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz)

结果同样运行和负载均衡正常,不过由于当前示例尚未添加属性 demo.service.name 的缘故,因此,“name”部分信息输出为 null

如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程,不妨为它们点 “star”,加油打气链接:

  • Apache Dubbo:https://github.com/apache/incubator-dubbo
  • DUbbo Nacos Registry:https://github.com/dubbo/dubbo-registry-nacos
  • Alibaba Nacos:https://github.com/alibaba/nacos

转载于:https://www.cnblogs.com/jay-wu/p/10154307.html

相关文章:

UBUNTU adb连接android设备

1sudo vi /etc/udev/rules.d/70-android.rules 2最新修改方法&#xff0c;不用去看设备的ID&#xff0c;直接在rules.d下增加一个文件51-android.rules&#xff0c;内容为&#xff1a; SUBSYSTEM"usb" ENV{DEVTYPE}"usb_device", MODE"0666" …

jetty9请求form表单太小限制

报错&#xff1a;java.lang.IllegalStateException: Form too large: 201975 > 200000解决&#xff1a;vi jetty.xml<Configure id"Server" class"org.eclipse.jetty.server.Server">在Server这行下面增加以下代码<!-- guowang add --><…

【ACM】杭电OJ 2037

题目链接&#xff1a;杭电OJ 2037 先把b[i]进行排序&#xff0c;然后&#xff0c;b[i]与a[i1]进行比较。 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <set> #include <…

第二十章:异步和文件I/O.(十三)

通过该开销&#xff0c;可以开始实际编写应用程序。 TextFileAsyncPage的XAML文件与TextFileTryoutPage相同&#xff0c;但必须将代码隐藏文件设置为使用异步文件I / O方法。 必须在此处捕获文件I / O函数中可能发生的任何异常&#xff0c;这意味着任何可以抛出异常的方法必须与…

shell [] [[ ]] {}区别

一、小括号&#xff0c;圆括号&#xff08;&#xff09; 1、单小括号 () ①命令组。括号中的命令将会新开一个子shell顺序执行&#xff0c;所以括号中的变量不能够被脚本余下的部分使用。括号中多个命令之间用分号隔开&#xff0c;最后一个命令可以没有分号&#xff0c;各命…

【C++】用指针做函数参数

此篇博客程序运行环境为&#xff1a;VS2017&#xff01;&#xff01;&#xff01; 函数的参数不仅可以是整型、浮点型、字符型等数据&#xff0c;还可以是指针类型。 它的作用是将一个变量的的地址传给被调用函数的形参。 e.g. 输入两个数&#xff0c;按由大到小顺序输出 #…

JAVA面向对象-----final关键字

JAVA面向对象—–final关键字 1&#xff1a;定义静态方法求圆的面积 2&#xff1a;定义静态方法求圆的周长 3&#xff1a;发现方法中有重复的代码&#xff0c;就是PI&#xff0c;圆周率。1&#xff1a;如果需要提高计算精度&#xff0c;就需要修改每个方法中圆周率。 4&#xf…

win7安装mysql-8.0.13-winx64

这里展示一下&#xff0c;由于需要安装一个版本测试一下数据&#xff0c;其实就是超简单的啦。 下包 注:https://dev.mysql.com/downloads/mysql/ 解压与配置 [mysqld] basedirC:\\Users\\hp\\Downloads\\mysql-8.0.13-winx64 datadirC:\\Users\\hp\\Downloads\\mysql-8.0.13-w…

Http 请求头中的 Proxy-Connection

平时用 Chrome 开发者工具抓包时&#xff0c;经常会见到 Proxy-Connection 这个请求头。之前一直没去了解什么情况下会产生它&#xff0c;也没去了解它有什么含义。最近看完《HTTP 权威指南》第四章「连接管理」和第六章「代理」之后&#xff0c;终于搞明白了这是因为给浏览器设…

【C++】枚举类型

如果一个变量只能有几种可能的值&#xff0c;可以定义为枚举类型。所谓“枚举”就是把变量的值一一列出来&#xff0c;变量的值只能在列出来的值的范围内。 声明枚举类型的一般形式&#xff1a; enum 枚举类型名 {枚举常量表} enum weekday {sun,mon,tue,wed,thu,fri,sat}; …

ubuntu设置securecrt串口权限

在普通用户的模式下&#xff0c;用SecureCRT链接串口交换机&#xff0c;开始会提示/dev/ttyUSB0权限不足&#xff0c;无法打开&#xff0c;临时的解决办法是 chmod 0rw /dev/ttyUSB0 但是这个重启后便没了作用&#xff0c;下面的方法能在重启后让普通用户链接串口设备。 sudo v…

深入解析Angular Component的源码示例

本篇文章主要介绍了剖析Angular Component的源码示例&#xff0c;写的十分的全面细致&#xff0c;具有一定的参考价值&#xff0c;对此有需要的朋友可以参考学习下。如有不足之处&#xff0c;欢迎批评指正。 Web Component 定义 W3C为统一组件化标准方式&#xff0c;提出Web Co…

VS2017 cout 不明确

各种头文件没问题。直接声明名称空间 using namespace std&#xff1b; 解决方法&#xff1a; 然后把using namespace std;这句给注释掉&#xff0c;等出现错误提示&#xff0c;在取消注释&#xff0c;然后就好了

google breakpad native crash分析工具

一. BreakPad简介Google breakpad是一个跨平台的崩溃转储和分析框架和工具集合。Breakpad由三个主要组件&#xff1a;client&#xff0c;以library的形式内置在你的应用中&#xff0c;当崩溃发生时写 minidump文件symbol dumper, 读取由编译器生成的调试信息&#xff08;debugg…

Java基础教程(15)--枚举类型

枚举类型定义了一个枚举值的列表&#xff0c;每个值是一个标识符。例如&#xff0c;下面的语句声明了一个枚举类型&#xff0c;用来表示星期的可能情况&#xff1a; public enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } 实际上&#xff0c;这个…

基于AOA协议的android USB通信

摘 要&#xff1a;AOA协议是Google公司推出的用于实现Android设备与外围设备之间USB通信的协议。该协议拓展了Android设备USB接口的功能&#xff0c;为基于Android系统的智能设备应用于数据采集和设备控制领域提供了条件。介绍了Android系统下USB通信的两种模式&#xff0c;并给…

Linux下使用ssh动态验证码登陆机器

ssh动态验证码登录机器Google Authenticator是一个动态验证码程序&#xff0c;兼容各种智能手机平板设备&#xff0c;可以用来做各种帐号的二次验证&#xff0c;增加帐号的安全性。SSH是Linux系统的最重要防线之一&#xff0c;为了防止密码泄露或者被爆破&#xff0c;可以使用G…

【C++】枚举类型应用

运行环境&#xff1a;VS2017 可以参考&#xff1a;【C】枚举类型 医院内科有A&#xff0c;B&#xff0c;C&#xff0c;D&#xff0c;E&#xff0c;F&#xff0c;G共七位医生&#xff0c;每人在一周内要值一次夜班&#xff0c;排班的要求&#xff1a; &#xff08;1&#xff…

量子力学又一突破,中国科学家首次实现量子纠缠态自检验

这也是国际上首个具有“高可靠、抗干扰”特性的纠缠态自检验实验。 最近&#xff0c;量子力学领域又传来好消息&#xff0c;中国科学技术大学的郭光灿院士团队在实验中首次实现了量子纠缠态的自检验&#xff0c;推动了自检验在各种量子信息过程中的基础发展。 何为量子纠缠&a…

awk命令中执行多条shell命令

awk中使用的shell命令&#xff0c;有2种方法&#xff1a;一。使用system&#xff08;&#xff09;二。使用print cmd | “/bin/bash”http://www.gnu.org/software/gawk/manual/gawk.html#I_002fO-Functions一。使用所以system&#xff08;&#xff09;awk程序中我们可以使用sy…

LAMP高级环境实战

LAMP架构应用实战介绍LAMP指的Linux&#xff08;操作系统&#xff09;、Apache&#xff08;HTTP 服务器&#xff09;&#xff0c;MySQL&#xff08;数据库软件&#xff09; 和PHP&#xff08;有时也是指Perl或Python&#xff09; 的第一个字母&#xff0c;一般用来建立web 服务…

【C++】用类来处理排序问题

运行环境&#xff1a;VS2017 由小到大排序 可以看出在主函数中所做的事&#xff1a; &#xff08;1&#xff09;定义对象。 &#xff08;2&#xff09;向各对象发出“消息”&#xff0c;通知各对象完成有关任务。即调用有关对象的成员函数&#xff0c;去完成相应的操作。 …

winform 弹出窗体位置设定

[转]https://www.cnblogs.com/liushenglin/p/5350641.html 一、C#中弹出窗口位置 加入命名空间using System.Drawing和using System.Windows.Forms假定窗口名为form1,则 form1.StartPosition FormStartPosition.CenterScreen;窗体位置在屏幕中间form1.StartPosition FormSta…

pkg-config工具在实际工程中的用法

在如今这个开源的环境里&#xff0c;想要开发某个功能&#xff0c;我们都会下意识的上网搜索有没有开源库&#xff0c;如果有开源库&#xff0c;那么好&#xff0c;下载下来给它编译好&#xff0c;使用。但是在使用过程中&#xff0c;你是否遇到不知如何将第三方库编译&#xf…

linux中pipe

调用pipe函数时在内核中开辟一块缓冲区(称为管道)用于通信,它有一个读端一个写端,然后通过filedes参数传出给用户程序两个文件描述符,filedes[0]指向管道的读端,filedes[1]指向管道的写端(很好记就像0是标准输入1是标准输出一样)。所以管道在用户程序看起来就像一个打开的文通r…

【C++】利用构造函数对类对象进行初始化

运行环境&#xff1a;VS2017 一、对象的初始化 每一个对象都应当在它建立之时就有就有确定的内容&#xff0c;否则就会失去对象的意义。 class Time {int hour 0;int min 0;int sec 0; }; 这种是错误的&#xff0c;类并不是一个实体&#xff0c;并不占储存空间&#xff…

自定义Chrome浏览器

一、全局 自用备份&#xff0c;窗体透明化、要添加对应网站的窗体class到对应的位置 /*主页背景*/ /*https://images.cnblogs.com/cnblogs_com/AardWolf/1350846/o_5900399dcdcbd.jpg*/ /*https://ws4.sinaimg.cn/large/0072Vf1pgy1foxkfzphb2j31hc0u0gvv.jpg*/body { backgrou…

ubuntu添加sudo权限

使用如下命令可以添加到用户组&#xff08;也可是超级用户组&#xff09;。 命令如下&#xff1a; sudo usermod -aG 超级用户组名 用户名 例子&#xff1a;sudo usermod -aG sudo username 其中a:表示添加&#xff0c;G&#xff1a;指定组名第二种方法是直接修改&#xff0c…

File.separator

报告“No such file or diretory ”的异常&#xff0c;上传不了。后来发现是文件路径的问题。模拟测试环境是windowstomcat&#xff0c;而正式的的环境是linuxtomcat&#xff0c;文件路径的分隔符在windows系统和linux系统中是不一样。 比如说要在temp目录下建立一个test.txt文…

【C++】对象数组

运行环境&#xff1a;VS2017 对象数组&#xff1a;每个元素都是同类的对象 如果构造函数只有一个参数&#xff0c;在定义数组时可以直接在等号后面的花括号内提供实参。 Student stud[3]{60,70,80}; 如果构造函数有多个参数&#xff0c;则不能用在定义数组时直接提供所有实…