中国龙卷风 發表於 2020-8-19 15:26:00

在spring boot中使用sapjco3,并用docker部署到Linux服务器

<p><span style="font-family: 宋体">一、在windows中的配置</span></p>
<p><span style="font-family: 宋体">二、linux的配置</span></p>
<p><span style="font-family: 宋体">三、spring boot项目构建</span></p>
<p><span style="font-family: 宋体">sapjco3包含很多个操作系统版本,不同的操作系统需要使用不同的配置文件和配置方式,不过sapjco3.jar是可以通用的。</span></p>
<p><span style="font-family: 宋体"><img src="https://img2020.cnblogs.com/blog/1543878/202008/1543878-20200819105934366-640195848.png"></span></p>
<h1><span style="font-family: 宋体">一、在windows中的配置</span></h1>
<p><span style="font-family: 宋体">windows中需要的文件主要包括:</span></p>
<p><span style="font-family: 宋体">sapjco3.dll</span></p>
<p><span style="font-family: 宋体">sapjco3.pdb</span></p>
<p><span style="font-family: 宋体">sapjco3.jar</span></p>
<h2><span style="font-family: 宋体">1、sapjco3.dll文件</span></h2>
<p><span style="font-family: 宋体; font-size: 14px">32位系统&nbsp;</span></p>
<p><span style="font-family: 宋体; font-size: 14px">将 sapjco3.dll 加入到c:/windows/system32/目录 或者 将 sapjco3.dll 加入到 JDK/bin 目录下;</span></p>
<p><span style="font-family: 宋体; font-size: 14px">64位系统</span></p>
<p><span style="font-family: 宋体; font-size: 14px">将 sapjco3.dll 加入到c:/windows/SysWOW64/目录 或者 将 sapjco3.dll 加入到 JDK/bin 目录下;</span></p>
<p><span style="font-family: 宋体; font-size: 14px">64位机可以直接将sapjco3.dll放在"C:\Windows\System32"目录下,这样以后不必针对具体的应用来配置它;</span></p>
<p><span style="font-family: 宋体; font-size: 14px"><img src="https://img2020.cnblogs.com/blog/1543878/202008/1543878-20200819110146552-1363688094.png"></span></p>
<p><span style="font-family: 宋体">上面的配置是为了将它放在系统环境变量path可以找到的地方,所以也可以直接把sapjco3.dll文件所在的位置,配置到系统环境变量path下面;</span></p>
<h2><span style="font-family: 宋体; font-size: 14pt">2、sapjco3.pdb文件</span></h2>
<p><span style="font-family: 宋体; font-size: 14px">在springboot中使用sapjco3可以不用配置sapjco3.pdb,如果在kettle中需要调用sap的接口,则需要把sapjco3.pdb放在kettle的根目录"${KETTLE_HOME}/"下面;</span></p>
<h2><span style="font-family: 宋体; font-size: 14pt">3、sapjco3.jar文件</span></h2>
<p><span style="font-family: 宋体; font-size: 14px">这个文件就是java调用sap的主要api,包含许多方法,如连接sap、获取接口参数、设置接口参数和获取返回参数等;</span></p>
<p><span style="font-family: 宋体; font-size: 14px">这里<strong>推荐将sapjco3.jar放在项目的lib目录下面,然后在项目打包的时候,将本地依赖的jar包打包到项目jar中</strong>;</span></p>
<p><span style="font-family: 宋体; font-size: 14px">也可以将sapjco3.jar放在${JAVA_HOME}/lib目录下或者放在其他目录并将该目录下的sapjco3.jar添加到系统环境变量CLASSPATH下面;</span></p>
<p><span style="font-family: 宋体; font-size: 14px">也可以在pom.xml中引入sapjco3.jar依赖,这样项目打包的时候会将sapjco3.0.14.jar打包到项目jar里面,但是这样sapjco3.jar文件是带有版本后缀的,在项目部署的时候,会启动失败,报</span><span style="font-family: 宋体"><span style="font-size: 14px">错</span><span style="font-size: 14px">:</span><span style="color: rgba(255, 0, 0, 1)">It is not allowed to rename or repackage the original archive “sapjco3.jar”</span>。</span><span style="font-family: 宋体; font-size: 14px">需要手动进入jar包,将sapjco3.0.14.jar名称修改为sapjco3.jar;</span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> https://mvnrepository.com/artifact/com.sap.conn.jco/sapjco3 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.sap.conn.jco<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>sapjco3<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>3.0.14<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: 宋体">还有一种方式是使用maven的install命令将sapjco3.jar安装到本地仓库,然后在项目中引入依赖,但是切记jar包不能后缀版本号;</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<hr>
<h1>&nbsp;<span style="font-family: 宋体; font-size: 18pt">二、linux的配置</span></h1>
<p><span style="font-family: 宋体; font-size: 14px">linux需要的文件包括:</span></p>
<p><span style="font-family: 宋体; font-size: 14px">libsapjco3.so</span></p>
<p><span style="font-family: 宋体; font-size: 14px">sapjco3.jar</span></p>
<p><span style="font-family: 宋体; font-size: 14px">其中jar包sapjco3.jar与windows版的可以通用;重点是将libsapjco3.so文件配置到linux的jre/lib运行时环境中或者项目可以加载到libsapjco3.so的其他位置;</span></p>
<h2><span style="font-family: 宋体; font-size: 14pt">1、Linux系统:</span></h2>
<p><span style="font-family: 宋体; font-size: 14px">1)将压缩文件解压到某个目录下面,比如/home/appuser/sapjco3/ 下面:</span></p>
<blockquote>
<p><span style="font-family: 宋体; font-size: 14px">tar -zxvf&nbsp;sapjco3-linuxx86_64-3.0.17.tgz -C /home/appuser/sapjco3</span></p>
</blockquote>
<p><span style="font-family: 宋体; font-size: 14px">2)配置环境变量</span></p>
<blockquote>
<p><span style="font-family: 宋体; font-size: 14px">vim /etc/profile</span></p>
</blockquote>
<p><span style="font-family: 宋体; font-size: 14px">在配置文件内加上下面的内容:</span></p>
<blockquote>
<p><span style="font-family: 宋体; font-size: 14px">export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/home/appuser/sapjco3</span></p>
<p><span style="font-family: 宋体; font-size: 14px">export CLASSPATH=.:${JAVA_HOME}/lib:${LD_LIBRARY_PATH}/sapjco3.jar</span></p>
</blockquote>
<p><span style="font-family: 宋体; font-size: 14px">或者</span></p>
<p><span style="font-family: 宋体; font-size: 14px">先将文件libsapjco3.so复制到${JAVA_HOME}/jre/lib/amd64/server文件夹下</span></p>
<blockquote>
<p><span style="font-family: 宋体">export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${JAVA_HOME}/jre/lib/amd63/server</span></p>
<p><span style="font-family: 宋体">export CLASSPATH=.:${JAVA_HOME}/lib:${LD_LIBRARY_PATH}/sapjco3.jar</span></p>
</blockquote>
<p><span style="font-family: 宋体">上面配置的重点是libsapjco3.so的位置,对于sapjco3.jar,可以配置到环境变量${CLASSPATH}中,也可以打包在项目中;</span></p>
<p><span style="font-family: 宋体">官方的推荐如下:</span></p>
<p><span style="font-family: 宋体"><img src="https://img2020.cnblogs.com/blog/1543878/202008/1543878-20200819115035594-718495692.png"></span></p>
<h2>&nbsp;<span style="font-family: 宋体; font-size: 14pt">2、docker:</span></h2>
<p><span style="font-family: 宋体; font-size: 14px">在docker中可以使用 <span style="color: rgba(255, 0, 0, 1)"><strong>-v 宿主机目录:容器目录</strong></span> 的方式将libsapjco3.so和sapjco3.jar进行映射:</span></p>
<blockquote>
<p><span style="font-family: 宋体; font-size: 14px">docker run -d \</span></p>
<p><span style="font-family: 宋体; font-size: 14px">--restart=always \</span></p>
<p><span style="font-family: 宋体; font-size: 14px">--name springboot-sapjco3 \</span></p>
<p><span style="font-family: 宋体; font-size: 14px">-v "$PWD/sapringboot-sapjco3":/usr/app \</span></p>
<p><span style="font-family: 宋体; font-size: 14px"><strong><span style="color: rgba(255, 0, 0, 1)">-v /home/appuser/sapjco3/libsapjco3.so:/usr/lib/libsapjco3.so</span></strong> \</span></p>
<p><span style="font-family: 宋体; font-size: 14px">--workdir="/usr/app" \</span></p>
<p><span style="font-family: 宋体; font-size: 14px">-p 8080:8080 \</span></p>
<p><span style="font-family: 宋体; font-size: 14px">java:8u111 \</span></p>
<p><span style="font-family: 宋体; font-size: 14px">java -jar sprringboot-sapjco3.jar</span></p>
</blockquote>
<p><span style="font-family: 宋体; font-size: 14px">这样就将宿主机的libsapjco3.so映射到了容器的/usr/lib下面;</span></p>
<p><span style="font-family: 宋体; font-size: 14px">如果项目中没有打包sapjco3.jar文件,也可以在docker run 的 -v 命令中将该文件映射到jdk的lib目录下:</span></p>
<blockquote>
<p><span style="font-family: 宋体; font-size: 14px">-v /hemo/appuser/sapjco3/sapjco3.jar:/usr/lib/jvm/java-8-openjdk-amd64/lib/sapjco3.jar</span></p>
</blockquote>
<h1><span style="font-family: 宋体; font-size: 18pt">三、spring boot项目构建</span></h1>
<h2><span style="font-family: 宋体; font-size: 14pt">1、项目结构与配置文件</span></h2>
<p><span style="font-family: 宋体; font-size: 14px"><img src="https://img2020.cnblogs.com/blog/1543878/202008/1543878-20200819133801805-1863216373.png"></span></p>
<p><span style="font-family: 宋体">将sapjco3.jar放在项目resource/lib下面;</span></p>
<p><span style="font-family: 宋体">CONNECT_SAP_CONFIG.jcoDestination文件是连接SAP的<span style="font-size: 14px">配置</span>文件,必需放在项目的根目录下面,其内容如下:</span></p>
<blockquote>
<pre><span style="font-size: 14px; font-family: 宋体">#for tests only !</span><br><span style="font-size: 14px; font-family: 宋体">jco.client.lang=en</span><br><span style="font-size: 14px; font-family: 宋体">jco.client.client=710</span><br><span style="font-size: 14px; font-family: 宋体">jco.client.user=admin</span><br><span style="font-size: 14px; font-family: 宋体">jco.client.passwd=admin</span><br><span style="font-size: 14px; font-family: 宋体">jco.client.sysnr=00</span><br><span style="font-size: 14px; font-family: 宋体">jco.client.ashost=192.168.1.11</span></pre>
</blockquote>
<p><span style="font-family: 宋体">配置pom.xml,引入自定义jar包,并在项目打包时将自定义jar包打包进项目的jar:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">maven打包时,将resources/lib/sapjco3.jar一并打包</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <strong><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.sap<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>sapjco3<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.0<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>system<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
          <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">systemPath</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>${project.basedir}/src/main/resources/lib/sapjco3.jar<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">systemPath</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></strong>
      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">其他依赖</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
            ......
      </span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-maven-plugin<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <strong><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">configuration</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">executable</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>true<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">executable</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">includeSystemScope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>true<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">includeSystemScope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">configuration</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></strong>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>springloaded<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.2.5.RELEASE<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h2><span style="font-family: 宋体; font-size: 14pt">2.获取sap连接对象</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> JCoDestinationBuilder {
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JCoDestination destination = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Logger logger =<span style="color: rgba(0, 0, 0, 1)"> LogUtils.getPlatformLogger();
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JCoDestination build() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> JCoException {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">null</span>==<span style="color: rgba(0, 0, 0, 1)">destination) {
            </span><span style="color: rgba(0, 0, 255, 1)">synchronized</span> (JCoDestinationBuilder.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">null</span> ==<span style="color: rgba(0, 0, 0, 1)"> destination)
                        destination </span>= JCoDestinationManager.getDestination("<span style="color: rgba(255, 0, 0, 1)"><strong>CONNECT_SAP_CONFIG</strong></span>"<span style="color: rgba(0, 0, 0, 1)">);
                  destination.ping();
                  logger.info(</span>"Connect to SAP successfully......"<span style="color: rgba(0, 0, 0, 1)">);
                } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (JCoException e) {
                  e.printStackTrace();
                  logger.error(</span>"Connect to SAP failed, error msg: " +<span style="color: rgba(0, 0, 0, 1)"> e.toString());
                }
            }
      }
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">logger.info(String.valueOf(destination.getAttributes()));</span>
      <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> destination;
    }
}</span></pre>
</div>
<h2><span style="font-family: 宋体; font-size: 14pt">3.调用sap接口函数</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Service
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> SappiMonitorServiceImpl <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> SappiMonitorService {
    @Autowired
    Sm58EntityRepository sm58EntityRepository;
    @Autowired
    MessageService messageService;
    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Environment environment;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Logger logger =<span style="color: rgba(0, 0, 0, 1)"> LogUtils.getPlatformLogger();</span><span style="color: rgba(0, 0, 0, 1)">

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> JSONObject monitorSm58(SappiSm58 sappiSm58) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> JCoException {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (sappiSm58==<span style="color: rgba(0, 0, 255, 1)">null</span>) sappiSm58 = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SappiSm58();
      <strong>JCoDestination destination </strong></span><strong>=</strong><span style="color: rgba(0, 0, 0, 1)"><strong> JCoDestinationBuilder.build();</strong>
      <strong>JCoFunction function </strong></span><strong>= destination.getRepository().getFunction("ZMONITOR_SM58"<span style="color: rgba(0, 0, 0, 1)">);
      JCoParameterList importlist </span>=<span style="color: rgba(0, 0, 0, 1)"> function.getImportParameterList();
      JCoParameterList exportList </span>=<span style="color: rgba(0, 0, 0, 1)"> function.getExportParameterList();
      JCoParameterList tables </span>=</strong><span style="color: rgba(0, 0, 0, 1)"><strong> function.getTableParameterList();</strong><br>
      <strong>importlist.setValue(</strong></span><strong>"I_BEGIN_DATE"<span style="color: rgba(0, 0, 0, 1)">,sappiSm58.getI_begin_date());
      importlist.setValue(</span>"I_END_DATE"<span style="color: rgba(0, 0, 0, 1)">,sappiSm58.getI_end_date());
      </span></strong><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            <strong>function.execute(destination);
            </strong></span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">logger.warn(function.toXML());</span>
      } <span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (JCoException e) {
            e.printStackTrace();
            logger.error(</span>"execute()failed......"<span style="color: rgba(0, 0, 0, 1)">);
      }
      <strong>JCoTable t_arfcistate </strong></span><strong>= tables.getTable("T_ARFCISTATE"<span style="color: rgba(0, 0, 0, 1)">);
      </span></strong><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0;i&lt;t_arfcistate.getNumRows();i++<span style="color: rgba(0, 0, 0, 1)">) {
            t_arfcistate.setRow(i);
            Sm58Entity sm58Entity </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Sm58Entity();
            sm58Entity.setArfcipid(t_arfcistate.getString(</span>"ARFCIPID"<span style="color: rgba(0, 0, 0, 1)">));
            sm58Entity.setArfcpid(t_arfcistate.getString(</span>"ARFCPID"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfctime(t_arfcistate.getString("ARFCTIME"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfctidcnt(t_arfcistate.getString("ARFCTIDCNT"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcdest(t_arfcistate.getString("ARFCDEST"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcluwcnt(t_arfcistate.getString("ARFCLUWCNT"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcstate(t_arfcistate.getString("ARFCSTATE"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcfnam(t_arfcistate.getString("ARFCFNAM"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcreturn(t_arfcistate.getString("ARFCRETURN"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcuzeit(t_arfcistate.getString("ARFCUZEIT"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcdatum(t_arfcistate.getString("ARFCDATUM"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcuser(t_arfcistate.getString("ARFCUSER"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcretrys(t_arfcistate.getLong("ARFCRETRYS"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfctcode(t_arfcistate.getString("ARFCTCODE"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcrhost(t_arfcistate.getString("ARFCRHOST"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcmsg(t_arfcistate.getString("ARFCMSG"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setArfcreserv(t_arfcistate.getString("ARFCRESERV"<span style="color: rgba(0, 0, 0, 1)">));</span>
            sm58Entity.setHash(t_arfcistate.getString("HASH"<span style="color: rgba(0, 0, 0, 1)">));
            sm58EntityRepository.save(sm58Entity);
      }
      String markdown </span>= SappiMonitorServiceImpl.generateMarkdown("monitorSm58"<span style="color: rgba(0, 0, 0, 1)">,
                exportList.getString(</span>"E_ERR_NUM").replaceAll(" ",""<span style="color: rgba(0, 0, 0, 1)">));
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用MessageService接口</span>
      MarkdownMessage markdownMessage = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MarkdownMessage();
      markdownMessage.setUserid(environment.getProperty(</span>"sap.pi.monitor.sm58.touser"<span style="color: rgba(0, 0, 0, 1)">));
      markdownMessage.setMarkdown(markdown);
      JSONObject jsonObject </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JSONObject();
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> ("E".equals(exportList.getString("E_STATUS"<span style="color: rgba(0, 0, 0, 1)">))) {
            jsonObject </span>=<span style="color: rgba(0, 0, 0, 1)"> messageService.sendMarkdown(markdownMessage);
            jsonObject.put(</span>"errCode","-1"<span style="color: rgba(0, 0, 0, 1)">);
            jsonObject.put(</span>"errMsg","系统运行出现" + exportList.getString("E_ERR_NUM") +"条错误"<span style="color: rgba(0, 0, 0, 1)">);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            jsonObject.put(</span>"errCode","0"<span style="color: rgba(0, 0, 0, 1)">);
            jsonObject.put(</span>"errMsg","系统运行正常"<span style="color: rgba(0, 0, 0, 1)">);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jsonObject;
    }</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: 宋体">上面代码中,<strong>粗体</strong>部分是获取sap连接对象、接口参数赋值、接口调用和获取返回结果的主要代码;</span></p>
<h2><span style="font-family: 宋体; font-size: 14pt">4.编写相应的Controller代码</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Api(tags </span>= "SAPPI运行状态监控", description = "监控SAPPI运行状态"<span style="color: rgba(0, 0, 0, 1)">)
@Controller
@RequestMapping(</span>"/monitor/sappi"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> SappiMonitorController {
    @Autowired
    SappiMonitorService sappiMonitorService;</span><span style="color: rgba(0, 0, 0, 1)">

    @ApiOperation(value </span>= "事务代码:SXI_MONITOR", notes = "请求方式:POST"<span style="color: rgba(0, 0, 0, 1)">)
    @PostMapping(</span>"/sxi_monitor"<span style="color: rgba(0, 0, 0, 1)">)
    @ResponseBody
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> JSONObject monitorSxi_monitor(@RequestBody(required = <span style="color: rgba(0, 0, 255, 1)">false</span>) SappiSxi_monitor sappiSxi_monitor) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sappiMonitorService.monitorSxi_monitor(sappiSxi_monitor);
    }

    @ApiOperation(value </span>= "事务代码:SM58", notes = "请求方式:POST"<span style="color: rgba(0, 0, 0, 1)">)
    @PostMapping(</span>"/sm58"<span style="color: rgba(0, 0, 0, 1)">)
    @ResponseBody
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> JSONObject monitorSm58(@RequestBody(required = <span style="color: rgba(0, 0, 255, 1)">false</span>) SappiSm58 sappiSm58) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sappiMonitorService.monitorSm58(sappiSm58);
    }
}</span></pre>
</div>
<h2><span style="font-family: 宋体; font-size: 14pt">5.运行与测试</span></h2>
<p><span style="font-family: 宋体">上面的代码中,只贴出了sapjco使用的主要部分,其中数据库的持久化部分和将sap接口调用结果发送到微信通知的部分都可以删减掉;</span></p>
<p><span style="font-family: 宋体">测试时,请求方式改为GET方式即可:</span></p>
<p><span style="font-family: 宋体">即<strong>@PostMapping("/sm58")&nbsp;</strong>改为 <strong>@GetMapping("/sm58")</strong><br></span></p>
<p><span style="font-family: 宋体">然后在浏览器或者用postman直接调用接口:<strong>localhost:8080/monitor/sappi/sm58</strong></span></p>
<p><span style="font-family: 宋体">将会看到如下返回结果:</span></p>
<blockquote>
<p><span style="font-family: 宋体">{</span><br><span style="font-family: 宋体">  "invalidUser": "",</span><br><span style="font-family: 宋体">  "invalidTagList": [],</span><br><span style="font-family: 宋体">  "errCode": "-1",</span><br><span style="font-family: 宋体">  "errMsg": "系统运行出现1 条错误",</span><br><span style="font-family: 宋体">  "invalidPartyList": [],</span><br><span style="font-family: 宋体">  "invalidUserList": []</span><br><span style="font-family: 宋体">}</span></p>


</blockquote>
<p>&nbsp;</p>
<h2><span style="font-family: 宋体; font-size: 14pt">6、打包与部署</span></h2>
<p><span style="font-family: 宋体">使用maven的package命令将项目打包成jar包,并上传到linux服务器/home/appuser/springboot-sapjco目录下面;</span></p>
<p><span style="font-family: 宋体">使用上面的docker命令部署并运行项目;</span></p>
<p>&nbsp;</p>
<h1><span style="font-family: 宋体; font-size: 18pt">四、各操作系统sapjco文件下载:</span></h1>
<p><span style="font-family: 宋体">链接: https://pan.baidu.com/s/1-1Cxam_9Q6o0z_NqqOGvoA 提取码: v7c8</span></p>
<p><span style="font-family: 宋体">或者扫码保存:</span></p>
<p><span style="font-family: 宋体"><img src="https://img2020.cnblogs.com/blog/1543878/202008/1543878-20200819160719646-1927666133.png"></span></p>
<p>&nbsp;</p>
<p><span style="font-family: 宋体"></span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/ybinlog/p/13527212.html
頁: [1]
查看完整版本: 在spring boot中使用sapjco3,并用docker部署到Linux服务器