前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud开发注意事项

Spring Cloud开发注意事项

作者头像
lyb-geek
发布2018-09-27 09:39:01
1.5K0
发布2018-09-27 09:39:01
举报
文章被收录于专栏:Linyb极客之路

1、消费者和服务提供者(接口和实现)

service 接口上添加注解@FeignClient( name = "${feign.provider.portal}" ) feign.provider.portal 需要在application.yml中配置 portal provider 的application.name service实现类上需添加注解 @RestController , *Mapper.java 接口上需要添加 @Mapper

2、接口中的方法定义 需添加注解

@RequestMapping(value = "/${path}",method = RequestMethod.POST) , ${path}为请求路径,可以以方法名定义,method 如不确定请使用 POST,如果对象参数必须使用 POST,并且 参数前必须使用 @RequestBody 注解如果参数为基本数据类型或者String,可以使用GET方式,参数前需加@RequestParam注解,注解的value不能为空

@RequestMapping(value = "/save" , method = RequestMethod.POST) public int save(@RequestBody User user);

@RequestMapping(value = "/auditStaff",method = RequestMethod.POST) public void auditStaff(@RequestParam("staffId") String staffId, @RequestParam("deptId") String deptId);

  • 接口中使用的@RequestParam("value") ,在接口实现类中的参数名必须和注解中的value一致: 例如 接口方法 User find(@RequestParam("userId") String id) , 实现类方法 User find(String userId) ,如果不一致参数将为null。实际就是springMvc中的参数将RestApi中的请求参数绑定到方法的参数上
  • 如果接口中定义的参数有对象类型,需要在参数前添加 @RequestBody 注解,一个接口参数中仅能有一个@RequestBody 注解, 并且需要在 实现方法中 参数也添加 @RequestBody注解
  • 如果接口中参数未添加@RequestParam(@RequestHeader、@RequestBody),那么FeignClient默认使用POST发送请求,如果接口定义中定义的是method = RequestMethod.GET ,那么将报错,方法不匹配

3、跨feign client调用

如果provider中需要引入其他feign client的接口,需在 provider的启动类添加注解 @EnableFeignClients(basePackages = {"com.complay.biz.service"}) ,basePackages 为其他模块接口的包名

例如: 在portal provider中需要应用 mdata-service 中的接口,需要扫描 接口中的FeignClient 注解 使 mdata-service接口注入到接口,同时需要在application.yml 中配置依赖接口的feignClient 的name

4、Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题

Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了

解决方案有三种,以feign为例。

方法一hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000该配置是让Hystrix的超时时间改为5秒 方法二hystrix.command.default.execution.timeout.enabled: false该配置,用于禁用Hystrix的超时时间 方法三feign.hystrix.enabled: false该配置,用于索性禁用feign的hystrix。该做法除非一些特殊场景,不推荐使用。

5、http请求直接跳视图view:

代码语言:javascript
复制
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/reserve/rsvquery/all").setViewName("/reserve/rsvquery/all");
registry.addViewController("/reserve/member/index").setViewName("/reserve/member/index");
}
}

6、debug关闭重试和超时:

feign.hystrix.enabled: false

7、config多级目录配置:

服务端svn 路径上配置一级目录: spring.cloud.config.server.svn.uri=svn://ip:port/repos/app-config/${server.code} 客户端将二级目录作为label配置 spring.cloud.config.label=portal

8、FeignClient Date类型参数有时差:

原因是Feign 客户端的将Date对象转为String,然后服务端接收的String转换为Date对象时采用的默认构造器的方式,new Date('Sat Sep 02 .....')

解决方法:1、当发送时间类型时,直接用String发送

2、Feign客户端实现FeignFormatterRegistrar接口自定义DateFormatRegister

代码语言:javascript
复制
@Component
public class DateFormatRegister implements FeignFormatterRegistrar{
public DateFormatRegister(){

}

@Override
public void registerFormatters(FormatterRegistry registry) {
registry.addConverter(Date.class, String.class, new Date2StringConverter()); 
}
private class Date2StringConverter implements Converter<Date,String>{
@Override
public String convert(Date source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(source);
}

}
}

provider增加相应的解析器

代码语言:javascript
复制
@Configuration
public class WebConfigBeans {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
/**
* 增加字符串转日期的功能
*/
@PostConstruct
public void initEditableValidation() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
.getWebBindingInitializer();
if (initializer.getConversionService() != null) {
GenericConversionService genericConversionService = (GenericConversionService) initializer
.getConversionService();
genericConversionService.addConverter(String.class, Date.class, new String2DateConverter());
}
}
}

9、Feign Rest请求参数过长 (角色选权限的时候,ID太多太长):

配置文件添加配置:server.maxHttpHeaderSize: 1024000 #单位byte,最大请求头数据大小,默认是8k

Properties类为ServerProperties,tomcat相关参数都在里面

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-08-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档