【k8s springcloud maven】记录一个fabric8:Kubernetes-client在SpringCloud中Maven依赖的问题

记录一个 fabric8:Kubernetes-clientSpringCloudMaven依赖的问题

引言

在微服务中使用 fabric8:kubernetes-clientk8s 集群进行管理,结果在引依赖的时候出现了依赖版本不正确导致有些类找不到的问题,在这里记录一下,方便日后查找

遇到的问题

在项目中引入 fabric8:kubernetes-client 依赖:

<dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    <version>6.13.0</version>
</dependency>

如果没有引入 SpringCloud 的依赖,那么 kubernetes-client 所依赖的其他 jar 的版本是正确的:

在这里插入图片描述

都是 6.13.0 。但是一旦在 dependencyManagement 中规定 SpringCloud(我规定的版本是 Hoxton.SR12 )的版本,那么就会出现 kubernetes-client 所依赖的其他 jar 的版本改变的情况。

规定 SpringCloud 的版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR12</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

这时版本就变为了:

在这里插入图片描述

可以看到依赖都变了版本

原因

其实就是 SpringCloud 一刀切将版本都限制了,导致 kubernetes-client 所依赖的其他 jar 的版本都跟着遭殃了,如果你想在旧版 SpringCloud 中用新版的 kubernetes-client,那么有两种办法。

解决办法

首先是删除依赖版本管理中的 <scope>import</scope>,但这么做会导致其他依赖的版本失去版本控制:

 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>Hoxton.SR12</version>
             <type>pom</type>
             <scope>import</scope><!--删掉这个-->
         </dependency>
     </dependencies>
 </dependencyManagement>

所以上面这个方法不建议,还有另外一种办法是在 dependencyManagement 中强制覆盖 kubernetes-client 的版本:

 <dependencyManagement>
     <dependencies>
	     <dependency>
	        <groupId>io.fabric8</groupId>
	        <artifactId>kubernetes-client-bom</artifactId>
	        <version>6.13.0</version>
	        <type>pom</type>
	        <scope>import</scope>
	    </dependency>
     </dependencies>
 </dependencyManagement>

其他问题

如果出现了 Spring 日志不打印,并且 kubernetes-client 日志相关的错误:

SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J(W): Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J(W): Ignoring binding found at [jar:file:/D:/dev/maven/apache-maven-3.9.6/repository/ch/qos/logback/logback-classic/1.2.12/logback-classic-1.2.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J(W): See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.3.12.RELEASE)

以及项目启动报错:

ch/qos/logback/classic/spi/LogbackServiceProvider has been compiled 
by a more recent version of the Java Runtime (class file version 55.0),
 this version of the Java Runtime only recognizes class file versions up to 52.0

的情况,可能是日志组件没引入或者版本不对应,
引入日志以及规定版本(SpringBoot 2.3.12.RELEASESpringCloud Hoxton.SR12kubernetes-client 6.13.0):

<dependencyManagement>
	<dependencies>
	    <dependency>
       	   <groupId>org.slf4j</groupId>
	       <artifactId>slf4j-api</artifactId>
	       <version>1.7.30</version>
       </dependency>
       <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-classic</artifactId>
           <version>1.2.3</version>
       </dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>slf4j-api</artifactId>
	    <version>1.7.30</version>
      </dependency>
     <dependency>
	     <groupId>ch.qos.logback</groupId>
	     <artifactId>logback-classic</artifactId>
	     <version>1.2.3</version>
     </dependency>
</dependencies>

再启动 Spring 的日志就回来了, kubernetes-client 的日志也正常不报错了

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值