Browse Source

修改bug

chenweida 6 năm trước cách đây
mục cha
commit
385d1fd0cc

+ 1 - 1
admin/admin-client-starter/src/main/java/com/yihu/admin/client/advice/HttpAdvice.java

@ -45,7 +45,7 @@ public class HttpAdvice {
    @Value("${spring.application.name}")
    private String spanrName;
    private Logger logger = LoggerFactory.getLogger(SQLTimeAdvice.class);
    private Logger logger = LoggerFactory.getLogger(HttpAdvice.class);
    private final String eventName = "sqlMonitor;";

+ 32 - 5
common-data-es-starter/src/main/java/com/yihu/base/es/config/ElasticFactory.java

@ -7,13 +7,19 @@ import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
@ -24,10 +30,6 @@ import java.util.concurrent.TimeUnit;
@Configuration
public class ElasticFactory {
    private static JestClientFactory factory = null;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private ElasticSearchPorperties elasticSearchPorperties;
//-----------------------------------jestClient----------------------------------------
@ -68,15 +70,40 @@ public class ElasticFactory {
    //-----------------------------------TransportClient----------------------------------------
    private TransportClient transportClient;
    public Client getTransportClient() {
        try {
            return elasticsearchTemplate.getClient();
            initTranClient();
            return transportClient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 9300
     *
     * @throws UnknownHostException
     */
    private synchronized void initTranClient() throws UnknownHostException {
        if (transportClient == null) {
            String[] hosts = elasticSearchPorperties.getClusterNodes().split(",");
            Settings settings = Settings.settingsBuilder()
                     .put("client.transport.sniff", elasticSearchPorperties.getJestDiscoveryEnabled())//开启嗅探功能
                    .put("cluster.name", StringUtils.isEmpty(elasticSearchPorperties.getClusterName()) ? "jkzl" : elasticSearchPorperties.getClusterName())//默认集群名字是jkzl
                    .build();
            transportClient = TransportClient.builder().settings(settings).build();
            for (String oneHost : hosts) {
                String[] hostAndport = oneHost.split(":");
                transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndport[0]), Integer.valueOf(hostAndport[1])));
            }
        }
    }
    /**
     * es连接池
     *

+ 10 - 0
common-data-es-starter/src/main/java/com/yihu/base/es/config/properties/ElasticSearchPorperties.java

@ -8,6 +8,8 @@ import org.springframework.stereotype.Component;
 */
@Component
public class ElasticSearchPorperties {
    @Value("${spring.data.elasticsearch.cluster-name:jkzl}")
    private String clusterName;//http://172.19.103.68:9200  #多个逗号分割
    @Value("${spring.data.elasticsearch.cluster-nodes:http://172.19.103.68:9300}")
    private String clusterNodes;//http://172.19.103.68:9200  #多个逗号分割
@ -96,4 +98,12 @@ public class ElasticSearchPorperties {
    public void setClusterNodes(String clusterNodes) {
        this.clusterNodes = clusterNodes;
    }
    public String getClusterName() {
        return clusterName;
    }
    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }
}