前言:
最近我司的一个新需求,对接一下银联OpenAPI开放的几个接口(https://open.unionpay.com/tjweb/support/doc/online/3)。其实需求很简单,但是因为没玩过嘛,所以,简单在这里撸一篇对接的记录吧。
本篇公号主要分享以下四个方面的内容:
1. Springboot项目中Redis的配置
2. 获取第三方接口token的一般逻辑与实现
3. http请求的线程安全工具类分享
4.银联通用全文OCR识别接口对接demo
废话不多说,干货送上。
一、Springboot项目中Redis的配置
对接的项目中恰好没有redis的配置,至于为什么要用redis呢?下文会详细述说,这边就是简单分享一下我的配置过程:
1. pom Maven的引入
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-redisartifactId>dependency>//因为我的配置文件中有直接对连接池的配置,所以需要下面这个jar//此外:注意⚠️Jedis与redis版本兼容的问题,高版本的我在试用3.0.1的时候,会报bean无法注入的错误!<dependency> <groupId>redis.clientsgroupId> <artifactId>jedisartifactId> <version>2.9.0version>dependency>
2. redis的配置文件(bigdata-redis-config.xml)
注意⚠️:不同的redis部署方式,其配置文件肯定是不一样的!以下是集群部署方式的配置文件。
补充Redis的四种部署方式:
1.单机模式2.主从模式3.哨兵模式4.集群方式
详细配置文件信息:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:apollo="http://www.ctrip.com/schema/apollo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd"> <apollo:config namespaces="OPERATION.redis.bigdata.cluster"/> <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="3">property> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="${redis.bigdata.cluster.host1}">constructor-arg> <constructor-arg name="port" value= "${redis.bigdata.cluster.port1}" >constructor-arg> bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="${redis.bigdata.cluster.host2}">constructor-arg> <constructor-arg name="port" value= "${redis.bigdata.cluster.port2}" >constructor-arg> bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host"