学习Spring Cloud问题集

4 篇文章 0 订阅
1 篇文章 0 订阅

一. Feign 

1、问题:

使用Feign时course-price项目导入其他项目(course-list)的实体类时,在feign客户端那个类上没有问题

        在course-price项目的Controller上出现找不到类的问题

 解决:

在course-list项目的pom.xml加上,

<configuration>
           <classifier>exec</classifier>
</configuration>

更改后的

 解决途径:(花费一个小时)一个一个问题 去找才问题真正所在是这个:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile 

 参考了https://blog.csdn.net/l13880647015/article/details/106999920?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162740447116780366586616%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=162740447116780366586616&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-106999920.first_rank_v2_pc_rank_v29&utm_term=+Failed+to+execute+goal+org.apache.maven.plugins%3Amaven-compiler-plugin%3A3.8.1&spm=1018.2226.3001.4187  的回答

问题2,

使用feign进行远程调用时,带上请求头信息 运行时报错:

java.io.IOException: too many bytes written

查找资料后 :因为服务之间调用需要携带一些用户信息之类的 所以实现了Feign的RequestInterceptor拦截器复制请求头,复制的时候是所有头都复制的,可能导致Content-length长度跟body不一致. 所以只需要判断如果是Content-length就跳过

解决途径是:是加一个”跳过 content-length“

while (headerNames.hasMoreElements()){
                //获取下一个元素
                String name = headerNames.nextElement();
                //获取请求头的 键对应的 值数组
                Enumeration<String> values = request.getHeaders(name);

                // 跳过 content-length
                if (name.equals("content-length")){
                    continue;
                }
                while (values.hasMoreElements()){
                    String value = values.nextElement();
                    requestTemplate.header(name,value);
                }
            }

 之后运行又报 status 405 reading ProductFeignClient#detailForFeign(Integer)

根据错误信息找到detailForFeign方法后,为参数加上 @RequestParam即可

结果:重新把 ”跳过 content-length“删除后并不会报 too many bytes written错误

待解决 ???

二、Eurska

关闭注册自身没有问题,开启注册注册报错:

Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}

解决:

服务提供者配置文件中defaultZone属性识别不到,

不将自己也注册的话就可以按照找:

eureka.client.service-url.xx

此时的xx是随意写都可以的,然而将自身也注册的话,必须让

eureka.client.service-url.defaultZone=项目的ip和端口

3.zuul

报错:启动时:

 Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthIndicatorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicatorRegistry]: Factory method 'healthIndicatorRegistry' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbHealthIndicator' defined in class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicator]: Factory method 'dbHealthIndicator' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

不知所云,继续找,

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

推测:因为远程调用feign使用的方法是需要使用到数据库的信息,使用在zuul的配置里也需要配置数据库。

解决:在zuul的配置文件里加MySQL数据库配置和mybtis的配置

spring.datasource.name=imooc_mall_datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/imooc_mall?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&userSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
#读取mapper的映射文件
mybatis.mapper-locations=classpath:mappers/*.xml

问题二:在user模块里可以过滤管理员,而在商品模块里过滤是报服务器过滤失败

o.s.c.n.z.filters.post.SendErrorFilter   : Error during filtering

com.netflix.zuul.exception.ZuulException: Filter threw Exception

Caused by: feign.FeignException$InternalServerError: status 500 reading UserFeignClient#checkAdminRole(User)

详细分析后,发现user模块里并没有使用到管理员的过滤。查看源码后发现

 注解是请求参数的,而方法的参数是一个对象,应该使用@RequestBody

再修改

小结:

  • 当使用参数是  局部类型或字符串  时使用@RequestParam
  • 使用参数是  对象  时使用@RequestBody

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值