spring cloud踩坑指南

版本信息:

spring cloud 版本Greenwich.SR2
spring boot 版本2.1.8.RELEASE

gateway报错 DefaultDataBuffer cannot be cast to org.springframework.core.io.buffer.NettyDataBuffer###

解决方式:
springcloud的gateway使用的是webflux,默认使用netty,所以从依赖中排除 tomcat相关的依赖 ,就可以了。
我的问题:
排除了依赖还是报错。后来发现使用了 tomcat-embed-core 包导致的,删除再引文需要的jar包即可。

starter-openfeign starter-feign 区别(来源)###

Feign
Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端
Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。
Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务
Feign本身不支持Spring MVC的注解,它有一套自己的注解

OpenFeign
OpenFeign是Spring Cloud 在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。
OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,
并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

启动读取外部配置文件###

springboot 有读取外部配置文件的方法,如下优先级:
第一种是在jar包的同一目录下建一个config文件夹,然后把配置文件放到这个文件夹下。
第二种是直接把配置文件放到jar包的同级目录。
第三种在classpath下建一个config文件夹,然后把配置文件放进去。
第四种是在classpath下直接放配置文件。

我的问题:
根据spring.profiles.active 建立正确的文件名。 目录按照上面规则 但是文件名一定是application-{spring.profiles.active}.yml

eureka相关配置###

指定 网页显示为 ip + 端口
解决 spring cloud feign docker上无法通讯的问题 (设置在 服务端和客户端)

eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

注:
1.instance-id: ${spring.cloud.client.ip-address} 不同版本好像写法不一样 请检查
2.需要引入 spring-cloud-commons 包

指定 服务启动后5秒钟注册 (设置在 客户端)

eureka:
  client:
	initial-instance-info-replication-interval-seconds: 10
	registry-fetch-interval-seconds: 5

指定监控信息 打开shutdown,info,health三个

management:
  endpoints:
    web:
      exposure:
        include:
          [shutdown,info,health]
  endpoint:
    shutdown:
      enabled: true

注:
1.需要引入spring-boot-starter-actuator 包
2. shutdown 访问为post方式,info和health为get方式

指定集群

客户端

client:
    registerWithEureka: true
    serviceUrl:
      defaultZone: {其他的eureka服务的链接}

服务端

eureka:
  client:
    serviceUrl:
      defaultZone: {所有的eureka服务的链接}

redis偶发 超时问题

报错内容:

org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 500 millisecond(s)

原因及解决方案:
spring-data-redis内置了两款驱动,jedis和lettuce。springboot1.X版本默认jedis实现,springboot2.X默认lettuce实现。
lettuce:基于netty实现,线程安全,但默认只有一个实例。
jedis:直连redis服务端,一般配合连接池使用,可以增加物理连接。

换成jedis

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

报错java.nio.file.InvalidPathException: Illegal char <:> at index##

报错原因:日志打印用了spring-boot-starter里面自带的logback组件
解决方案:排除掉即可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <!-- 排除自带的logback依赖 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Could not find acceptable representation

目前定义的MessageConverter是fastJsonHttpMessageConverter

public class WebMvcConfg extends WebMvcConfigurationSupport {
	/******/
	@Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter()
    {
        //1.需要定义一个convert转换消息的对象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue);
        //设置日期格式
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //3处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastMediaTypes.add(MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json"));
        //4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return fastJsonHttpMessageConverter;
        // HttpMessageConverter<?> converter =
       // return new HttpMessageConverters(converter);
    }

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {

       converters.add(fastJsonHttpMessageConverter());
        //addDefaultHttpMessageConverters(converters);
    }
}

接口会添加@ResponseBody注释,一般访问正常,但是会出现以下情况:
1.文件导出会报错
2.自带监控/actuator/info 访问时报错

原因:造Spring无法找到合适的视图解析器 。
解决问题:
问题1
之前指定了唯一的MediaType 为json ,
而actuator 需要的mediaType是 application/vnd.spring-boot.actuator.v2+json
所以添加mediaType即可

List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastMediaTypes.add(MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json"));
//4.在convert中添加配置信息.
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

问题2
导出接口不要添加@ResponseBody注释

posted @ 2019-11-11 12:05  CalronLoveRonnie  阅读(2337)  评论(0编辑  收藏  举报
AmazingCounters.com