Dubbo 2.6 +SpringBoot2.0 使用log4j2作为日志框架:

1.去除springboot-starter的log4j依赖:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
</dependency>
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
</dependency>

2.引入log4j2

<!-- Log4j2 -->
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

3.dubbo-springboot的依赖不需要去除log4j,在依赖树中只能看到接口层的slf4j,可以兼容log4j2,所以不需要更改

<!--dubbo-springBoot依赖 -->
<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>

4.更改配置信息:

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<!-- 设置变量替换属性 -->
		<log4j2.level>debug</log4j2.level>
		<log4j2.root.path>/opt/appstack/apache-tomcat/logs/${project.name}</log4j2.root.path>
		<log4j2.error.path>/opt/appstack/apache-tomcat/logs/${project.name}-error</log4j2.error.path>
		<log4j2.package.path>/opt/appstack/apache-tomcat/logs/${project.name}-kk</log4j2.package.path>
	 
</properties>

5.在resources目录下新建log4j2-spring.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<properties>
		<!-- 文件输出格式 -->
		<property name="PATTERN">log4j2|%d{yyyy-MM-dd  HH:mm:ss.SSS} | -%-5level [%thread] %c [%L] -| %msg%n</property>
	</properties>
 
	<appenders>
		<Console name="CONSOLE" target="system_out">
			<PatternLayout pattern="${PATTERN}" />
		</Console>
	</appenders>
	
	<loggers>
		<logger name="com.roncoo.education" level="debug" />
		<root level="info">
			<appenderref ref="CONSOLE" />
		</root>
	</loggers>
 
</configuration>

6.思考
其实上面的步骤,基本上亦可以被用来更换springboot默认使用的logback日志应用层,因为dubbo-spring-boot-starter这个依赖中,我本以为需要去除log4j,在网上查了很久,都查不到这个依赖的信息,查看依赖等级之后没有看到log4j的依赖,此处存疑,希望有人能告诉我结果.
这里写图片描述
7.其他问题:
maven的依赖都是很多层的关系,稍有不慎就会出现问题,在我这个项目中,同样出现一个很奇怪的bug,那就是出现服务层消费者在调用提供者的服务时,出现java.lang.ClassCastException的错误,可是明明对象都是一个,经过查询得知,是springboot dev tools的问题,这里需要去除这个依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
 </dependency>

另外,如果出现跟数据源有关的错误的时候,

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

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).

需要在启动类的注解

@SpringBootApplication()加入exclude = DataSourceAutoConfiguration.class--->
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章