(OCPP服务器)SteVe编译搭建全过程
注意:建议使用3.6.0,我升级到3.7.1,并没有多什么新功能,反而电表的实时数据只能看到累计电能了,我回退了就正常,数据库是兼容的,java版本换位java11,其他不变就好
背景:
国外欧标充电桩开发中,服务器对接是很重要的一环,出国外的基本都是需要支持OCPP功能,基本要求是OCPP1.6,还有些客户已经开始要2.0了,OCPP测试至关重要,目前有两种测试方法,1,使用Monta服务器,去注册一个账号,然后链接调试即可,2.自己在云端搭建一个自己的OCPP平台(SteVe)。
两个平台我都有使用,用得多并且顺手的是SteVe平台。以下是简单总结一下使用后的感受。
Monta平台界面比较单一,下发的命令的应答只能看log里面找到对应的应答数据(json),优点是有认证测试功能,可以免费自己注册一个账号就能使用,免费https://ocpp-toolkit.monta.app/
SteVe平台界面丰富,可以管理ocpp tags(充电卡)或者get configration的时候预设很多系统的key使用,应答命令会帮你解析在页面显示出来,目前SteVe平台是最完善的,并且是开源免费的。
综上所述,自己搭建一个服务器就很有必要了。
SteVe平台搭建主要流程:
- 1.先准备一个云服务器,如果没有云服务器,可以考虑用虚拟机安装一个centos7,然后把这个机器映射到外网临时用也行。
- 2.下载SteVe官方源码包,我这里就用3.17.1举例。官方源码在GitHub https://github.com/steve-community/steve。
- 3.数据库MYSQL8的安装与配置,因为CentOS使用的是mariadb,但你去编译SteVe的时候你会发现提示非长期支持版,会报错。
- 4.安装Java17,Centos内置了Java工具,但查看版本发现是1.8的,SteVe3.7.1要求是Java17,删除Java1.8再安装Java17.
- 5.编译SteVe源码。
- 6.运行,测试
- 7.制作开机自启。
详细步骤:
1.下载SteVe源码。并阅读README.md,了解这个平台需要用到的环境,先配置好这些环境,然后了解编译工具和编译运行。经过阅读发现需要使用mysql8,java17,Maven
2.首先来安装数据库,数据库MYSQL8的安装与配置,查看当前mariadb版本信息。
rpm -e --nodeps
上一条命令列出的文件名。
rpm -qa|grep mariadb
用于检查是否卸载干净。
让AI帮忙写了一个centos7安装mysql8的脚步,这样比传统的下载安装更省事
#!/bin/bash
# Exit script on any error
set -e
# Fixed password to be used
fixed_password="Msb666666"
# 1. Add the MySQL Yum repository
echo "Adding the MySQL Yum repository..."
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
echo "Checking the integrity of the downloaded package..."
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql
echo "Installing the MySQL Yum repository..."
yum localinstall mysql80-community-release-el7-3.noarch.rpm -y
# 2. Install the MySQL server
echo "Installing the MySQL server..."
yum install mysql-community-server -y
# 3. Start the MySQL service
echo "Starting the MySQL service..."
systemctl start mysqld
systemctl enable mysqld
# 4. Get the temporary password generated for the root user
echo "Retrieving the temporary password..."
temp_password=$(grep 'temporary password' /var/log/mysqld.log | tail -1 | rev | cut -d" " -f1 | rev)
echo "The temporary MySQL root password is: $temp_password"
# 5. Change the root user's password to the desired fixed password
echo "Changing the root user's password..."
mysql --connect-expired-password -uroot -p"$temp_password" <<-EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '$fixed_password';
FLUSH PRIVILEGES;
EOF
echo "The root password has been successfully changed to the fixed password."
# Optionally, you can run the mysql_secure_installation steps here if you want to automate it further.
echo "MySQL 8 installation is complete."
还有mysql运行脚本
#!/bin/bash
# This script starts the MySQL service and sets it to launch on boot
# Exit script on any error
set -e
# Start the MySQL service
echo "Starting the MySQL service..."
systemctl start mysqld
# Enable the MySQL service to start on boot
echo "Setting the MySQL service to start on boot..."
systemctl enable mysqld
# Confirmation message
echo "MySQL service has been started and set to launch on boot."
安装好了之后通过ssh命令行登陆数据库
mysql -u root
接下来为steve创建相关数据
steve 数据库mysql8
用户名:root
密码:Msb666666@,
Steve数据库用户名:steve
密码:changeme
脚本安装后需要更新默认的随机密码,密码不能太简单,需要带点符合,否则会失败
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Msb666666@,';
FLUSH PRIVILEGES;
创建SteVe需要用到的数据库stevedb
CREATE DATABASE stevedb CHARACTER SET utf8 COLLATE utf8_unicode_ci;
创建完毕后可以使用命令查看一下,可以看到Stevedb
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| stevedb |
| sys |
+--------------------+
5 rows in set (0.00 sec)
再为这个数据创建一个数据库用户steve,这个和SteVe平台README.md默认用户名和密码保持一致,否则两边一起改。
CREATE USER 'steve'@'localhost' IDENTIFIED BY 'changeme';
GRANT ALL PRIVILEGES ON stevedb.* TO 'steve'@'localhost';
GRANT SUPER ON *.* TO 'steve'@'localhost';
然后退出数据库尝试使用新数据库用户登陆
mysql -u steve -p
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
| stevedb |
+--------------------+
3 rows in set (0.00 sec)
mysql>
这样就完成了,不需要建表,这个SteVe在打包编译和运行的时候都会自动建表。
编译之前还需要改一下数据库默认时区,解决java编译的时候时区问题。
vi /etc/my.cnf
增加default-time-zone=‘+08:00’
重启数据库
systemctl restart mysqld
数据库好了,还需要Java运行环境
yum install java-17-openjdk-devel
如果系统里有多个java版本可以使用alternatives来切换
CentOS 支持通过 alternatives 命令来管理多个版本的 Java。您可以按照以下步骤进行设置:
alternatives --install /usr/bin/javac java /usr/lib/jvm/temurin-17-jdk/bin/java 1
alternatives --install /usr/bin/javac javac /usr/lib/jvm/temurin-17-jdk/bin/javac 1
sudo alternatives --config java
选择Java17即可
修改Steve代码
修改Java代码,改到上海时区东八区,方便测试使用
public class Application implements ApplicationStarter, AutoCloseable {
private final ApplicationStarter delegate;
public Application() {
// For Hibernate validator
System.setProperty("org.jboss.logging.provider", "slf4j");
SteveConfiguration sc = SteveConfiguration.CONFIG;
log.info("Loaded the properties. Starting with the '{}' profile", sc.getProfile());
// 设置java.util.TimeZone的默认时区为东八区
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
DateTimeZone.setDefault(DateTimeZone.forID("Asia/Shanghai"));
//TimeZone.setDefault(TimeZone.getTimeZone(sc.getTimeZoneId()));
//DateTimeZone.setDefault(DateTimeZone.forID(sc.getTimeZoneId()));
log.info("Date/time zone of the application is set to {}. Current date/time: {}", sc.getTimeZoneId(), DateTime.now());
switch (sc.getProfile()) {
case DEV:
默认的配置部署到服务器是无法正常访问的,需要修改一下配置,数据库部分一定要和前面配置的数据库的鞥冷信息和端口一样,网页登录信息建议改为自己的密码,最好还是随机密码,修改的java配置文件在src\main\resources\config\prod\main.properties
# Just to be backwards compatible with previous versions, this is set to "steve",
# since there might be already configured chargepoints expecting the older path.
# Otherwise, might as well be changed to something else or be left empty.
#
context.path = steve
# Database configuration
#
db.ip = 127.0.0.1
db.port = 3306
db.schema = stevedb
db.user = steve
db.password = changeme
# Credentials for Web interface access
#
auth.user = admin
auth.password = 写上你的密码
# The header key and value for Web API access using API key authorization.
# Both must be set for Web APIs to be enabled. Otherwise, we will block all calls.
#
webapi.key = STEVE-API-KEY
webapi.value =
# Jetty configuration
#
server.host = 0.0.0.0
server.gzip.enabled = true
# Jetty HTTP configuration
#
http.enabled = true
http.port = 8080
# Jetty HTTPS configuration
#
https.enabled = false
https.port = 8443
keystore.path =
keystore.password =
# When the WebSocket/Json charge point opens more than one WebSocket connection,
# we need a mechanism/strategy to select one of them for outgoing requests.
# For allowed values see de.rwth.idsg.steve.ocpp.ws.custom.WsSessionSelectStrategyEnum.
#
ws.session.select.strategy = ALWAYS_LAST
# if BootNotification messages arrive (SOAP) or WebSocket connection attempts are made (JSON) from unknown charging
# stations, we reject these charging stations, because stations with these chargeBoxIds were NOT inserted into database
# beforehand. by setting this property to true, this behaviour can be modified to automatically insert unknown
# stations into database and accept their requests.
#
# CAUTION: setting this property to true is very dangerous, because we will accept EVERY BootNotification or WebSocket
# connection attempt from ANY sender as long as the sender knows the URL and sends a valid message.
#
auto.register.unknown.stations = false
# if this field is set, it will take precedence over the default regex we are using in
# de.rwth.idsg.steve.web.validation.ChargeBoxIdValidator.REGEX to validate the format of the chargeBoxId values
#
charge-box-id.validation.regex =
### DO NOT MODIFY ###
steve.version = ${project.version}
git.describe = ${git.commit.id.describe}
db.sql.logging = false
profile = prod
安装Maven工具,这个是编译这个项目必不可少的工具,
wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
yum install -y apache-maven
[root@iZj6cagyhi0qjy83boeqcyZ ~]# mvn -version
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 17.0.13, vendor: Eclipse Adoptium
Java home: /usr/lib/jvm/temurin-17-jdk
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.114.2.el7.x86_64", arch: "amd64", family: "unix"
编译代码,生成Java应用程序
./mvnw package
执行并测试
/usr/bin/java -jar /home/steve3.7.1/target/steve.jar
打开网页
能打开基本就成功了,登陆用户名和密码是src\main\resources\config\prod\main.properties里面的
# Credentials for Web interface access
#
auth.user = admin
auth.password = 写上你的密码
手工执行通过后,可以制作自启动Steve服务器
vi /etc/systemd/system/steve.service
[Unit]
Description=Steve Java Application
[Service]
User=root# 替换为实际运行该服务的系统用户名
ExecStart=/usr/bin/java -jar /home/steve3.7.1/target/steve.jar
WorkingDirectory=/home/steve3.7.1/target # 确保这是jar文件所在的目录
SuccessExitStatus=143 # java进程退出码为143表示正常退出
TimeoutStopSec=10
Restart=on-failure # 服务失败时进行重启
RestartSec=5 # 重启间隔时间
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable steve.service
systemctl start steve.service
systemctl status steve.service
//重启服务,用户修改了服务源码,重启服务生效。
systemctl restart steve.service