2024-09-01

在Linux下使用C语言操作SQLite数据库,你需要安装SQLite3库。如果尚未安装,可以使用以下命令安装:




sudo apt-install libsqlite3-dev

以下是一个简单的C程序示例,展示了如何使用SQLite3库来打开数据库、创建表格、插入数据以及查询数据。




#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
 
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
   for(int i = 0; i < argc; i++){
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   printf("\n");
   return 0;
}
 
int main(){
   sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
 
   rc = sqlite3_open("example.db", &db);
   if( rc ){
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
      return(0);
   }else{
      fprintf(stderr, "Opened database successfully\n");
   }
 
   // 创建一个表
   char *sql = "CREATE TABLE IF NOT EXISTS students(ID INTEGER PRIMARY KEY, NAME TEXT, AGE INTEGER, ADDRESS TEXT);";
   rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }
   // 插入数据
   sql = "INSERT INTO students(NAME, AGE, ADDRESS) VALUES('John', 21, 'Highway 21');";
   rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }
   sql = "INSERT INTO students(NAME, AGE, ADDRESS) VALUES('Sam', 25, 'Long Street');";
   rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }
 
   // 查询数据
   sql = "SELECT * FROM students;";
   rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }
 
   sqlite3_close(db);
   return 0;
}

确保你的C编译器能够找到SQLite3库。如果你使用的是gcc编译器,可以在编译时加上 -lsqlite3 参数。




gcc your_program.c -o your_program -lsqlite3

运行程序之前,确保当前目录下不存在example.db数据库文件,程序会创建它。运行程序后,你会看到数据库被创建,表格被创建,数据被插入,并且查询结果被打印出来。

2024-09-01



import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
 
@Component
public class AuthorizationGlobalFilter implements GlobalFilter, Ordered {
 
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 示例:检查请求中是否包含有效的API密钥
        String apiKey = exchange.getRequest().getQueryParams().getFirst("apiKey");
        if (apiKey == null || !apiKey.equals("mySecretApiKey")) {
            // 如果API密钥无效,则返回401未授权状态码
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        // 如果API密钥有效,则继续请求处理
        return chain.filter(exchange);
    }
 
    @Override
    public int getOrder() {
        // 确保此过滤器在其他过滤器之前运行
        return -1;
    }
}

这段代码定义了一个全局过滤器,用于检查进入API网关的请求中是否包含有效的API密钥。如果密钥无效,则过滤器会直接返回HTTP状态码401,表示请求未被授权。这种方式可以用来实现简单的API访问控制,在实际应用中可以根据需要进行更复杂的权限控制。

2024-09-01

要在CentOS 8中卸载PostgreSQL 14数据库,可以使用dnf命令。以下是卸载PostgreSQL 14的步骤和示例代码:

  1. 停止PostgreSQL服务:



sudo systemctl stop postgresql-14
  1. 删除PostgreSQL 14软件包:



sudo dnf remove postgresql14 postgresql14-server
  1. 删除数据目录和配置文件(如果不再需要保留数据):



sudo rm -rf /var/lib/postgresql/14/main/
sudo rm -rf /etc/postgresql/14/main.conf

确保替换路径中的14main为实际安装时使用的版本和数据目录名称。

  1. 如果需要,删除用户和用户组(可选):



sudo userdel -r postgres
sudo groupdel postgres

请注意,在执行这些步骤之前确保备份任何重要数据,并确认你已停止所有数据库连接。

2024-09-01

Redis 原生不支持在 Windows 上直接运行,但是有一些第三方的尝试来使得 Redis 在 Windows 上运行,例如微软的开源项目 redis-windows者是基于 Windows 的 Redis 版本如 Redis-x64-3.2.100 可以在 GitHub 上找到。

如果你想要在 Windows 上部署 Redis 集群,你可以考虑使用 Docker 来运行 Redis 容器,然后使用 Redis 的官方集群工具 redis-trib.rb 来创建集群。

以下是一个基本的步骤指南:

  1. 安装 Docker for Windows。
  2. 拉取 Redis 镜像:docker pull redis
  3. 运行 Redis 容器实例,创建一个三主三从的 Redis 集群。

示例 Docker 命令:




docker network create redis-net
 
docker run --name redis-node1 --net redis-net -d redis redis-server --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
docker run --name redis-node2 --net redis-net -d redis redis-server --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
docker run --name redis-node3 --net redis-net -d redis redis-server --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
docker run --name redis-node4 --net redis-net -d redis redis-server --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
docker run --name redis-node5 --net redis-net -d redis redis-server --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
docker run --name redis-node6 --net redis-net -d redis redis-server --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
 
docker exec redis-node1 redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1

注意:以上命令假设你的 Docker 环境可以访问端口 30001-30006 以便于创建集群。

这个过程是在假设你已经有 Docker 环境的情况下进行的。如果你从未使用过 Docker,你需要先在 Windows 上安装 Docker,并对 Docker 有一定的了解。

由于 Windows 环境和 Linux 环境在某些方面存在差异,如权限、文件路径等,所以在 Windows 上运行 Redis 集群可能会遇到一些问题。如果你在实践中遇到问题,可以查看具体的错误信息,并在相关社区寻求帮助。

2024-09-01

在CentOS 7上安装Docker,并学习常用的Docker命令,接着使用Docker安装常见的组件,如Tomcat、MySQL和Redis。

  1. 安装Docker:



sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
  1. Docker常用命令:



# 启动Docker
sudo systemctl start docker
 
# 停止Docker
sudo systemctl stop docker
 
# 重启Docker
sudo systemctl restart docker
 
# 查看Docker状态
sudo systemctl status docker
 
# 查看Docker版本
docker --version
 
# 查看所有容器
docker ps -a
 
# 查看运行中的容器
docker ps
 
# 拉取镜像
docker pull 镜像名称
 
# 创建并启动容器
docker run -d -p 主机端口:容器端口 --name 容器名称 镜像名称
 
# 停止容器
docker stop 容器名称或ID
 
# 删除容器
docker rm 容器名称或ID
 
# 删除镜像
docker rmi 镜像名称或ID
  1. 使用Docker安装Tomcat:



docker run -d -p 8080:8080 --name my-tomcat tomcat:9.0
  1. 使用Docker安装MySQL:



docker run -d -p 3306:3306 --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7
  1. 使用Docker安装Redis:



docker run -d -p 6379:6379 --name my-redis redis:5.0

以上命令需要在具备sudo权限的CentOS 7系统上执行。这些命令展示了如何安装Docker,启动和使用它来运行Tomcat、MySQL和Redis等常见的Web服务。

2024-09-01

在Spring Boot项目中使用SpringBootServletInitializer可以将项目打包成WAR文件,并部署到支持Servlet API的Web容器中。以下是步骤和示例代码:

  1. 在Spring Boot主类上扩展SpringBootServletInitializer并重写configure方法。
  2. pom.xml中设置打包方式为war
  3. 构建项目并生成WAR文件。
  4. 部署到Web容器。

示例代码:

主类:




import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 
@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class);
    }
 
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

pom.xml:




<packaging>war</packaging>
 
<!-- 其他依赖 -->

构建项目:




mvn clean package

生成的WAR文件通常位于target/目录下。

部署到Tomcat或其他容器时,确保容器配置正确,并且没有其他相关的Web服务器(如Jetty或Undertow)配置在类路径下。

2024-09-01



-- 假设redis-cli已经设置好并且可以在命令行中使用
 
-- Lua脚本实现原子增加计数器操作
local key = KEYS[1] -- 获取Lua脚本传入的第一个参数(键名)
local increment = tonumber(ARGV[1]) -- 获取Lua脚本传入的第一个参数(键名)并转换为数字
 
-- 检查键是否存在,如果不存在,设置初始值为0
if (redis.call('exists', key) == 0) then
    redis.call('set', key, 0)
end
 
-- 对键的值进行原子增加操作
local newValue = redis.call('incrby', key, increment)
 
-- 返回新的值
return newValue

在这个Lua脚本中,我们首先检查指定的键是否存在,如果不存在,我们将其值初始化为0。然后我们使用incrby命令来原子地增加键的值。最后,脚本返回键的新值。这个脚本可以通过Redis的EVAL命令在Redis服务器上执行。

2024-09-01

在Spring Boot中,你可以加载自定义的YAML配置文件,并将其属性绑定到一个配置类中。以下是一个简单的例子:

  1. 创建自定义配置文件 custom-config.yml:



custom:
  property: value
  1. 创建配置类来绑定YAML中的属性:



package com.example.demo.config;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@ConfigurationProperties(prefix = "custom")
public class CustomConfig {
    private String property;
 
    public String getProperty() {
        return property;
    }
 
    public void setProperty(String property) {
        this.property = property;
    }
}
  1. 在Spring Boot应用的主类或配置类中添加 @EnableConfigurationProperties(CustomConfig.class) 注解:



package com.example.demo;
 
import com.example.demo.config.CustomConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
 
@SpringBootApplication
@EnableConfigurationProperties(CustomConfig.class)
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  1. 在需要使用配置的地方注入 CustomConfig:



import com.example.demo.config.CustomConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class SomeComponent {
 
    private final CustomConfig customConfig;
 
    @Autowired
    public SomeComponent(CustomConfig customConfig) {
        this.customConfig = customConfig;
    }
 
    public void printCustomProperty() {
        System.out.println(customConfig.getProperty());
    }
}

确保 custom-config.yml 位于 src/main/resources 目录下,与Spring Boot的标准配置文件分开管理。当Spring Boot应用启动时,它会自动加载这个配置文件并将属性绑定到 CustomConfig 类中,你可以在应用程序的任何部分通过依赖注入来访问这些属性。

2024-09-01

在CentOS 8中部署Tomcat和Jenkins可以通过以下步骤进行:

  1. 安装Java环境

    Tomcat和Jenkins都需要Java环境,确保系统已安装Java。




sudo dnf install java-11-openjdk-devel
java -version
  1. 安装Tomcat



sudo dnf install tomcat
sudo systemctl enable --now tomcat

检查Tomcat是否运行:




sudo systemctl status tomcat

浏览器访问 http://<服务器IP>:8080 确认Tomcat安装成功。

  1. 安装Jenkins

添加Jenkins仓库:




sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

安装Jenkins:




sudo dnf install jenkins

启动并启用Jenkins服务:




sudo systemctl start jenkins
sudo systemctl enable jenkins

检查Jenkins状态:




sudo systemctl status jenkins

浏览器访问 http://<服务器IP>:8080 并按照Jenkins初始化向导进行配置。

注意:确保防火墙允许访问8080端口。




sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

以上步骤可能会根据系统的实际情况稍有变化,请确保在执行前检查最新的安装指南和更新的配置要求。

Elasticsearch 的重要系统参数包括:

  1. cluster.name: 设置 Elasticsearch 集群的名称,默认是 "elasticsearch"。
  2. node.name: 设置节点的名称,在集群中用于识别不同的节点,默认是机器的主机名。
  3. node.master: 是否允许该节点被选举为 master 节点,默认是 true。
  4. node.data: 是否允许存储数据,默认是 true。
  5. network.host: 设置 Elasticsearch 监听的网络接口,默认是 127.0.0.1(本地回环地址)。
  6. http.port: 设置 Elasticsearch 节点对外服务的 HTTP 端口,默认是 9200。
  7. discovery.seed_hosts: 设置集群中的种子节点列表,新节点加入集群时会参考这个列表。
  8. cluster.initial_master_nodes: 设置集群启动时的初始 master 节点列表。
  9. node.max_local_storage_nodes: 设置单个节点能够参与集群的最大数据节点数量,默认是 2。
  10. indices.fielddata.cache.size: 设置字段数据缓存的大小,用于优化聚合操作性能。

这些参数可以在 Elasticsearch 的配置文件 elasticsearch.yml 中设置,也可以在启动 Elasticsearch 时通过命令行参数或环境变量来设置。

示例配置文件片段:




cluster.name: my-elasticsearch-cluster
node.name: node-1
network.host: 192.168.1.10
http.port: 9200
discovery.seed_hosts: ["192.168.1.10", "192.168.1.11"]
cluster.initial_master_nodes: ["node-1", "node-2"]
node.max_local_storage_nodes: 2
indices.fielddata.cache.size: 20%

在实际部署和调优 Elasticsearch 时,应当根据具体的硬件和软件环境以及业务需求来合理设置这些参数。