- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章使用springboot打包成zip部署,并实现优雅停机由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
众所周知springboot项目,使用springboot插件打包的话,会打包成一个包含依赖的可执行jar,非常方便。只要有java运行环境的电脑上,运行java -jar xxx.jar就可以直接运行项目.
但是这样的缺点也很明显,如果我要改个配置,要将jar包中的配置文件取出来,修改完再放回去。这样做在windows下还比较容易。如果在linux上面就很费劲了.
另外如果代码中需要读取一些文件(比如说一张图片),也被打进jar中,就没办法像在磁盘中时一句File file = new File(path)代码就可以读取了。(当然这个可以使用spring的ClassPathResource来解决).
还有很多公司项目上线后,都是增量发布,这样如果只有一个jar 的话,增量发布也是很麻烦的事情。虽然我是很讨厌这种增量发布的方式,因为会造成线上生产环境和开发环境有很多不一致的地方,这样在找问题的时候会走很多弯路。很不幸我现在在的项目也是这样的情况,而且最近接的任务就是用springboot搭建一个定时任务服务,为了维护方便,最后决定将项目打包成zip进行部署.
网上找到了很多springboot打包成zip的文章,不过基本都是将依赖从springboot的jar中拿出来放到lib目录中,再将项目的jar包中META-INF中指定lib到classpath中。这样做还是会有上面的问题.
最后我决定自己通过maven-assembly-plugin来实现这个功能.
首先maven-assembly-plugin是将项目打包的一个插件。可以通过指定配置文件来决定打包的具体要求.
我的想法是将class打包到classes中,配置文件打包到conf中,项目依赖打包到lib中,当然还有自己编写的启动脚本在bin目录中.
如图 。
maven的target/classes下就是项目编译好的代码和配置文件。原来的做法是在assembly.xml中配置筛选,将该目录下class文件打包进classes中,除class文件打包到conf中(bin目录文件打包进bin目录,项目依赖打包进lib目录)。结果发现conf目录下会有空文件夹(java包路径).
pom.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<
plugin
>
<
artifactId
>maven-assembly-plugin</
artifactId
>
<
configuration
>
<
appendAssemblyId
>false</
appendAssemblyId
>
<
descriptors
>
<
descriptor
>assembly/assembly.xml</
descriptor
>
</
descriptors
>
</
configuration
>
<
executions
>
<
execution
>
<
id
>make-assembly</
id
>
<
phase
>package</
phase
>
<
goals
>
<
goal
>single</
goal
>
</
goals
>
</
execution
>
</
executions
>
</
plugin
>
|
assembly.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<
assembly
xmlns
=
"http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"
>
<
id
>package</
id
>
<
formats
>
<
format
>zip</
format
>
</
formats
>
<
includeBaseDirectory
>true</
includeBaseDirectory
>
<
dependencySets
>
<
dependencySet
>
<
useProjectArtifact
>true</
useProjectArtifact
>
<
outputDirectory
>lib</
outputDirectory
>
<
excludes
>
<
exclude
>
${groupId}:${artifactId}
</
exclude
>
</
excludes
>
</
dependencySet
>
</
dependencySets
>
<
fileSets
>
<
fileSet
>
<
directory
>bin</
directory
>
<
outputDirectory
>/bin</
outputDirectory
>
<
fileMode
>777</
fileMode
>
</
fileSet
>
<
fileSet
>
<
directory
>${project.build.directory}/conf</
directory
>
<
outputDirectory
>/conf</
outputDirectory
>
<
excludes
>
<
exclude
>**/*.class</
exclude
>
<
exclude
>META-INF/*</
exclude
>
</
excludes
>
</
fileSet
>
<
fileSet
>
<
directory
>${project.build.directory}/classes</
directory
>
<
outputDirectory
>/classes</
outputDirectory
>
<
includes
>
<
include
>**/*.class</
include
>
<
include
>META-INF/*</
include
>
</
includes
>
</
fileSet
>
</
fileSets
>
</
assembly
>
|
其实这样是不影响项目运行的,但是我看着很难受,尝试了很多方法去修改配置来达到不打包空文件夹的效果。但是都没成功.
然后我换了个方式,通过maven-resources-plugin插件将配置文件在编译的时候就复制一份到target/conf目录下,打包的时候配置文件从conf目录中取。这样就可以避免打包空白文件夹到conf目录中的情况.
pom.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<
build
>
<
plugins
>
<
plugin
>
<
artifactId
>maven-resources-plugin</
artifactId
>
<
executions
>
<
execution
>
<
id
>compile-resources</
id
>
<
goals
>
<
goal
>resources</
goal
>
</
goals
>
<
configuration
>
<
encoding
>utf-8</
encoding
>
<
useDefaultDelimiters
>true</
useDefaultDelimiters
>
<
resources
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>true</
filtering
>
<
includes
>
<!--只对yml文件进行替换-->
<
include
>*.yml</
include
>
</
includes
>
</
resource
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>false</
filtering
>
</
resource
>
</
resources
>
</
configuration
>
</
execution
>
<
execution
>
<
id
>-resources</
id
>
<
goals
>
<
goal
>resources</
goal
>
</
goals
>
<
configuration
>
<
encoding
>utf-8</
encoding
>
<
useDefaultDelimiters
>true</
useDefaultDelimiters
>
<
resources
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>true</
filtering
>
<
includes
>
<!--只对yml文件进行替换-->
<
include
>*.yml</
include
>
</
includes
>
</
resource
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>false</
filtering
>
</
resource
>
</
resources
>
<
outputDirectory
>${project.build.directory}/conf</
outputDirectory
>
</
configuration
>
</
execution
>
</
executions
>
</
plugin
>
<!-- springboot maven打包-->
<
plugin
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-maven-plugin</
artifactId
>
</
plugin
>
<
plugin
>
<
artifactId
>maven-assembly-plugin</
artifactId
>
<
configuration
>
<
appendAssemblyId
>false</
appendAssemblyId
>
<
descriptors
>
<
descriptor
>assembly/assembly.xml</
descriptor
>
</
descriptors
>
</
configuration
>
<
executions
>
<
execution
>
<
id
>make-assembly</
id
>
<
phase
>package</
phase
>
<
goals
>
<
goal
>single</
goal
>
</
goals
>
</
execution
>
</
executions
>
</
plugin
>
</
plugins
>
</
build
>
|
assembly.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<
assembly
xmlns
=
"http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"
>
<
id
>package</
id
>
<
formats
>
<
format
>zip</
format
>
<
format
>tar.gz</
format
>
</
formats
>
<
includeBaseDirectory
>true</
includeBaseDirectory
>
<
dependencySets
>
<
dependencySet
>
<
useProjectArtifact
>true</
useProjectArtifact
>
<
outputDirectory
>lib</
outputDirectory
>
<
excludes
>
<
exclude
>
${groupId}:${artifactId}
</
exclude
>
</
excludes
>
</
dependencySet
>
</
dependencySets
>
<
fileSets
>
<
fileSet
>
<
directory
>bin</
directory
>
<
outputDirectory
>/bin</
outputDirectory
>
<
fileMode
>777</
fileMode
>
</
fileSet
>
<
fileSet
>
<
directory
>${project.build.directory}/conf</
directory
>
<
outputDirectory
>/conf</
outputDirectory
>
</
fileSet
>
<
fileSet
>
<
directory
>${project.build.directory}/classes</
directory
>
<
outputDirectory
>/classes</
outputDirectory
>
<
includes
>
<
include
>**/*.class</
include
>
<
include
>META-INF/*</
include
>
</
includes
>
</
fileSet
>
</
fileSets
>
</
assembly
>
|
pom文件中resources插件配置了2个execution,一个是正常往classes中写配置文件的execution,一个是往conf写配置文件的execution。这样做的好处是不影响maven本身的打包逻辑。如果再配置一个springboot的打包插件,也可以正常打包,执行.
原来打包成jar后,只要一句java -jar xxx.jar就可以启动项目。现在为多个文件夹的情况下,就要手动指定环境,通过java -classpath XXX xxx.xxx.MainClass来启动项目,所以写了启动脚本.
run.sh 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#!/bin/bash
#Java程序所在的目录(classes的上一级目录)
APP_HOME=..
#需要启动的Java主程序(main方法类)
APP_MAIN_CLASS="io.github.loanon.springboot.MainApplication"
#拼凑完整的classpath参数,包括指定lib目录下所有的jar
CLASSPATH="$APP_HOME/conf:$APP_HOME/lib/*:$APP_HOME/classes"
s_pid=0
checkPid() {
java_ps=`jps -l | grep $APP_MAIN_CLASS`
if [ -n "$java_ps" ]; then
s_pid=`echo $java_ps | awk '{print $1}'`
else
s_pid=0
fi
}
start() {
checkPid
if [ $s_pid -ne 0 ]; then
echo "================================================================"
echo "warn: $APP_MAIN_CLASS already started! (pid=$s_pid)"
echo "================================================================"
else
echo -n "Starting $APP_MAIN_CLASS ..."
nohup java -classpath $CLASSPATH $APP_MAIN_CLASS >./st.out 2>&1 &
checkPid
if [ $s_pid -ne 0 ]; then
echo "(pid=$s_pid) [OK]"
else
echo "[Failed]"
fi
fi
}
echo "start project......"
start
run.cmd
@echo off
set APP_HOME=..
set CLASS_PATH=%APP_HOME%/lib/*;%APP_HOME%/classes;%APP_HOME%/conf;
set APP_MAIN_CLASS=io.github.loanon.springboot.MainApplication
java -classpath %CLASS_PATH% %APP_MAIN_CLASS%
|
这样就可以启动项目了.
linux下停止tomcat一般怎么做?当然是通过运行shutdown.sh。这样做有什么好处呢?可以优雅停机。何为优雅停机?简单点说就是让代码把做了一半工作的做完,还没做的(新的任务,请求)就不要做了,然后停机.
因为做的是定时任务处理数据的功能。试想下如果一个任务做了一半,我给停了,这个任务处理的数据被我标记了在处理中,下次重启后,就不再处理,那么这些数据就一直不会再被处理。所以需要像tomcat一样能优雅停机.
网上查询springboot优雅停机相关资料。主要是使用spring-boot-starter-actuator,不过很多人说这个在1.X的springboot中可以用,springboot 2.X不能用,需要自己写相关代码来支持,亲测springboot 2.0.4.RELEASE可以用。pom文件中引入相关依赖.
pom.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<?
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"
>
<
modelVersion
>4.0.0</
modelVersion
>
<
parent
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-parent</
artifactId
>
<
version
>2.0.4.RELEASE</
version
>
<
relativePath
/>
<!-- lookup parent from repository -->
</
parent
>
<
groupId
>io.github.loanon</
groupId
>
<
artifactId
>spring-boot-zip</
artifactId
>
<
version
>1.0.0-SNAPSHOT</
version
>
<
properties
>
<
java.version
>1.8</
java.version
>
<
encoding
>UTF-8</
encoding
>
<
maven.compiler.encoding
>UTF-8</
maven.compiler.encoding
>
<
project.build.sourceEncoding
>UTF-8</
project.build.sourceEncoding
>
<
project.reporting.outputEncoding
>UTF-8</
project.reporting.outputEncoding
>
<
maven.compiler.source
>${java.version}</
maven.compiler.source
>
<
maven.compiler.target
>${java.version}</
maven.compiler.target
>
</
properties
>
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-web</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-logging</
artifactId
>
</
dependency
>
<!-- springboot监控 -->
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-actuator</
artifactId
>
</
dependency
>
<!--springboot自定义配置-->
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-autoconfigure-processor</
artifactId
>
</
dependency
>
<!--定时任务-->
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-quartz</
artifactId
>
</
dependency
>
<!--发送http请求 -->
<
dependency
>
<
groupId
>org.apache.httpcomponents</
groupId
>
<
artifactId
>httpclient</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.httpcomponents</
groupId
>
<
artifactId
>httpmime</
artifactId
>
</
dependency
>
</
dependencies
>
<
build
>
<
plugins
>
<
plugin
>
<
artifactId
>maven-resources-plugin</
artifactId
>
<
executions
>
<
execution
>
<
id
>compile-resources</
id
>
<
goals
>
<
goal
>resources</
goal
>
</
goals
>
<
configuration
>
<
encoding
>utf-8</
encoding
>
<
useDefaultDelimiters
>true</
useDefaultDelimiters
>
<
resources
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>true</
filtering
>
<
includes
>
<!--只对yml文件进行替换-->
<
include
>*.yml</
include
>
</
includes
>
</
resource
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>false</
filtering
>
</
resource
>
</
resources
>
</
configuration
>
</
execution
>
<
execution
>
<
id
>-resources</
id
>
<
goals
>
<
goal
>resources</
goal
>
</
goals
>
<
configuration
>
<
encoding
>utf-8</
encoding
>
<
useDefaultDelimiters
>true</
useDefaultDelimiters
>
<
resources
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>true</
filtering
>
<
includes
>
<!--只对yml文件进行替换-->
<
include
>*.yml</
include
>
</
includes
>
</
resource
>
<
resource
>
<
directory
>src/main/resources/</
directory
>
<
filtering
>false</
filtering
>
</
resource
>
</
resources
>
<
outputDirectory
>${project.build.directory}/conf</
outputDirectory
>
</
configuration
>
</
execution
>
</
executions
>
</
plugin
>
<!-- springboot maven打包-->
<
plugin
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-maven-plugin</
artifactId
>
</
plugin
>
<
plugin
>
<
artifactId
>maven-assembly-plugin</
artifactId
>
<
configuration
>
<
appendAssemblyId
>false</
appendAssemblyId
>
<
descriptors
>
<
descriptor
>assembly/assembly.xml</
descriptor
>
</
descriptors
>
</
configuration
>
<
executions
>
<
execution
>
<
id
>make-assembly</
id
>
<
phase
>package</
phase
>
<
goals
>
<
goal
>single</
goal
>
</
goals
>
</
execution
>
</
executions
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
|
在application.yml中配置一下 。
application.yml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
management: #开启监控管理,优雅停机
server:
ssl:
enabled:
false
endpoints:
web:
exposure:
include:
"*"
endpoint:
health:
show-details: always
shutdown:
enabled:
true
#启用shutdown端点
|
启动项目,可以通过POST方式访问/actuator/shutdown让项目停机.
实际线上可能没办法方便的发送POST请求,所以写个类处理下 。
Shutdown.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package
io.github.loanon.springboot;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpPost;
import
org.apache.http.impl.client.HttpClients;
import
java.io.IOException;
/**
* 应用关闭入口
* @author dingzg
*/
public
class
Shutdown {
public
static
void
main(String[] args) {
String url =
null
;
if
(args.length >
0
) {
url = args[
0
];
}
else
{
return
;
}
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost =
new
HttpPost(url);
try
{
httpClient.execute(httpPost);
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
只要将启动脚本中的启动类改成Shutdown类,并指定请求的地址即可.
stop.sh 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#!/bin/bash
#Java程序所在的目录(classes的上一级目录)
APP_HOME=..
#需要启动的Java主程序(main方法类)
APP_MAIN_CLASS="io.github.loanon.springboot.MainApplication"
SHUTDOWN_CLASS="io.github.loanon.springboot.Shutdown"
#拼凑完整的classpath参数,包括指定lib目录下所有的jar
CLASSPATH="$APP_HOME/conf:$APP_HOME/lib/*:$APP_HOME/classes"
ARGS="http://127.0.0.1:8080/actuator/shutdown"
s_pid=0
checkPid() {
java_ps=`jps -l | grep $APP_MAIN_CLASS`
if [ -n "$java_ps" ]; then
s_pid=`echo $java_ps | awk '{print $1}'`
else
s_pid=0
fi
}
stop() {
checkPid
if [ $s_pid -ne 0 ]; then
echo -n "Stopping $APP_MAIN_CLASS ...(pid=$s_pid) "
nohup java -classpath $CLASSPATH $SHUTDOWN_CLASS $ARGS >./shutdown.out 2>&1 &
if [ $? -eq 0 ]; then
echo "[OK]"
else
echo "[Failed]"
fi
sleep 3
checkPid
if [ $s_pid -ne 0 ]; then
stop
else
echo "$APP_MAIN_CLASS Stopped"
fi
else
echo "================================================================"
echo "warn: $APP_MAIN_CLASS is not running"
echo "================================================================"
fi
}
echo "stop project......"
stop
stop.cmd
@echo off
set APP_HOME=..
set CLASS_PATH=%APP_HOME%/lib/*;%APP_HOME%/classes;%APP_HOME%/conf;
set SHUTDOWN_CLASS=io.github.loanon.springboot.Shutdown
set ARGS=http://127.0.0.1:8080/actuator/shutdown
java -classpath %CLASS_PATH% %SHUTDOWN_CLASS% %ARGS%
|
这样就可以通过脚本来启停项目.
关于停机这块还是有缺点,主要是安全性。如果不加校验都可以访问接口,别人也就可以随便让我们的项目停机,实际操作过程中我是通过将web地址绑定到127.0.0.1这个地址上,不允许远程访问。当然也可添加spring-security做严格的权限控制,主要项目中没有用到web功能,只是spring-quartz的定时任务功能,所以就将地址绑定到本地才能访问。而且项目本身也是在内网运行,基本可以保证安全.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://www.cnblogs.com/jimmyfan/p/11340899.html 。
最后此篇关于使用springboot打包成zip部署,并实现优雅停机的文章就讲到这里了,如果你想了解更多关于使用springboot打包成zip部署,并实现优雅停机的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
背景: 我最近一直在使用 JPA,我为相当大的关系数据库项目生成持久层的轻松程度给我留下了深刻的印象。 我们公司使用大量非 SQL 数据库,特别是面向列的数据库。我对可能对这些数据库使用 JPA 有一
我已经在我的 maven pom 中添加了这些构建配置,因为我希望将 Apache Solr 依赖项与 Jar 捆绑在一起。否则我得到了 SolarServerException: ClassNotF
interface ITurtle { void Fight(); void EatPizza(); } interface ILeonardo : ITurtle {
我希望可用于 Java 的对象/关系映射 (ORM) 工具之一能够满足这些要求: 使用 JPA 或 native SQL 查询获取大量行并将其作为实体对象返回。 允许在行(实体)中进行迭代,并在对当前
好像没有,因为我有实现From for 的代码, 我可以转换 A到 B与 .into() , 但同样的事情不适用于 Vec .into()一个Vec . 要么我搞砸了阻止实现派生的事情,要么这不应该发
在 C# 中,如果 A 实现 IX 并且 B 继承自 A ,是否必然遵循 B 实现 IX?如果是,是因为 LSP 吗?之间有什么区别吗: 1. Interface IX; Class A : IX;
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在阅读标准haskell库的(^)的实现代码: (^) :: (Num a, Integral b) => a -> b -> a x0 ^ y0 | y0 a -> b ->a expo x0
我将把国际象棋游戏表示为 C++ 结构。我认为,最好的选择是树结构(因为在每个深度我们都有几个可能的移动)。 这是一个好的方法吗? struct TreeElement{ SomeMoveType
我正在为用户名数据库实现字符串匹配算法。我的方法采用现有的用户名数据库和用户想要的新用户名,然后检查用户名是否已被占用。如果采用该方法,则该方法应该返回带有数据库中未采用的数字的用户名。 例子: “贾
我正在尝试实现 Breadth-first search algorithm , 为了找到两个顶点之间的最短距离。我开发了一个 Queue 对象来保存和检索对象,并且我有一个二维数组来保存两个给定顶点
我目前正在 ika 中开发我的 Python 游戏,它使用 python 2.5 我决定为 AI 使用 A* 寻路。然而,我发现它对我的需要来说太慢了(3-4 个敌人可能会落后于游戏,但我想供应 4-
我正在寻找 Kademlia 的开源实现C/C++ 中的分布式哈希表。它必须是轻量级和跨平台的(win/linux/mac)。 它必须能够将信息发布到 DHT 并检索它。 最佳答案 OpenDHT是
我在一本书中读到这一行:-“当我们要求 C++ 实现运行程序时,它会通过调用此函数来实现。” 而且我想知道“C++ 实现”是什么意思或具体是什么。帮忙!? 最佳答案 “C++ 实现”是指编译器加上链接
我正在尝试使用分支定界的 C++ 实现这个背包问题。此网站上有一个 Java 版本:Implementing branch and bound for knapsack 我试图让我的 C++ 版本打印
在很多情况下,我需要在 C# 中访问合适的哈希算法,从重写 GetHashCode 到对数据执行快速比较/查找。 我发现 FNV 哈希是一种非常简单/好/快速的哈希算法。但是,我从未见过 C# 实现的
目录 LRU缓存替换策略 核心思想 不适用场景 算法基本实现 算法优化
1. 绪论 在前面文章中提到 空间直角坐标系相互转换 ,测绘坐标转换时,一般涉及到的情况是:两个直角坐标系的小角度转换。这个就是我们经常在测绘数据处理中,WGS-84坐标系、54北京坐标系
在软件开发过程中,有时候我们需要定时地检查数据库中的数据,并在发现新增数据时触发一个动作。为了实现这个需求,我们在 .Net 7 下进行一次简单的演示. PeriodicTimer .
二分查找 二分查找算法,说白了就是在有序的数组里面给予一个存在数组里面的值key,然后将其先和数组中间的比较,如果key大于中间值,进行下一次mid后面的比较,直到找到相等的,就可以得到它的位置。
我是一名优秀的程序员,十分优秀!