her~~llo,我是你们的好朋友Lyle,是名梦想成为计算机大佬的男人!

博客是为了记录自我的学习历程,加强记忆方便复习,如有不足之处还望多多包涵!非常欢迎大家的批评指正。

这两天在学做项目,之前也学了spring、springmvc、springboot。那么现在就可以用springboot来做项目了,但是学的依然都只是皮毛,很多整合我都没有头绪,问题也很多。那么现在我就分享一下我在敲敲敲的时候遇到的掉头发问题。

目录

问题一:网上各式各样的Maven坐标,到底需要导哪个?

问题二:Dubbo + MybatisPlus报错'java.lang.invode.SerializedLambda' culd not be instantiated

问题三:Failed to check the status of the service . No provider available for the service  from the url consumer

其他解决方案(网上搜索的,我就直接引用了)

问题四:mybatis的关联映射查询细节问题

问题细节说明:

问题五:mybatis的sql日志如何开启?

问题六:redis连接错误:ERR Client sent AUTH, but no password is set 

问题七:SpringBoot启动报错:LoggerFactory is not a Logback LoggerContext but Logback is on the classpath

问题八:log4j警告:WARN No appenders could be found for logger(org.apache.ibatis.logging.LogFactory). 

问题九:dubbo报错:org.apache.dubbo.rpc.RpcException: Fail to start server ...cause: Address already in use: bind

问题十:[DUBBO] Failed to create file store cache. Local file cache will be disabled. 


问题一:网上各式各样的Maven坐标,到底需要导哪个?

首先说说Dubbo的依赖。

这个问题真的是搞的我好头疼,终于成功解决。

我用的是Zookeeper注册中心,按照的是Apache官方给的maven坐标,可以参考下面的链接配置。

Annotation 配置 | Apache Dubbohttps://dubbo.apache.org/zh/docs3-v2/java-sdk/reference-manual/config/annotation/我跟官方里面的不一样,因为我导了dubbo-spring-boot-starter,代替了dubbo-bom。版本version的话可以查看Maven Repository: Search/Browse/Explore (mvnrepository.com)选择。

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>${dubbo.version}</version>
    <type>pom</type>
    </dependency>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>${dubbo.version}</version>
</dependency>

然后我们说一下Redis相关依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.7.2</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>${jedis.version}</version>
</dependency>

问题二:Dubbo + MybatisPlus报错'java.lang.invode.SerializedLambda' culd not be instantiated

我启动了两个dubbo服务,一个服务提供者provider和一个消费者consumer。

我在consumer的controller中这样写的(这段需要修改,不在这用IService的方法),

LambdaQueryWrapper<Manager> queryWrapper= new LambdaQueryWrapper<Manager>();
queryWrapper.eq(Manager::getPhone,phone);
managerService.selectOne(queryWrapper);

这个时候因为consumer用到了provider中的Service,而当前consumer和provider不在同一个服务,需要将wrapper序列化转至Service的服务,于是出现这个错误。

而网上查询得知:wrapper不支持也不推荐进行dubbo传递

所以,我们可以将这个写在一个服务里也就是provider,

改成在服务实现类impl中这样写,再在consumer中直接调用该Service的这个方法。

LambdaQueryWrapper<Manager> queryWrapper= new LambdaQueryWrapper<Manager>();
queryWrapper.eq(Manager::getPhone,phone);
managerDao.selectOne(queryWrapper);

问题就可以解决。


问题三:Failed to check the status of the service . No provider available for the service  from the url consumer

排除异常的话就要选择重要的信息进行观察,Failed to check the status of the service ,这里的意思就是这个Service接口创建失败,后面的异常信息非常重要:No provider available for the service ,意思就是对于service来说,没有可实现的provider,消费者在访问提供者的时候失败了。

我为什么会报这个错误呢,就是因为先启动了consumer的服务,服务提供者还没有,所以会失败,所以启动有个顺序,先启动provider再启动consumer。

其他解决方案(网上搜索的,我就直接引用了)

Dubbo默认(缺省)会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认check=true,如果你的Spring容器是懒加载的,或者通过API编程延迟引用服务,请关闭check,否则服务临时不可用时,会抛出异常,拿到null引用,如果check=false,总是会返回引用,当服务恢复时,能自动连上。


问题四:mybatis的关联映射查询细节问题

这是我在项目开发时写的一段关联映射查询。搞了一晚上,mybatis没有学的太深入,踩了很多坑,第一次用到,就总结一下。以后用的就方便了。

<mapper namespace="com.ylm.dao.DeliveryOrderDao">
    <resultMap type="com.ylm.pojo.DeliveryOrder" id="baseResultMap">
        <id column="id" property="id"/>
        <result column="userId" property="userId"/>
        <result column="time" property="time"/>
        <result column="productInfo" property="productInfo"/>
        <result column="status" property="status"/>
        <result column="money" property="money"/>
        <result column="updateTime" property="updateTime"/>
    </resultMap>
    <resultMap type="com.ylm.pojo.DeliveryOrder"
               id="findByIdResultMap"
               extends="baseResultMap">
        <collection property="user"
                    ofType="com.ylm.pojo.User"
                    column="{id=userId}"
                    select="com.ylm.dao.UserDao.selectById">
        </collection>
    </resultMap>
    <select id="getAllDeliveryOrderAndUser" resultMap="findByIdResultMap">
        select id,user_id as userId,time,product_info as productInfo,status,money,update_time as updateTime from product_delivery
    </select>

首先我们看一下重点collection的标签属性:
property: resultMap返回实体类中字段和result标签中的property一样
column: 数据库的列名或者列标签别名,是关联查询往下一个语句传送值。注意:在处理组合键时,您可以使用column=“{prop1=col1,prop2=col2}”这样的语法,设置多个列名传入到嵌套查询语句。这就会把prop1和prop2设置到目标嵌套选择语句的参数对象中。
javaType: 一般为ArrayList或是java.util.List,
ofType: java的实体类,对应数据库表的列名称,即关联查询select对应返回的类
select: 执行一个其他映射的sql语句返回一个java实体类型

问题细节说明:

①因为我的一个订单里只有一个User对象,而不是List,属性javaType不用配置。

②我的UserDao继承了MyBatisPlus提供的BaseMapper,select属性中的selectById方法是继承过来的,sql语句需要我们自己去想,因为要用到column传送值,可以想底层的selectById方法中语句中where 后面为 id = #{id} ,

则column="{id=userId}"中id对应selectById方法中的#{id}的参数名称,userId对应getAllDeliveryOrderAndUser方法中“user_id as userId”的别名userId,没有别名就是字段名。

③getAllDeliveryOrderAndUser中最好不写成select * from product_delivery,有可能会报错。

因为column传送值时,不知道字段名在哪。

④我开启了MybatisPlus配置全局的驼峰命名规则,sql中就用的数据库字段名,而不是实体的属性名,查询还是出现null,也不知道是什么情况,如果有报错那么写的sql语句就数据库中的字段名as成实体的属性名。


问题五:mybatis的sql日志如何开启?

在provider的配置文件application.yml中添加(其他格式的配置文件类似,可自行搜索)

方式一:

logging:
    level:
      com.ylm.dao: DEBUG //包路径为mapper文件包路径

方式二:

mybatis: 
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 

问题六:redis连接错误:ERR Client sent AUTH, but no password is set 

这个错误呢就是,你的redis服务没有设置密码,但是你配置了密码导致的(配置密码为空也算)

我是这样写的(yml配置文件)

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    timeout: 30000
    password:
    jedis:
      pool:
        max-active: 100
        max-idle: 50
        min-idle: 5

这里的password后面为空,导致错误出现。

解决方案有2个 。

解决方案一通过配置文件(/etc/redis.conf)进行设置

这种方法在设置密码后需要重启redis生效。首先找到redis的配置文件—redis.conf文件,然后修改里面的requirepass(requirepass 是配置redis访问密码的参数),这个本来是注释起来了的,将注释去掉,并将后面对应的字段设置成自己想要的密码,保存退出。重启redis服务,即可。(推荐学习:Redis视频教程

requirepass 设置成你想改的密码


解决方案二:
  把上面的配置文件中password一行去掉,既然没密码,就不要写。

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    timeout: 30000
    jedis:
      pool:
        max-active: 100
        max-idle: 50
        min-idle: 5

问题七:SpringBoot启动报错:LoggerFactory is not a Logback LoggerContext but Logback is on the classpath

报错日志信息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/Administrator/.m2/repository/org/slf4j/slf4j-reload4j/1.7.36/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/Administrator/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Reload4jLoggerFactory]
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Reload4jLoggerFactory loaded from file:/C:/Users/Administrator/.m2/repository/org/slf4j/slf4j-reload4j/1.7.36/slf4j-reload4j-1.7.36.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.Reload4jLoggerFactory
	at org.springframework.util.Assert.instanceCheckFailed(Assert.java:702)
	at org.springframework.util.Assert.isInstanceOf(Assert.java:621)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:294)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:118)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:232)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:213)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
	at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:79)
	at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:56)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
	at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:56)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:299)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
	at com.ylm.JobSpringBootApplication.main(JobSpringBootApplication.java:9)

Process finished with exit code 1

从上面的日志中我们可以看出一下几点:

1. 项目中存在多个slf4j bindings

SLF4J: Class path contains multiple SLF4J bindings.

2. slf4j选择了Reload4j

SLF4J: Actual binding is of type [org.slf4j.impl.Reload4jLoggerFactory]

3. 由于ch.qos.logback:logback-classic在classpath中,spring-boot会优先加载logback-classi作为日志打印工具,

然而,slf4j又不是选的logback-classic做slf4j的binding,这里就会出错了。

LoggerFactory is not a Logback LoggerContext but Logback is on the classpath

解决方法:找到冲突的依赖排除掉即可(可以在Maven插件里面翻找一下,很快就可以找到)。我的冲突是因为dubbo-dependencies-zookeeper引入冲突。

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <type>pom</type>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-reload4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

问题八:log4j警告:WARN No appenders could be found for logger(org.apache.ibatis.logging.LogFactory). 

日志警告:

log4j:WARN No appenders could be found for logger (org.apache.dubbo.rpc.model.FrameworkModel).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

如果找不到默认配置文件log4j.properties和log4j.xml,并且应用程序不执行显式配置,就会发生这种情况。

解决方法:添加log4j.properties和log4j.xml配置文件

Log4j.properties配置详解 - 简书 (jianshu.com)https://www.jianshu.com/p/ccafda45bcea


问题九:dubbo报错:org.apache.dubbo.rpc.RpcException: Fail to start server ...cause: Address already in use: bind

日志报错:

com.alibaba.dubbo.rpc.RpcException: Fail to start server

服务启动失败

Address already in use: bind

绑定的地址已经被使用。

这里问题是,使用了同一个端口,导致冲突无法启动服务。所以换一个端口号就好了。

即修改下图的port为不同的端口号。


问题十:[DUBBO] Failed to create file store cache. Local file cache will be disabled. 

无法创建文件存储缓存。本地文件缓存被禁用。

解决方法:添加enable-file-cahe: false

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐