一. 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
问题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