一、在SpringCloudServer中配置了
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
一定在SpringCloudClient客户端也要配置,否则会找不到spring cloud confg的配置
spring:
cloud:
config:
uri: http://localhost:8088
二、spring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法
在spring boot1.x下,使用注解@EnableFeignClients,jar包依赖是:
<!-- feign远程调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
但是 在spring boot2.x下,
Spring Cloud对Feign的支持由org.springframework.cloud:spring-cloud-netflix-core 移到 org.springframework.cloud:spring-cloud-openfeign-core下,
而Finchley.M9版本下的spring-cloud-starter-openfeign:2.0.0.M2的pom依赖文件中导入的是spring-cloud-netflix-core而非spring-cloud-openfeign-core,
需要我们在pom文件中添加对应依赖管理使spring-cloud-starter-openfeign版本更新到
我这里使用的spring boot 2.0.3版本,所以解决方案是:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
三、mysql驱动变更:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
四、@EnableDiscoveryClient和@EnableFeignClients的区别
@EnableDiscoveryClient的作用是被发现
@EnableFeignClients的作用包含@EnableDiscoveryClient:被发现和发现别的客户端
五、解决SpringBoot默认转换器无法将POJO对象装换为fastJSON的问题,2种方案:
error:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer
@ConditionalOnClass(value = {RestTemplate.class, HttpClient.class})
1、局部解决方案(不推荐)
在每个POJO类(或属性)上添加@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"})
2、整体解决方案(推荐)
@Configuration
public class RestTemplateConfiguration {
@Value("${remote.maxTotalConnect:0}")
private int maxTotalConnect; //连接池的最大连接数默认为0
@Value("${remote.maxConnectPerRoute:200}")
private int maxConnectPerRoute; //单个主机的最大连接数
@Value("${remote.connectTimeout:2000}")
private int connectTimeout; //连接超时默认2s
@Value("${remote.readTimeout:30000}")
private int readTimeout; //读取超时默认30s
//创建HTTP客户端工厂
private ClientHttpRequestFactory createFactory() {
if (this.maxTotalConnect <= 0) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(this.connectTimeout);
factory.setReadTimeout(this.readTimeout);
return factory;
}
HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(this.maxTotalConnect).setMaxConnPerRoute(this.maxConnectPerRoute).build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 连接超时
factory.setConnectTimeout(this.connectTimeout);
// 数据读取超时时间,即SocketTimeout
factory.setReadTimeout(this.readTimeout);
// 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
factory.setConnectionRequestTimeout(200);
// 缓冲请求数据,默认值是true。通过POST或者PUT大量发送数据时,建议将此属性更改为false,以免耗尽内存。
factory.setBufferRequestBody(false);
return factory;
}
//初始化RestTemplate,并加入spring的Bean工厂,由spring统一管理
@Bean
@ConditionalOnBean(RestTemplate.class)
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate(this.createFactory());
// List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
//
// //重新设置StringHttpMessageConverter字符集为UTF-8,解决中文乱码问题
// HttpMessageConverter<?> converterTarget = null;
// for (HttpMessageConverter<?> item : converterList) {
// if (StringHttpMessageConverter.class == item.getClass()) {
// converterTarget = item;
// break;
// }
// }
// if (null != converterTarget) {
// converterList.remove(converterTarget);
// }
// converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
//
// //加入FastJson转换器
// converterList.add(new FastJsonHttpMessageConverter4());
// 添加内容转换器
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate = new RestTemplate(messageConverters);
return restTemplate;
}
}