云计算python Spring cloud 微服务

理论

微服务概念

 微服务的概念源于2014年3月Martin Fowler所写的一篇文章“Microservices”,微服务架构是一种架构模式,他提倡将单一应用程序划分成一组小的服务,服务之间互相互相协调、互相配合,为用户提供最终价值,每个服务运行在其独立的进程中,服务与服务间采用器轻量级的通信机制互相沟通(通常是基于HTTP的RESTFUL API)。每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等,另外,应尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对其进行构建。微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成。系统中的各个微服务可被独立部署,各个微服务之间是松耦合的(可以相互调用)。每个微服务仅关注于完成一件任务并很好的完成该任务,在所有的情况下,每个任务代表着一个小的业务能力。

耦合:两个东西的关联度,根据关联度的高低来分耦合的程度,

     分为:完全耦合:关联紧密,不能分开,下一步以上一步为基础

           松耦合:关联度不高

           完全解耦:没有任何关联

微服务可以放在容器里跑,容器需要给微服务提供环境,容器里跑的就是微服务,容器跟微服务没有关系

为什么叫微服务架构?

整个架构里只要有微服务层,就可以叫做微服务架构

容易混淆的概念

Spring boot:spring boot是一个框架,一种全新的编程规范,他的产生简化了框架的使用,所谓简化是指简化了spring众多框架中所需的大量且繁琐的配置文件,所以springboot是一个服务于框架的框架,服务范围是简化配置文件

Spring cloud:spring cloud基于spring boot提供了一整套服务的解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器等组件

             Spring cloud利用spring boot的开发便利性巧妙的简化了分布式系统的基础设施开发,spring cloud为开发人员提供了快速构建分布式系统的一些工具,包扣配置管理、服务发现、断路器、路由、微代理、事件总线,他们都可以用spring boot的开发风格做到一键启动和部署

Springboot与springcloud的区别

Spring boot:专注于快速方便的开发单个个体微服务(关注微观,简化配置);

Spring cloud:关注全局的微服务协调治理框架,将spring boot开发的一个个单体微服务组合并管理起来(关注宏观);

Spring boot可以离开spring cloud独立使用,但是spring cloud不可以离开spring boot,属于依赖关系。

Spring cloud全部组件的解析:

  1. Fegin(接口调用):微服务之间通过rest接口通讯,springcloud提供fegin框架来支持rest的调用,fegin使得不同进程的rest接口调用得以用优雅的方式进行,这种优雅表现的就像同一个进程调用一样(简化微服务的通信过程)

  1. Eureka(注册发现):微服务模式下,一个大的web应用通常都被拆分为很多比较小的web应用(服务),这个时候就需要有一个地方保存这些服务的相关信息,才能让各个小的应用彼此知道对方,这个时候就需要在注册中心进行注册。每个应用启动时向配置的注册中心注册自己的信息(IP地址、端口号、服务名称等信息),注册中心将他们保存起来,服务间相互调用的时候,通过服务名称就可以到注册中心找到对应的服务信息,从而进行通讯,注测与发现服务为微服务之间的调用带来了方便,解决了硬编码的问题,服务间只通过对方的服务id,而无需知道其IP和端口即可,以获取对方服务

硬编码:代码写死,调用谁就写谁才能查找出来

  1. Ribbon(负载均衡):ribbon是Netflix发布的负载均衡器,他有助于控制http和TCP客户端的行为,为ribbon配置服务提供者的地址列表后,ribbon就可基于某种负载均衡算法,自动的帮助服务消费者去请求。Ribbon默认为我们提供了很多的负载均衡算法,例如轮询、随机等,当然,我们也可为ribbon实现自定义的负载均衡算法,在spring cloud中,当ribbon与eureka配合使用时,ribbon可自动从eurekaserver获取服务提供者的地址列表,并基于负载均衡算法,请求其中一个服务提供者的实例(为了服务的可靠性,一个微服务可能部署多个实例)

  1. Hystrix(熔断器):当服务提供者响应非常缓慢,那么消费者对提供者的请求就会被强制等待,直到提供者响应超时。在高负载场景下,如果不做任何处理,此类问题可能会导致服务提供者的资源耗竭甚至整个系统的崩溃(雪崩效应)。Hystrix正是为了防止此类问题发生。Hystirx是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或第三方库,防止级联失败,从而提升系统的可用性与容错性。

    第一台:192.168.2.10  

    第二台:192.168.2.20

    两台:
    [root@localhost ~]# yum -y install git
    [root@localhost ~]# git clone https://github.com/luojunyong/spring-cloud-examples.git

    注册中心的集群

    第一台:

    [root@localhost ~]# cd /root/spring-cloud-examples/eureka-producer-consumer/spring-cloud-eureka/

    [root@localhost spring-cloud-eureka]# ls

    pom.xml  src

    [root@localhost spring-cloud-eureka]# vim src/main/resources/application.properties

    spring.application.name=spring-cloud-eureka   #名称

    server.port=8000   #端口

    eureka.client.register-with-eureka=true   #当前的应用是否注册到注册中心上

    eureka.client.fetch-registry=true    #当前的应用是否在注册中心上获取数据                                                                   

    eureka.client.serviceUrl.defaultZone=http://192.168.2.10:8000/eureka/,http://192.168.2.20:8000/eureka/   #两台注册中心的地址

    [root@localhost spring-cloud-eureka]# source /etc/profile

    [root@localhost spring-cloud-eureka]# mvn clean package   打包

    [root@localhost spring-cloud-eureka]# java -jar target/spring-cloud-eureka-0.0.1-SNAPSHOT.jar

        运行     会阻塞终端

    第二台主机

    [root@localhost ~]# cd /root/spring-cloud-examples/eureka-producer-consumer/spring-cloud-eureka/

    [root@localhost spring-cloud-eureka]# vim src/main/resources/application.properties

    server.port=8000   #端口

    eureka.client.register-with-eureka=true   #当前的应用是否注册到注册中心上

    eureka.client.fetch-registry=true    #当前的应用是否在注册中心上获取数据                                                                   

    eureka.client.serviceUrl.defaultZone=http://192.168.2.10:8000/eureka/,http://192.168.2.20:8000/eureka/   #两台注册中心的地址

    [root@localhost spring-cloud-eureka]# source /etc/profile

    打包并运行

    [root@localhost spring-cloud-eureka]# mvn spring-boot:run  会阻塞终端

    访问:

    192.168.2.10:8000/

  2. 启动produce   两台

    第一台

    另开终端

    [root@www ~]# cd /root/spring-cloud-examples/eureka-producer-consumer/spring-cloud-producer/

    [root@www spring-cloud-producer]# vim src/main/resources/application.properties

    spring.application.name=spring-cloud-producer

    server.port=9000

    eureka.client.serviceUrl.defaultZone=http://192.168.2.10:8000/eureka/,http://192.168.2.20:8000/eureka/

    [root@www spring-cloud-producer]# source /etc/profile

    [root@www spring-cloud-producer]# mvn spring-boot:run

    第二台:

    另开终端

    [root@localhost ~]# cd /root/spring-cloud-examples/eureka-producer-consumer/spring-cloud-producer/

    [root@localhost spring-cloud-producer]# vim src/main/resources/application.properties

    eureka.client.serviceUrl.defaultZone=http://192.168.2.10:8000/eureka/,http://192.168.2.20:8000/eureka/

    为了看到负载均衡组件的作用  所以需要把第二台主机源代码的内容进行更改    和第一台不一样

    [root@localhost spring-cloud-producer]# vim src/main/java/com/neo/controller/HelloController.java

    12         return "hello "+name+",thissdjgakjgaaaa";

    [root@localhost spring-cloud-producer]# source /etc/profile

    [root@localhost spring-cloud-producer]# mvn spring-boot:run

    刷新注册中心的界面

  3.  

    验证注册中心和负载均衡组件

    [root@www spring-cloud-consumer]# curl 192.168.2.10:9001/hello/aa

    hello aa,thissdjgakjgaaaa

    [root@www spring-cloud-consumer]# curl 192.168.2.10:9001/hello/aa

    hello aa,this is first messge

  4.  

    需要与运行带熔断器的consumer

    [root@www ~]# cd /root/spring-cloud-examples/spring-cloud-hystrix/spring-cloud-consumer-hystrix/

    [root@www spring-cloud-consumer-hystrix]# vim src/main/resources/application.properties

    spring.application.name=spring-cloud-consumer-hystrix

    server.port=9001

    feign.hystrix.enabled=true

    eureka.client.serviceUrl.defaultZone=http://192.168.2.10:8000/eureka/,http://192.168.2.20:8000/eureka/

    [root@www spring-cloud-consumer-hystrix]# mvn spring-boot:run

    另开终端  访问   得到轮询的效果

    [root@www ~]# curl 192.168.2.10:9001/hello/aa

    hello aa,thissdjgakjgaaaa

    [root@www ~]# curl 192.168.2.10:9001/hello/aa

    hello aa,this is first messge

    测试熔断器    可以在这里把第一台的produce停止掉    发现轮询效果没有了

    [root@www ~]# curl 192.168.2.10:9001/hello/aa

    hello aa,thissdjgakjgaaaa

    [root@www ~]# curl 192.168.2.10:9001/hello/aa

    hello aa,thissdjgakjgaaaa

    熔断器的监控界面

    第一台的produce启动

    第一台上面的带熔断器的consumer结束掉

    第一台上运行带熔断带监控的conumer开启

    [root@www ~]# cd /root/spring-cloud-examples/hystrix-dashboard-turbine/spring-cloud-consumer-hystrix-dashboard/

    [root@www spring-cloud-consumer-hystrix-dashboard]# vim src/main/resources/application.properties

    spring.application.name=spring-cloud-consumer-hystrix-dashboard

    server.port=9001

    feign.hystrix.enabled=true

    eureka.client.serviceUrl.defaultZone=http://192.168.2.10:8000/eureka/,http://192.168.2.20:8000/eureka/

    [root@www spring-cloud-consumer-hystrix-dashboard]# source /etc/profile

    [root@www spring-cloud-consumer-hystrix-dashboard]# mvn spring-boot:run

    另开终端

    [root@www ~]# curl 192.168.2.10:9001/hello/aa

    hello aa,thissdjgakjgaaaa

    [root@www ~]# curl 192.168.2.10:9001/hello/aa

    hello aa,this is first messge

    [root@www ~]# firefox 192.168.2.10:9001/hystrix

  5.  

     

     

    [root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-sleuth-zipkin/

    注册中心

    [root@localhost spring-cloud-sleuth-zipkin]# cd spring-cloud-eureka/

    [root@localhost spring-cloud-eureka]# vim src/main/resources/application.yml

          defaultZone: http://192.168.2.10:8761/eureka/

    [root@localhost spring-cloud-eureka]# source /etc/profile

    [root@localhost spring-cloud-eureka]# mvn spring-boot:run

    链路追踪

    另开终端

    [root@www ~]# cd /root/spring-cloud-examples/spring-cloud-sleuth-zipkin/zipkin-server/

    [root@www zipkin-server]# vim src/main/resources/application.yml

          defaultZone: http://192.168.2.10:8761/eureka/

    [root@www zipkin-server]# source /etc/profile

    [root@www zipkin-server]# mvn spring-boot:run

    运行生产者

    另开终端

    [root@www ~]# cd /root/spring-cloud-examples/spring-cloud-sleuth-zipkin/spring-cloud-producer/

    [root@www spring-cloud-producer]# vim src/main/resources/application.yml

     7     base-url: http://192.168.2.10:9000

    14       defaultZone: http://192.168.2.10:8761/eureka/

    [root@www spring-cloud-producer]# source /etc/profile

    [root@www spring-cloud-producer]# mvn spring-boot:run

    运行网关

    另开终端

    [root@www ~]# cd /root/spring-cloud-examples/spring-cloud-sleuth-zipkin/spring-cloud-zuul/

    [root@www spring-cloud-zuul]# vim src/main/resources/application.yml

        base-url: http://192.168.2.10:9000

      defaultZone: http://192.168.2.10:8761/eureka/

    [root@www spring-cloud-zuul]# source /etc/profile

    [root@www spring-cloud-zuul]# mvn spring-boot:run

    另开终端   访问

    [root@www ~]# curl 'http://192.168.2.10:8888/producer/hello?name=haha&token=123'

    hello haha,this is first messge

    [root@www ~]# curl 'http://192.168.2.10:8888/producer/hello?name=haha&token=123'

    hello haha,this is first messge

    [root@www ~]# firefox 192.168.2.10:9000

  6.  

     

  7.  

     

     

     

     

     

     

     

     

     

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud是一款基于Spring框架的微服务解决方案,它提供了一系列的基于云原生的组件和工具,用于简化微服务架构的开发和部署。 要实现Spring Cloud集成Python Web服务,首先需要在Spring Boot项目中集成Python解释器。可以使用Jython工具,它是一个Java实现的Python解释器。通过集成Jython,我们可以在Java代码中执行Python脚本。 首先,需要在项目的依赖中引入Jython相关的依赖项。然后,创建一个Python类或脚本,并通过编写适配器类,将Python脚本与Spring Boot应用进行交互。 适配器类可以使用PythonInterpreter类,它是Jython提供的一个用于执行Python脚本的类。在适配器类中,可以编写一些方法来调用Python脚本中的函数或方法,并将结果返回给Spring Boot应用。 使用Spring Cloud提供的服务发现和负载均衡功能,可以将Python Web服务注册到服务注册中心,并使用服务发现的特性进行服务的发现和调用。通过在Spring Cloud中定义Python Web服务的接口和Feign客户端,可以方便地调用Python Web服务。 最后,可以使用Spring Cloud的网关组件,如Spring Cloud Gateway或Zuul,来实现对Python Web服务的路由和负载均衡。这样,我们就可以在Spring Cloud微服务框架中集成Python Web服务,并实现微服务架构的高可用和扩展性。 综上所述,通过集成Jython和使用Spring Cloud提供的组件和特性,可以实现Spring Cloud集成Python Web服务。这样的架构可以实现不同语言微服务之间的互操作,并在实际项目中提供更灵活、高效和可扩展的解决方案。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库从删库到跑路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值