关于spring-cloud-kubernetes
spring-cloud-kubernetes是springcloud官方推出的开源项目,用于将Spring Cloud和Spring Boot应用运行在kubernetes环境,并且提供了通用的接口来调用kubernetes服务,GitHub上官方地址是:https://github.com/spring-cloud/spring-cloud-kubernetes
系列文章列表本文是《spring-cloud-kubernetes实战系列》的第一篇,全文链接如下:
《spring-cloud-kubernetes官方demo运行实战》
《你好spring-cloud-kubernetes》
《spring-cloud-kubernetes背后的三个关键知识点》
《spring-cloud-kubernetes的服务发现和轮询实战(含熔断)》
《spring-cloud-kubernetes与SpringCloud Gateway》
《spring-cloud-kubernetes与k8s的configmap》
官方demo官方提供了简单的demo用于快速了解spring-cloud-kubernetes,但是成功运行此demo需要做一些设置和修改,这也是此文的意义所在,接下来我们一起实战这个demo;
环境信息本次实战的环境和版本信息如下:
操作系统:CentOS Linux release 7.6.1810
minikube:1.1.1
Java:1.8.0_191
Maven:3.6.0
fabric8-maven-plugin插件:3.5.37
spring-cloud-kubernetes:1.0.1.RELEASE
上面提到的linux、minikube、java、maven,请确保已全部准备好,关于linux环境下minikube的安装和启动请参考《Linux安装minikube指南 》。
下载源码官方demo包含在整个spring-cloud-kubernetes开源项目中,因此要下载整个开源项目,由于主干的提交一直很活跃,因此最好下载个release版本,我这里下载的是v1.0.1.RELEASE,如下图红框所示,地址是:https://github.com/spring-cloud/spring-cloud-kubernetes/releases
需要事先修改maven的设置,否则编译构建的时候会报错:
打开maven的配置文件settings.xml,完整路径是apache-maven-3.6.0/conf/settings.xm;
在settings.xml文件内,找到pluginGroups节点,在里面增加两行,修改完成后效果如下:
<pluginGroups> <pluginGroup>io.fabric8</pluginGroup> <pluginGroup>org.springframework.boot</pluginGroup> </pluginGroups>修改完毕保存退出,maven设置完成;
如果您想了解该错误的细节,请参考文章《使用fabric8-maven-plugin插件的错误处理(No plugin found for prefix 'fabric8')》;
编译项目源码将上图中的源码下载解压,得到一个新的文件夹spring-cloud-kubernetes-1.0.1.RELEASE;
进入文件夹spring-cloud-kubernetes-1.0.1.RELEASE,执行命令mvn clean compile -U,完全编译整个工程,由于要下载大量依赖库所以较为耗时,我这里是二十分钟以上(这一步不是必须的,看个人爱好吧),构建通过后如下所示:
[INFO] Reactor Summary for Spring Cloud Kubernetes 1.0.1.RELEASE: [INFO] [INFO] Spring Cloud Kubernetes :: Dependencies ............ SUCCESS [ 0.077 s] [INFO] Spring Cloud Kubernetes ............................ SUCCESS [ 2.575 s] [INFO] Spring Cloud Kubernetes :: Core .................... SUCCESS [01:51 min] [INFO] Spring Cloud Kubernetes :: Config .................. SUCCESS [ 21.357 s] [INFO] Spring Cloud Kubernetes :: Discovery ............... SUCCESS [ 6.473 s] [INFO] Spring Cloud Kubernetes :: Ribbon .................. SUCCESS [ 31.616 s] [INFO] Spring Cloud Kubernetes :: Starter ................. SUCCESS [ 0.558 s] [INFO] Spring Cloud Kubernetes :: Starter :: Config ....... SUCCESS [ 0.569 s] [INFO] Spring Cloud Kubernetes :: Starter :: Ribbon ....... SUCCESS [ 0.595 s] [INFO] Spring Cloud Kubernetes :: Starter :: All .......... SUCCESS [ 0.571 s] [INFO] Spring Cloud Kubernetes :: Examples ................ SUCCESS [ 0.558 s] [INFO] Spring Cloud Kubernetes :: Examples :: Reload ConfigMap SUCCESS [ 9.077 s] [INFO] Spring Cloud Kubernetes :: Examples :: Hello World . SUCCESS [ 1.323 s] [INFO] Spring Cloud Kubernetes :: Leader .................. SUCCESS [ 7.395 s] [INFO] Spring Cloud Kubernetes :: Examples :: Leader Election SUCCESS [ 0.594 s] [INFO] Spring Cloud Kubernetes :: Istio ................... SUCCESS [ 12.788 s] [INFO] Spring Cloud Kubernetes :: Integration Tests ....... SUCCESS [ 0.574 s] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Simple Core SUCCESS [02:14 min] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Simple Configmap SUCCESS [ 0.646 s] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Istio SUCCESS [ 0.623 s] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Parent SUCCESS [ 0.564 s] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Service A SUCCESS [ 0.605 s] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Service B SUCCESS [ 0.625 s] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Client SUCCESS [ 0.608 s] [INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Tests SUCCESS [ 1.440 s] [INFO] Spring Cloud Kubernetes Docs ....................... SUCCESS [ 0.583 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 05:53 min [INFO] Finished at: 2019-06-08T19:32:19+08:00 [INFO] ------------------------------------------------------------------------