Commit 4144c46d9fe39632fa6803691cb8019ec3bc5b66

Authored by 王通
1 parent c9398b00

1.https请求

src/main/java/com/bsth/server_ws/util/ControlHttpUtils.java
1   -package com.bsth.server_ws.util;
2   -
3   -import com.alibaba.fastjson.JSONArray;
4   -import com.bsth.entity.ScheduleRealInfo;
5   -import com.bsth.util.ConfigUtil;
6   -import org.apache.http.client.config.RequestConfig;
7   -import org.apache.http.client.methods.CloseableHttpResponse;
8   -import org.apache.http.client.methods.HttpGet;
9   -import org.apache.http.impl.client.CloseableHttpClient;
10   -import org.apache.http.impl.client.HttpClients;
11   -import org.apache.http.util.EntityUtils;
12   -import org.slf4j.Logger;
13   -import org.slf4j.LoggerFactory;
14   -
15   -import java.io.IOException;
16   -import java.util.List;
17   -
18   -/**
19   - * Created by panzhao on 2017/3/15.
20   - */
21   -public class ControlHttpUtils {
22   -
23   - static Logger logger = LoggerFactory.getLogger(ControlHttpUtils.class);
24   -
25   - static String url;
26   -
27   - static String secretKey;
28   -
29   - static {
30   - url = ConfigUtil.get("http.control.service_data_url");
31   - secretKey = ConfigUtil.get("http.control.secret.key");
32   - }
33   -
34   - /**
35   - * 从调度系统获取班次信息
36   - *
37   - * @return
38   - */
39   - public static List<ScheduleRealInfo> getCurrentDayPlan(String companyId, String workId) throws IOException {
40   -
41   - CloseableHttpClient httpClient = null;
42   - List<ScheduleRealInfo> rs = null;
43   - httpClient = HttpClients.createDefault();
44   - //超时时间
45   - RequestConfig requestConfig = RequestConfig.custom()
46   - .setConnectTimeout(2000).setConnectionRequestTimeout(1000)
47   - .setSocketTimeout(3000).build();
48   -
49   - HttpGet get = new HttpGet(url + "/getCurrentDayPlan?companyId=" + companyId + "&workId=" + workId + "&secretKey=" + secretKey);
50   - get.setConfig(requestConfig);
51   -
52   - CloseableHttpResponse response = httpClient.execute(get);
53   -
54   - rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class);
55   - return rs;
56   - }
57   -
58   - /**
59   - * 从调度系统获取进出场班次信息
60   - * @param comanyId
61   - * @param jcOrCc
62   - * @return
63   - */
64   - public static List<ScheduleRealInfo> returnJCCInfo(String companyId, String jcOrCc) throws IOException{
65   -
66   - String method = jcOrCc.equals("out")?"/returnCCInfo":"/returnJCInfo";
67   -
68   - CloseableHttpClient httpClient = null;
69   - List<ScheduleRealInfo> rs = null;
70   - httpClient = HttpClients.createDefault();
71   - //超时时间
72   - RequestConfig requestConfig = RequestConfig.custom()
73   - .setConnectTimeout(2000).setConnectionRequestTimeout(1000)
74   - .setSocketTimeout(3000).build();
75   -
76   - HttpGet get = new HttpGet(url + method+"?companyId=" + companyId + "&secretKey=" + secretKey);
77   - get.setConfig(requestConfig);
78   -
79   - CloseableHttpResponse response = httpClient.execute(get);
80   -
81   - rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class);
82   - return rs;
83   - }
84   -}
  1 +package com.bsth.server_ws.util;
  2 +
  3 +import com.alibaba.fastjson.JSONArray;
  4 +import com.bsth.entity.ScheduleRealInfo;
  5 +import com.bsth.util.ConfigUtil;
  6 +import com.bsth.util.HttpClientUtils;
  7 +import org.apache.http.client.config.RequestConfig;
  8 +import org.apache.http.client.methods.CloseableHttpResponse;
  9 +import org.apache.http.client.methods.HttpGet;
  10 +import org.apache.http.impl.client.CloseableHttpClient;
  11 +import org.apache.http.impl.client.HttpClients;
  12 +import org.apache.http.util.EntityUtils;
  13 +import org.slf4j.Logger;
  14 +import org.slf4j.LoggerFactory;
  15 +
  16 +import java.io.IOException;
  17 +import java.util.List;
  18 +
  19 +/**
  20 + * Created by panzhao on 2017/3/15.
  21 + */
  22 +public class ControlHttpUtils {
  23 +
  24 + static Logger logger = LoggerFactory.getLogger(ControlHttpUtils.class);
  25 +
  26 + static String url;
  27 +
  28 + static String secretKey;
  29 +
  30 + static {
  31 + url = ConfigUtil.get("http.control.service_data_url");
  32 + secretKey = ConfigUtil.get("http.control.secret.key");
  33 + }
  34 +
  35 + /**
  36 + * 从调度系统获取班次信息
  37 + *
  38 + * @return
  39 + */
  40 + public static List<ScheduleRealInfo> getCurrentDayPlan(String companyId, String workId) throws IOException {
  41 +
  42 + CloseableHttpClient httpClient = null;
  43 + List<ScheduleRealInfo> rs = null;
  44 + httpClient = HttpClientUtils.defaultHttpClient(url);
  45 + //超时时间
  46 + RequestConfig requestConfig = RequestConfig.custom()
  47 + .setConnectTimeout(2000).setConnectionRequestTimeout(1000)
  48 + .setSocketTimeout(3000).build();
  49 +
  50 + HttpGet get = new HttpGet(url + "/getCurrentDayPlan?companyId=" + companyId + "&workId=" + workId + "&secretKey=" + secretKey);
  51 + get.setConfig(requestConfig);
  52 +
  53 + CloseableHttpResponse response = httpClient.execute(get);
  54 +
  55 + rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class);
  56 + return rs;
  57 + }
  58 +
  59 + /**
  60 + * 从调度系统获取进出场班次信息
  61 + * @param companyId
  62 + * @param jcOrCc
  63 + * @return
  64 + */
  65 + public static List<ScheduleRealInfo> returnJCCInfo(String companyId, String jcOrCc) throws IOException{
  66 +
  67 + String method = jcOrCc.equals("out")?"/returnCCInfo":"/returnJCInfo";
  68 +
  69 + CloseableHttpClient httpClient = null;
  70 + List<ScheduleRealInfo> rs = null;
  71 + httpClient = HttpClientUtils.defaultHttpClient(url);
  72 + //超时时间
  73 + RequestConfig requestConfig = RequestConfig.custom()
  74 + .setConnectTimeout(2000).setConnectionRequestTimeout(1000)
  75 + .setSocketTimeout(3000).build();
  76 +
  77 + HttpGet get = new HttpGet(url + method+"?companyId=" + companyId + "&secretKey=" + secretKey);
  78 + get.setConfig(requestConfig);
  79 +
  80 + CloseableHttpResponse response = httpClient.execute(get);
  81 +
  82 + rs = JSONArray.parseArray(EntityUtils.toString(response.getEntity()), ScheduleRealInfo.class);
  83 + return rs;
  84 + }
  85 +}
... ...
src/main/java/com/bsth/util/HttpClientUtils.java
1   -package com.bsth.util;
2   -
3   -import org.apache.http.HttpEntity;
4   -import org.apache.http.client.config.RequestConfig;
5   -import org.apache.http.client.methods.CloseableHttpResponse;
6   -import org.apache.http.client.methods.HttpGet;
7   -import org.apache.http.client.methods.HttpPost;
8   -import org.apache.http.entity.StringEntity;
9   -import org.apache.http.impl.client.CloseableHttpClient;
10   -import org.apache.http.impl.client.HttpClients;
11   -import org.slf4j.Logger;
12   -import org.slf4j.LoggerFactory;
13   -
14   -import java.io.BufferedReader;
15   -import java.io.IOException;
16   -import java.io.InputStreamReader;
17   -
18   -/**
19   - * Created by panzhao on 2017/8/2.
20   - */
21   -public class HttpClientUtils {
22   -
23   - static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
24   -
25   - public static StringBuilder get(String url) throws Exception {
26   - CloseableHttpClient httpClient = null;
27   - CloseableHttpResponse response = null;
28   - StringBuilder stringBuffer = null;
29   - try {
30   - httpClient = HttpClients.createDefault();
31   - HttpGet get = new HttpGet(url);
32   - //超时时间
33   - RequestConfig requestConfig = RequestConfig.custom()
34   - .setConnectTimeout(6500).setConnectionRequestTimeout(6000)
35   - .setSocketTimeout(6500).build();
36   - get.setConfig(requestConfig);
37   - get.addHeader("Content-Encoding", "gzip");
38   -
39   - response = httpClient.execute(get);
40   -
41   - int statusCode = response.getStatusLine().getStatusCode();
42   - if(statusCode != 200){
43   - get.abort();
44   - System.out.println("http client status code: " + statusCode);
45   - return null;
46   - }
47   -
48   - stringBuffer = getResult(response.getEntity());
49   - } catch (Exception e) {
50   - logger.error("", e);
51   - } finally {
52   - if (null != httpClient)
53   - httpClient.close();
54   - if (null != response)
55   - response.close();
56   - }
57   - return stringBuffer;
58   - }
59   -
60   - /**
61   - * raw post data
62   - * @param url
63   - * @param data
64   - * @return
65   - */
66   - public static StringBuilder post(String url, String data) throws Exception {
67   - CloseableHttpClient httpClient = null;
68   - CloseableHttpResponse response = null;
69   - StringBuilder stringBuffer = null;
70   - try {
71   - httpClient = HttpClients.createDefault();
72   - HttpPost post = new HttpPost(url);
73   -
74   - post.setHeader("Accept", "application/json");
75   - post.setHeader("Content-Type", "application/json");
76   - //超时时间
77   - RequestConfig requestConfig = RequestConfig.custom()
78   - .setConnectTimeout(3500).setConnectionRequestTimeout(2000)
79   - .setSocketTimeout(3500).build();
80   - post.setConfig(requestConfig);
81   - post.setEntity((new StringEntity(data, "UTF-8")));
82   -
83   - response = httpClient.execute(post);
84   -
85   - int statusCode = response.getStatusLine().getStatusCode();
86   - if(statusCode != 200){
87   - post.abort();
88   - System.out.println("http client status code: " + statusCode);
89   - return null;
90   - }
91   -
92   - stringBuffer = getResult(response.getEntity());
93   - } catch (Exception e) {
94   - logger.error("", e);
95   - } finally {
96   - if (null != httpClient)
97   - httpClient.close();
98   - if (null != response)
99   - response.close();
100   - }
101   - return stringBuffer;
102   - }
103   -
104   - private static StringBuilder getResult(HttpEntity entity) throws IOException {
105   - StringBuilder stringBuffer = null;
106   - if (null != entity) {
107   - BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
108   - stringBuffer = new StringBuilder();
109   - String str = "";
110   - while ((str = br.readLine()) != null)
111   - stringBuffer.append(str);
112   - }
113   - return stringBuffer;
114   - }
115   -}
  1 +package com.bsth.util;
  2 +
  3 +import org.apache.http.HttpEntity;
  4 +import org.apache.http.client.config.RequestConfig;
  5 +import org.apache.http.client.methods.CloseableHttpResponse;
  6 +import org.apache.http.client.methods.HttpGet;
  7 +import org.apache.http.client.methods.HttpPost;
  8 +import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  9 +import org.apache.http.entity.StringEntity;
  10 +import org.apache.http.impl.client.CloseableHttpClient;
  11 +import org.apache.http.impl.client.HttpClients;
  12 +import org.slf4j.Logger;
  13 +import org.slf4j.LoggerFactory;
  14 +
  15 +import javax.net.ssl.*;
  16 +import java.io.BufferedReader;
  17 +import java.io.IOException;
  18 +import java.io.InputStreamReader;
  19 +import java.security.cert.CertificateException;
  20 +import java.security.cert.X509Certificate;
  21 +
  22 +/**
  23 + * Created by panzhao on 2017/8/2.
  24 + */
  25 +public class HttpClientUtils {
  26 +
  27 + static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
  28 +
  29 + private static String HTTPS = "https://";
  30 +
  31 + private static SSLConnectionSocketFactory sslConnectionSocketFactory;
  32 +
  33 + static {
  34 + try {
  35 + SSLContext sslCtx = SSLContext.getInstance("TLSv1.2");
  36 + sslCtx.init(null, new TrustManager[] {
  37 + new X509TrustManager() {
  38 + @Override
  39 + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  40 + }
  41 +
  42 + @Override
  43 + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  44 + }
  45 +
  46 + @Override
  47 + public X509Certificate[] getAcceptedIssuers() {
  48 + return null;
  49 + }
  50 + }
  51 + }, null);
  52 + sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslCtx, new HostnameVerifier() {
  53 +
  54 + @Override
  55 + public boolean verify(String hostname, SSLSession session) {
  56 + // TODO Auto-generated method stub
  57 + return true;
  58 + }
  59 + });
  60 + } catch (Exception e) {
  61 + logger.error("SSL context set fail: {}", e);
  62 + }
  63 + }
  64 +
  65 + public static CloseableHttpClient defaultHttpClient(String url) {
  66 + if (url.startsWith(HTTPS)) {
  67 + return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
  68 + }
  69 + return HttpClients.createDefault();
  70 + }
  71 +
  72 + public static StringBuilder get(String url) throws Exception {
  73 + CloseableHttpClient httpClient = null;
  74 + CloseableHttpResponse response = null;
  75 + StringBuilder stringBuffer = null;
  76 + try {
  77 + httpClient = defaultHttpClient(url);
  78 + HttpGet get = new HttpGet(url);
  79 + //超时时间
  80 + RequestConfig requestConfig = RequestConfig.custom()
  81 + .setConnectTimeout(6500).setConnectionRequestTimeout(6000)
  82 + .setSocketTimeout(6500).build();
  83 + get.setConfig(requestConfig);
  84 + get.addHeader("Content-Encoding", "gzip");
  85 +
  86 + response = httpClient.execute(get);
  87 +
  88 + int statusCode = response.getStatusLine().getStatusCode();
  89 + if(statusCode != 200){
  90 + get.abort();
  91 + System.out.println("http client status code: " + statusCode);
  92 + return null;
  93 + }
  94 +
  95 + stringBuffer = getResult(response.getEntity());
  96 + } catch (Exception e) {
  97 + logger.error("", e);
  98 + } finally {
  99 + if (null != httpClient)
  100 + httpClient.close();
  101 + if (null != response)
  102 + response.close();
  103 + }
  104 + return stringBuffer;
  105 + }
  106 +
  107 + /**
  108 + * raw post data
  109 + * @param url
  110 + * @param data
  111 + * @return
  112 + */
  113 + public static StringBuilder post(String url, String data) throws Exception {
  114 + CloseableHttpClient httpClient = null;
  115 + CloseableHttpResponse response = null;
  116 + StringBuilder stringBuffer = null;
  117 + try {
  118 + httpClient = defaultHttpClient(url);
  119 + HttpPost post = new HttpPost(url);
  120 +
  121 + post.setHeader("Accept", "application/json");
  122 + post.setHeader("Content-Type", "application/json");
  123 + //超时时间
  124 + RequestConfig requestConfig = RequestConfig.custom()
  125 + .setConnectTimeout(3500).setConnectionRequestTimeout(2000)
  126 + .setSocketTimeout(3500).build();
  127 + post.setConfig(requestConfig);
  128 + post.setEntity((new StringEntity(data, "UTF-8")));
  129 +
  130 + response = httpClient.execute(post);
  131 +
  132 + int statusCode = response.getStatusLine().getStatusCode();
  133 + if(statusCode != 200){
  134 + post.abort();
  135 + System.out.println("http client status code: " + statusCode);
  136 + return null;
  137 + }
  138 +
  139 + stringBuffer = getResult(response.getEntity());
  140 + } catch (Exception e) {
  141 + logger.error("", e);
  142 + } finally {
  143 + if (null != httpClient)
  144 + httpClient.close();
  145 + if (null != response)
  146 + response.close();
  147 + }
  148 + return stringBuffer;
  149 + }
  150 +
  151 + private static StringBuilder getResult(HttpEntity entity) throws IOException {
  152 + StringBuilder stringBuffer = null;
  153 + if (null != entity) {
  154 + BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
  155 + stringBuffer = new StringBuilder();
  156 + String str = "";
  157 + while ((str = br.readLine()) != null)
  158 + stringBuffer.append(str);
  159 + }
  160 + return stringBuffer;
  161 + }
  162 +}
... ...
src/main/resources/application-cloud.properties 0 → 100644
  1 +server.port=9089
  2 +management.port= 9001
  3 +management.address= 127.0.0.1
  4 +
  5 +spring.jpa.hibernate.ddl-auto= none
  6 +spring.jpa.hibernate.naming_strategy= org.hibernate.cfg.ImprovedNamingStrategy
  7 +#DATABASE
  8 +spring.jpa.database= MYSQL
  9 +spring.jpa.show-sql= false
  10 +spring.datasource.driver-class-name= com.mysql.jdbc.Driver
  11 +spring.datasource.url= jdbc:mysql://192.170.100.132/control?useUnicode=true&characterEncoding=utf-8&useSSL=false
  12 +spring.datasource.username= root
  13 +spring.datasource.password= root2jsp
  14 +#DATASOURCE
  15 +spring.datasource.max-active=100
  16 +spring.datasource.max-idle=8
  17 +spring.datasource.min-idle=8
  18 +spring.datasource.initial-size=3
  19 +
  20 +spring.datasource.test-on-borrow=true
  21 +spring.datasource.test-on-connect=true
  22 +spring.datasource.test-on-return=true
  23 +spring.datasource.test-while-idle=true
  24 +spring.datasource.validation-query=select 1
  25 +
  26 +#REDIS
  27 +spring.redis.database=0
  28 +spring.redis.host=127.0.0.1
  29 +spring.redis.password=bsth_control_001
  30 +spring.redis.port=28008
  31 +
  32 +#kafka
  33 +spring.kafka.bootstrap-servers=112.64.45.145:19093,112.64.45.145:19094,112.64.45.145:19095,112.64.45.145:19096
  34 +spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
  35 +spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
  36 +spring.kafka.producer.buffer-memory=33554432
  37 +spring.kafka.producer.acks=all
  38 +
  39 +http.control.service_data_url= https://192.170.100.54:9088/companyService
  40 +http.control.secret.key= dVPHJkWUt5FhMT7jrM2dLV7QvlHAmZFd42rs1P0usBx8A7HZki
  41 +
  42 +http.gps.real.url= http://192.170.100.252:8080/transport_server/rtgps/
0 43 \ No newline at end of file
... ...