SpringCloud学习笔记

一、在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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值