Maven配置
<properties>
<fruit.version>1.0.4</fruit.version>
<java.version>8</java.version>
<!-- Spring Cloud Alibaba 最新版本 -->
<spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
<!-- Spring Cloud 版本-->
<spring.cloud.version>2021.0.1</spring.cloud.version>
<xxl-job.version>2.3.0</xxl-job.version>
<hystrix-version>1.5.18</hystrix-version>
<!-- spock version start -->
<spock.version>1.3-groovy-2.4</spock.version>
<groovy.version>2.4.21</groovy.version>
<powermock.version>2.0.4</powermock.version>
<jmockit.version>1.39</jmockit.version>
<junitparaams.versopm>1.1.0</junitparaams.versopm>
<!-- spock version end -->
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>me.ghui</groupId>
<artifactId>Fruit</artifactId>
<version>${fruit.version}</version>
</dependency>
<!-- feign start -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.8.0</version>
</dependency>
<!-- feign end -->
<!-- spock test dependency start -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>${spock.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.28.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<!-- db test -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
<!-- db end -->
<!-- spock test dependency end -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${xxl-job.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>${hystrix-version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>${hystrix-version}</version>
</dependency>
</dependencies>
忽略SSL配置
import feign.Client;
import feign.httpclient.ApacheHttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
public class HttpClientFactory {
public static Client buildIgnoreSSLClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
return new ApacheHttpClient(HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setSSLSocketFactory(sslConnectionSocketFactory)
.build());
}
}
FtFeignClient
import com.boommanpro.xportsreserve.rest.FtServerNotifyClient;
import com.boommanpro.xportsreserve.utils.HttpClientFactory;
import feign.Feign;
import feign.Logger;
import feign.Request;
import feign.form.FormEncoder;
import feign.jackson.JacksonDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Slf4j
@Configuration
public class FtServerRestConfiguration {
public interface FTServerConstant {
String SERVER_URL = "https://sctapi.ftqq.com/";
}
@Bean
public FtServerNotifyClient ftServerNotifyClient() throws Exception {
return Feign.builder()
.encoder(new FormEncoder())
.decoder(new JacksonDecoder())
.client(HttpClientFactory.buildIgnoreSSLClient())
.logLevel(Logger.Level.FULL)
.options(new Request.Options(3, TimeUnit.MINUTES, 3, TimeUnit.MINUTES, true))
.target(FtServerNotifyClient.class, FTServerConstant.SERVER_URL);
}
}
FtServerNotifyClient
import com.boommanpro.xportsreserve.model.FtServerBody;
import com.boommanpro.xportsreserve.model.FtServerResult;
import feign.Headers;
import feign.Param;
import feign.RequestLine;
public interface FtServerNotifyClient {
@RequestLine("POST {sendKey}.send")
@Headers("Content-Type: application/x-www-form-urlencoded;charset=UTF-8")
FtServerResult send(@Param String sendKey, FtServerBody ftServerBody);
}
FtServerResult
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@Data
public class FtServerResult {
@JsonProperty("code")
private Integer code;
@JsonProperty("message")
private String message;
@JsonProperty("data")
private DataDTO data;
@NoArgsConstructor
@Data
public static class DataDTO {
@JsonProperty("pushid")
private String pushid;
@JsonProperty("readkey")
private String readkey;
@JsonProperty("error")
private String error;
@JsonProperty("errno")
private Integer errno;
}
}
FtNotifyAccountService
import com.alibaba.fastjson.JSON;
import com.boommanpro.xportsreserve.model.CommitResult;
import com.boommanpro.xportsreserve.model.FtServerBody;
import com.boommanpro.xportsreserve.model.FtServerResult;
import com.boommanpro.xportsreserve.rest.FtServerNotifyClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
public class FtNotifyAccountService {
@Autowired
private FtServerNotifyClient ftServerNotifyClient;
public void notifyMessage(String sendKey, String text, String desp) {
FtServerResult result = ftServerNotifyClient.send(sendKey, new FtServerBody(text, desp));
log.info("notify result:{}", result);
}
}