博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微服务springcloud(H版本)与springboot(2.X版本) maven常见问题整理记录
阅读量:2134 次
发布时间:2019-04-30

本文共 5821 字,大约阅读时间需要 19 分钟。

微服务springcloud与springboot maven常见问题整理记录

1. 组件问题

(1) 使用nacos组件注册服务时,可不用指定@EnableDiscoveryClient注解(2) 使用openfeign封装外部接口时,只用在fallback类指定@EnableFeignClients说明即可注入

2. 扫描问题

(1) @SpringBootApplication( exclude = {BaseException.class})//专门用来排除auto-configuration的类(2) @ComponentScan(/* 指定扫描的基包路径*/basePackages = "com.XXX.cloud", /* 用于排除注入的定义的BEAN类 */excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = BaseException.class))

3. 多模块maven问题

(1) 从spring-boot-starter-parent扩展的模块或dependency中有spring-boot-starter-web,则是web项目,应有启动的main方法或@SpringBootApplication启动类(2) 引用某个模块的方式,从
pom
类型的maven模块扩展或dependency中增加类型
jar
的模块默认的packaging类型是jar(3) 打包问题: 指令: compile 编译 install 安装本地 deploy 安装本地并推远程仓库 package 打包在maven多模块module组织的项目中,打普通的jar工具包,可使用默认的打包配置(不设置
),打包可执行的web项目时使用spring-boot-maven-plugin工具(外部引用不了可执行的web项目jar包中的方法或类)

说明:

4. 异常问题

(1) 网关gateway异常    
增加
org.springframework.cloud
spring-cloud-starter-loadbalancer
(2) 组件nacos异常: [NACOS HTTP-POST] The maximum number of tolerable server reconnection errors 修改: application.yml -> bootstrap.yml(3) 启动指令nohup java -jar XXX.jar & 系统内存不足时启动jar包提示内存不足异常,可修改-Xms500M -Xmx500M参数(4) 异常Feign调用报错:No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessageConvert 修改注入bean HttpMessageConverters @Bean @ConditionalOnMissingBean public HttpMessageConverters httpMessageConverters(ObjectProvider
> converters) { return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList())); }(5)
org.springframework.cloud
spring-cloud-starter-loadbalancer

5. 关于SPRINGBOOT事务注解

@Transactional(rollbackFor = Exception.class)//多个函数调用只用外层使用事务注解,在内层函数的事务注解只控制内层以及内部调用的部分@Transactional(rollbackFor = Exception.class)private int fun1() throws Exception{	fun2();}//@Transactional(rollbackFor = Exception.class)private int fun2() throws Exception{}

6. 关于OPEN-FEIGN调用传header参数

/** * Feign调用,携带header */@Configurationpublic class FeignConfig implements RequestInterceptor {    @Override    public void apply(RequestTemplate template) {        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();        if (requestAttributes == null) {return; }        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();        Enumeration
headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); Enumeration
values = request.getHeaders(name); while (values.hasMoreElements()) { String value = values.nextElement(); template.header(name, value); } } } }}

7. 关于MP-PLUS(版本3.4.2)字段填充

@Slf4j@Componentpublic class MyMetaObjectHandler implements MetaObjectHandler {    @Override    public void insertFill(MetaObject metaObject) {        log.info("start insert fill ....");        //this.strictInsertFill(metaObject, "id", Long.class, IdUtil.getId());        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());        this.strictInsertFill(metaObject, "createBy", Long.class, UserUtil.getUserId());        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());        this.strictInsertFill(metaObject, "updateBy", Long.class, UserUtil.getUserId());        this.strictInsertFill(metaObject, "deleted", Integer.class, 0);        //this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)        // 或者        //this.strictUpdateFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)        // 或者        //this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)    }    @Override    public void updateFill(MetaObject metaObject) {        log.info("start update fill ....");        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());        this.strictInsertFill(metaObject, "updateBy", Long.class, UserUtil.getUserId());        //this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)        // 或者        //this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)        // 或者        //this.fillStrategy(metaObject, "updateTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)    }}

8. 传参与返回值时间格式转换

当前端传来的是键值对,用@DateTimeFormat 规定接收的时间格式。当前端传来json串,后台用@ReuqestBody接收,必须用@JsonFormat 规定接收的时间格式。@ReuqestBody 如果不使用@JsonFormat 接收空格会自动转换成 T 导致时间转化错误返回给前端的时间值,只能用@JsonFormat 规定返回格式 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")   private Date createTime;@DateTimeFormat 无法决定返回值的格式。@JsonFormat 是jackson提供。@DateTimeFormat 由spring提供。

9. 参数校验

(1)在controller控制层参数校验例public BaseResult
add(@RequestBody @Validated /*或@Valid*/ SysRequest sysRequest){}&& 在SysRequest中的参数使用JSR-303校验注解(2) 在service层校验有不同方式在ISysService或SysServiceImpl类的位置使用@Validated注解&&在ISysService方法内的参数中使用@Valid注解或同时在ISysService与SysServiceImpl类方法内的参数中使用@Valid注解&&在SysRequest中的参数使用JSR-303校验注解例@Validatedpublic class SysServiceImpl implements ISysService { }public interface ISysService{ int add(@Valid SysRequest sysRequest);}

(3)

转载地址:http://aqkgf.baihongyu.com/

你可能感兴趣的文章
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>
composer install或composer update 或 composer require phpoffice/phpexcel 失败解决办法
查看>>
TP5.1项目从windows的Apache服务迁移到linux的Nginx服务需要注意几点。
查看>>
win10安装软件 打开时报错 找不到 msvcp120.dll
查看>>
PHPunit+Xdebug代码覆盖率以及遇到的问题汇总
查看>>
PHPUnit安装及使用
查看>>
PHP项目用xhprof性能分析(安装及应用实例)
查看>>
composer安装YII
查看>>
Sublime text3快捷键演示
查看>>
sublime text3 快捷键修改
查看>>
关于PHP几点建议
查看>>
硬盘的接口、协议
查看>>
VLAN与子网划分区别
查看>>
Cisco Packet Tracer教程
查看>>
02. 交换机的基本配置和管理
查看>>
03. 交换机的Telnet远程登陆配置
查看>>
微信小程序-调用-腾讯视频-解决方案
查看>>
phpStudy安装yaf扩展
查看>>
密码 加密 加盐 常用操作记录
查看>>