理论

微服务概念

微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成。系统中的各个微服务可被独立部署,各个微服务之间是松耦合的(可以相互调用)。每个微服务仅关注于完成一件任务并很好的完成该任务,在所有的情况下,每个任务代表着一个小的业务能力。

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

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

松耦合:关联度不高

完全解耦:没有任何关联

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

为什么叫微服务架构?

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

容易混淆的概念

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

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

  1. Ribbon(负载均衡):ribbon是Netflix发布的负载均衡器,他有助于控制http和TCP客户端的行为,为ribbon配置服务提供者的地址列表后,ribbon就可基于某种负载均衡算法,自动的帮助服务消费者去请求。Ribbon默认为我们提供了很多的负载均衡算法,例如轮询、随机等,当然,我们也可为ribbon实现自定义的负载均衡算法,在spring cloud中,当ribbon与eureka配合使用时,ribbon可自动从eurekaserver获取服务提供者的地址列表,并基于负载均衡算法,请求其中一个服务提供者的实例(为了服务的可靠性,一个微服务可能部署多个实例)
  2. Hystrix(熔断器):当服务提供者响应非常缓慢,那么消费者对提供者的请求就会被强制等待,直到提供者响应超时。在高负载场景下,如果不做任何处理,此类问题可能会导致服务提供者的资源耗竭甚至整个系统的崩溃(雪崩效应)。Hystrix正是为了防止此类问题发生。Hystirx是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或第三方库,防止级联失败,从而提升系统的可用性与容错性。 第一台:192.168.2.10  
1. 第二台: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.defaultZnotallow=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.defaultZnotallow=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.defaultZnotallow=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.defaultZnotallow=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
1. 
需要与运行带熔断器的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.defaultZnotallow=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.defaultZnotallow=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

 

Python微服务部署方案 微服务 python_Python微服务部署方案

 

Python微服务部署方案 微服务 python_开发语言_02

[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:900014       defaultZone: http://192.168.2.10:8761/eureka/">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

 

Python微服务部署方案 微服务 python_开发语言_03

 

Python微服务部署方案 微服务 python_Python微服务部署方案_04


 

Python微服务部署方案 微服务 python_Python微服务部署方案_05

 

Python微服务部署方案 微服务 python_python_06

 

Python微服务部署方案 微服务 python_开发语言_07

 

Python微服务部署方案 微服务 python_spring_08

 

Python微服务部署方案 微服务 python_Python微服务部署方案_09

 

Python微服务部署方案 微服务 python_开发语言_10

 

Python微服务部署方案 微服务 python_spring_11

 

Python微服务部署方案 微服务 python_云计算_12

Python微服务部署方案 微服务 python_云计算_13