背景:因为项目使用的是Spring cloud的框架,以及部署使用了Docker ,后来要和Sap对接接口。于是我们选择了直连使用RFC连接。
接下来我们言归正传:
1。创建一个Spring boot的项目
2.添加sapjco-3.0.6.jar,sapjco3.dll(是window环境),libsapjco3.so(centos环境)在lib目录下,添加依赖
3.修改pom.xml文件
<?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>com.github.pig</groupId>
<artifactId>cfmoto-modules</artifactId>
<version>1.3.1</version>
</parent>
<groupId>com.cfmoto.sap.api</groupId>
<artifactId>cfmoto-sap-api</artifactId>
<version>1.3.1</version>
<name>cfmoto-sap-api</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cfmoto.sap.api</groupId> <!--自定义-->
<artifactId>jco</artifactId> <!--自定义-->
<version>3.0.6</version> <!--自定义-->
<scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
<systemPath>${basedir}/lib/sapjco-3.0.6.jar</systemPath> <!--项目根目录下的lib文件夹下-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>BOOT-INF</targetPath>
<includes>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!--<targetPath>BOOT-INF/classes/</targetPath>-->
<includes>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
</build>
</project>
4.在yml文件添加统一配置
#sap连接配置
sap:
jco:
provider:
destName: ABAP
ashost: 172.16.0.225
sysnr: 00
client: 300
user: space
passwd: 123456
pool_capacity: 10
peak_limit: 50
saprouter: "/H/116.62.119.46/S/3299/H/"
lang: zh
5.添加类JcoProviderConfig
package com.cfmoto.sap.api.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class JcoProviderConfig {
@Value("${sap.jco.provider.destName}")
public String jcoDestName;
@Value("${sap.jco.provider.ashost}")
public String jcoAshost;
@Value("${sap.jco.provider.sysnr}")
public String jcoSysnr;
@Value("${sap.jco.provider.client}")
public String jcoClient;
@Value("${sap.jco.provider.user}")
public String jcoUser;
@Value("${sap.jco.provider.passwd}")
public String jcoPasswd;
@Value("${sap.jco.provider.pool_capacity}")
public String jcoPoolCapacity;
@Value("${sap.jco.provider.peak_limit}")
public String jcoPeakLimit;
@Value("${sap.jco.provider.saprouter}")
public String jcoSaprouter;
@Value("${sap.jco.provider.lang}")
public String jcoLang;
}
6.添加类CustomDestinationDataProvider继承DestinationDataProvider
package com.cfmoto.sap.api.config;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class CustomDestinationDataProvider implements DestinationDataProvider {
private DestinationDataEventListener eL;
private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();
public Properties getDestinationProperties(String destinationName){
return secureDBStorage.get(destinationName);
}
public void setDestinationDataEventListener(DestinationDataEventListener eventListener){
this.eL = eventListener;
}
public boolean supportsEvents(){
return true;
}
public void addDestinationProperties(String destName, Properties properties){
synchronized(secureDBStorage){
secureDBStorage.put(destName, properties);
eL.updated(destName); // create or updated
}
}
}
7.添加一个service类:CustomJcoService
package com.cfmoto.sap.api.service;
import com.cfmoto.sap.api.utils.R;
import java.util.Map;
public interface CustomJcoService {
//测试连接是否连通
R pingCalls( String destName );
R execute( String functionName, Map<String, Object> paramMap );
}
8.实现CustomJcoService类的方法CustomJcoServiceImpl