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注釋


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM