前言
在今天的文章中小码哥将会给大家分享一个目前工作中遇到的一个比较有趣的案例,就是如何将Python写的微服务融入到以Java技术栈为主的Spring Cloud微服务体系中?也许有朋友会有疑问,到底什么样的场景需要用Python写一个微服务,并且还要融入以Java技术栈为主的Spring Cloud微服务体系中呢?
大致情况是这样的,小码哥目前所在的公司后端技术栈基本上是以Java为主,并且整个后端软件系统采用的也是基于Spring Cloud框架为主的微服务架构(PS:在以往的文章中小码哥写了一些关于这方面的文章,大家可以在文末的推荐阅读中查看相关内容),服务的注册发现是基于Consul,而服务的调用及负载均衡也都是基于FeignClient调用以及Robbin客户端依赖来实现的,所以整体架构大概就是这样的一个标准Spring Cloud微服务架构。如图所示:
大部分场景下基于以上微服务架构是比较好扩展的,例如你有一个新的微服务,如果完全可以通过Java语言构建的话,那就是非常简单的一件事,因为你只需要基于Spring Boot编写一个微服务项目,然后通过Spring Cloud提供的注解将其快速地注入Consul的服务注册&发现机制,然后就可以很快地对内或对外提供服务了。
而在这里,小码哥遇到的是一个比较特殊的场景,因为最近小码哥一直在做一些出行行业相关的事情,所以需要做一些路径规划和计算的工作。这里就有一个比较棘手的需求:“需要对车辆的调度做一些路径规划,简单的来说就是地图上有很多个坐标点的位置,需要给有限的运营车辆做路径规划,尽量以一个距离最短的最佳路线去遍历完这些位置,从而节省运营资源提高运营效率”。
关于这个问题,实际上是涉及到计算机科学中比较经典的一个TSP(旅行商)算法问题,如果大家对这个算法有了解的话,就会理解这个问题需要非常大的计算量,因为每多几个位置,其算法的复杂度就会呈指数增长。而要解决这个问题,如果自行编写解决方案的话需要耗费很大的精力并且还需要不断的优化算法!
所以这个时候小码哥就想是不是有一些相对比较可靠的开源工具可以利用呢?所以经过一些研究和调研,果然发现有一个Google开源的运筹计算工具OR-TOOLS,其中提供了关于TSP及VRP问题的解法,关于这个工具解决TSP及VRP问题的方法与TSP问题一样,小码哥会在后面找机会给大家分享。
因为计算量非常大所以在使用OR-TOOLS工具时,我们需要在本地安装OR-TOOLS软件,而在具体编写计算代码时因其对Java的支持体验比较差(缺乏官方发布的Maven依赖,以及示例代码不全等),所以最终我们需要使用Python语言来进行开发,并且每一次的路径规划计算,都需要以服务的方式对上层应用进行开放。
说到这里,各位应该已经理解了小码哥的纠结的问题了,因为Python服务相对于Spring Cloud这一套体系来说,算是一个异构服务了,其本身并不像Java那样可以很方便的利用Spring Boot、Spring Cloud提供的一套成熟的体系。而如果选择不融入Spring Cloud体系,那意味着对于Python服务,我们需要做单独的部署及负载设计。例如,我们可能需要单独部署几个Python节点,然后通过Nginx单独配置负载均衡,内部微服务调用也需要每次都绕到Nginx那一层才可以以负载的方式访问。如下图所示:
实际上这种方式就是回到了早期传统服务架构时代的负载均衡模式配置方式上去了,虽然也没有太大的问题,只是为这样个别的异构服务单独设置一套部署体系,从成本及扩展性上来说的确有些别扭!所以,如果我们可以直接将Python写的异构服务也能通过注册到Consul的话,这样也就融入了标准的Spring Cloud微服务体系,问题就简单多了!
构建Python web服务
接下来我们就以具体代码的方式先一起来看看怎么样编写一个Python web服务,并看看怎么样才可以将其注册到Consul中,并与其他微服务实现服务发现和调用!在基于Python编写Web服务时,为了简化开发可以选择一个比较成熟的PythonWeb框架,这里小码哥用的是Tornado,Python中其他Web框架还有Flask、Django等,因为Tornado性能相对比较高适合做后端接口服务,所以就选择了Tornado,这里就不再进一步说明了。
在具体进行代码开发时,我们需要安装好Python开发环境,这里小码哥使用的是Python3.7.3,而Tornado使用的则是5.1.1版本,具体的安装方式大家可以查一下,这里就不再多说!接下了,我们具体来看下在真实的项目工程中时怎么将Python注入Consul的!
因为Python不像Java那样基于Spring Cloud有一套完整的依赖包,可以很方便地使用一个注解就可以进行服务注册与发现,所以我们需要基于consulate这个Python库来单独编写服务注册代码,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
有了以上这段服务注册代码的实现,我们再来看看入口代码中如何在启动服务时注入Consul,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
可以看到上述代码通过配置获取了consul的地址及端口信息,并自定义了服务节点的serviceId、tags以及进行健康性检查时,Consul探测的服务接口地址的定义:
1 |
|
我们知道Consul与微服务之间需要通过健康性检查来做心跳,在Java中因为Spring Cloud依赖包已经替我们实现好了这样的接口,而在Python中就需要我们手工定义,如上述代码中我们就定义了/actuator/health服务,并实现了其处理代码,很简单就是返回成功,如下:
1 2 3 4 5 |
|
这样在服务注册到Consul之后,Consul就可以通过这个接口来与Python微服务之间通过发送心跳来探活了。此时,如果我们在配置中制定Consul的地址,并启动Python微服务,就可以将其注入Consul了,如:
1 2 3 4 5 6 7 |
|
启动服务后,就可以看到Consul发过来的心跳请求了,此时如果我们打开Consul的web控制台,也能看到服务成功的被注册到Consul上了,如:
之后该Python服务就可以像其他Java编写的微服务一样即可以通过api-gateway直接被前端调用,也可以通过FeignClient以负载均衡的方式被其他微服务调用了!
Python 多环境配置
这里再多给大家分享一点,就是我们知道在Spring Cloud微服务中,我们可以通过spring.profile.active这个参数来指定不同环境的配置,从而实现多环境适配,而在Python中因为没有像Spring Boot这样的框架,所以我们只能自己来实现了,例如,下面的配置代码就是给Python微服务实现的一个多环境配置的代码,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
这样我们在启动python脚本时只需要传递对应的环境参数,也可以实现多环境的配置读取了,例如:
1 2 3 |
|
后记
以上就是关于Python微服务作为异构服务融入Spring Cloud体系的一些介绍了,在实际的场景中还会有诸如其他语言编写的微服务的场景,如Go!其基本思路类似,只是Java相对于其他语言来说,由于其完整的开源生态,会简单很多!
推荐阅读:
Spring Cloud微服务中网关服务是如何实现的?(Zuul篇)