package abacus.springboot.example;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import abacus.commons.responsebody.wrapper.ResultResponseBodyWrapper;

@SpringBootApplication
@EnableDiscoveryClient
// @EnableHystrix
// 配置扫描
@ComponentScan(basePackages = { "abacus.springboot.example", "abacus.springboot.example.*" })
// 指定加载的repository 否则不能加载外部的repository
@EnableJpaRepositories(basePackages = { "abacus.springboot.example.*", })
// 指定加载的外部entity 否则不能加载外部的entity
@EntityScan(basePackages = { "abacus.springboot.example.*", "com.abacus.pms.*", "com.abacus.xpos.foundation.dao" })
//指定加载外部Feign
@EnableFeignClients("abacus.springboot.example.feign")
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	} 

	@Bean
	public ResultResponseBodyWrapper getResultResponseBodyWrapper() {
		return new ResultResponseBodyWrapper();
	}

	@Bean
	public RestTemplate getRestTemplate() {

		RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();

		HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

		ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

		// SimpleClientHttpRequestFactory re = new
		// SimpleClientHttpRequestFactory();
		// re.setConnectTimeout(5000);
		// re.setReadTimeout(5000);
		return new RestTemplate(requestFactory);
		
	}
}
