spring cloud

Springcloud 微服务架构 什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 什么是集群 多台服务器部署相同应用构成一个集群 作用:通过负载均衡设备共同对外提供服务 什么是RPC RPC 的全称是 Remote Procedure Call 是一种进程间通信方式。 它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。即无论是调用本地接口/服务的还是远程的接口/服务,本质上编写的调用代码基本相同。 比如两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数或者方法,由于不在一个内存空间,不能直接调用,这时候需要通过就可以应用RPC框架的实现来解决 restful、soap、rpc (1)RESTful是一种架构设计风格,提供了设计原则和约束条件,而不是架构。而满足这些约束条件和原则的应用程序或设计就是 RESTful架构或服务。 (2)SOAP,简单对象访问协议是一种数据交换协议规范, 是一种轻量的、简单的、基于XML的协议的规范。SOAP协议和HTTP协议一样,都是底层的通信协议,只是请求包的格式不同而已,SOAP包是XML格式的。 SOAP的消息是基于xml并封装成了符合http协议,因此,它符合任何路由器、 防火墙或代理服务器的要求。 soap可以使用任何语言来完成,只要发送正确的soap请求即可,基于soap的服务可以在任何平台无需修改即可正常使用。 (3)RPC就是从一台机器(客户端)上通过参数传递的方式调用另一台机器(服务器)上的一个函数或方法(可以统称为服务)并得到返回的结果。 RPC 会隐藏底层的通讯细节(不需要直接处理Socket通讯或Http通讯) RPC 是一个请求响应模型。客户端发起请求,服务器返回响应(类似于Http的工作方式) RPC 在使用形式上像调用本地函数(或方法)一样去调用远程的函数(或方法)。 rpc远程调用框架 几种比较典型的RPC的实现和调用框架。 (1)RMI实现,利用java.rmi包实现,基于Java远程方法协议(Java Remote Method Protocol) 和java的原生序列化。 (2)Hessian,是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 基于HTTP协议,采用二进制编解码。 (3)thrift是一种可伸缩的跨语言服务的软件框架。thrift允许你定义一个描述文件,描述数据类型和服务接口。依据该文件,编译器方便地生成RPC客户端和服务器通信代码。 (4)SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。 什么是SOA 业务系统分解为多个组件,让每个组件都独立提供离散,自治,可复用的服务能力 通过服务的组合和编排来实现上层的业务流程 作用:简化维护,降低整体风险,伸缩灵活 什么是微服务 架构设计概念,各服务间隔离(分布式也是隔离),自治(分布式依赖整体组合)其它特性(单一职责,边界,异步通信,独立部署)是分布式概念的跟严格执行  SOA到微服务架构的演进过程  作用:各服务可独立应用,组合服务也可系统应用(巨石应用[monolith]的简化实现策略-平台思想) 使用RPC http技术实现会员与订单系统通讯 微服务架构 SpringCloud SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于Springboot的,所以需要开发中对Springboot有一定的了解,如果不了解的话可以看蚂蚁课堂SpringBoot课程。 服务提供者与消费关系   服务提供者:提供服务被人调用 消费者:调用被人服务 服务的注册与发现(Eureka ) 在这里,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。 4.1 服务注册 Eureka的作用 这里先简单说明使用eureka进行业务层隔离,实现项目服务化也可以理解为微服务,先简单上手进行操作,eureka使用分为三块,1是服务注册中心,2是服务生产模块,3是服务消费模块 关系调用说明: 服务生产者启动时,向服务注册中心注册自己提供的服务 服务消费者启动时,在服务注册中心订阅自己所需要的服务 注册中心返回服务提供者的地址信息给消费者 消费者从提供者中调用服务 Eureka包含了Server端和Client端组件。 Server端是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。 Client端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka服务生产者会向注册中心注册自身提供的服务并每30s发送心跳来更新它的服务租约,当一段时间生产者没有向服务中心续租将会被移出服务提供注册表。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。 4.1.1创建eurekaserver 项目 新建父项目,选择maven结构: 选择路径: Pom文件添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <!--Spring Boot的核心启动器-->     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.2.1.RELEASE</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>com.ls.springcloud</groupId>     <artifactId>springcloud</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>pom</packaging>     <name>springcloud</name>     <description>project for Spring Boot</description>     <modules>         <module>eureka</module>     </modules>     <!--springcloud最新版本Hoxton -->     <properties>         <java.version>1.8</java.version>         <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>     </properties>     <dependencies>         <!--支持@SpringCloudApplication注解-->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter</artifactId>         </dependency>         <!--支持HTTP调用方式,包含了Spring Boot预定义的一些Web开发的常用依赖包 如: spring-webmvc,Tomcat....-->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>     </dependencies>     <!--依赖管理-->     <dependencyManagement>         <dependencies>             <dependency>                 <groupId>org.springframework.cloud</groupId>                 <artifactId>spring-cloud-dependencies</artifactId>                 <version>${spring-cloud.version}</version>                 <type>pom</type>                 <scope>import</scope>             </dependency>         </dependencies>     </dependencyManagement>     <build>         <plugins>             <!--告诉Maven包含Spring特定的Maven插件,用于构建和部署SpringBoot应用程序。-->             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project> 新建模块Eureka pom添加依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <!--父依赖-->     <parent>         <groupId>com.ls.springcloud</groupId>         <artifactId>springcloud</artifactId>         <version>0.0.1-SNAPSHOT</version>     </parent>     <groupId>com.ls.springcloud</groupId>     <artifactId>eureka</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>eureka</name>     <description>eureka 注册中心</description>     <dependencies>         <!--eureka注册中心依赖-->         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project> 添加配置文件application.yml spring:   application:     name: eureka eureka:   instance:     #注册中心地址     hostname: localhost   ###客户端调用地址   client:     serviceUrl:       defaultZone: http://${eureka.instance.hostname}:8081/eureka/     ###是否将自己注册到Eureka服务中,因为该应用本身就是注册中心,不需要再注册自己(集群的时候为true)     register-with-eureka: false     ###是否从Eureka中获取注册信息,因为自己为注册中心,不会在该应用中的检索服务信息     fetch-registry: true 创建启动类: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerTestApplication {     public static void main(String[] args) {         SpringApplication.run(EurekaServerTestApplication.class, args);     } } 运行项目访问结果: http://localhost:8081/   此处没有服务注册,所以没有发现服务。 五、服务提供者 创建一个服务提供者 (eureka client),当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除 5.1 创建项目eurekaclient Pom文件添加依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <parent>         <artifactId>springcloud</artifactId>         <groupId>com.ls.springcloud</groupId>         <version>0.0.1-SNAPSHOT</version>     </parent>     <modelVersion>4.0.0</modelVersion>     <artifactId>client</artifactId>     <dependencies>         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project> 添加application.yml配置文件 server:   port: 8082   enable-self-preservation: false spring:   application:     name: eureka-server eureka:   instance:     #注册中心地址     hostname: localhost   client:     serviceUrl:       defaultZone: http://${eureka.instance.hostname}:8081/eureka/ 创建启动类: @EnableEurekaClient @SpringBootApplication   public class EurekaClientApplication {     public static void main(String[] args) {         SpringApplication.run(EurekaClientApplication.class, args);     } } 创建controller: @RestController public class UserController {     @Value("${server.port}")     String port;     @RequestMapping("/hi")     public String home(@RequestParam String name) {         return "hi " + name + ",i am from port:" + port;     } } 查看注册中心: 表示该服务已经注册,可以查看端口。 访问:http://localhost:8082/hi?name=li 六、服务消费者(rest+ribbon) 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。 6.1 什么是ribbon ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。 6.2 创建ribbon Pom文件添加依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <parent>         <artifactId>springcloud</artifactId>         <groupId>com.ls.springcloud</groupId>         <version>0.0.1-SNAPSHOT</version>     </parent>     <modelVersion>4.0.0</modelVersion>     <artifactId>ribbon</artifactId>     <dependencies>         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project> 添加配置文件: server:   port: 8084   enable-self-preservation: false spring:   application:     name: ribbon eureka:   instance:     #注册中心地址     hostname: localhost   client:     serviceUrl:       defaultZone: http://${eureka.instance.hostname}:8081/eureka/ 添加启动类 package com.ls.ribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.client.RestTemplate; @EnableAutoConfiguration @ComponentScan(basePackages={"com.ls.ribbon.controller","com.ls.ribbon.service"}) @EnableEurekaClient public class DemoApplication {     public static void main(String[] args) {         SpringApplication.run(DemoApplication.class, args);     }     @Bean     @LoadBalanced     RestTemplate restTemplate() {         return new RestTemplate();     } } 创建controller: package com.ls.ribbon.controller; import com.ls.ribbon.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloControler {         @Autowired         private HelloService helloService;         @RequestMapping(value = "/hi")         public String hi(@RequestParam String name) {             return helloService.hiService(name);         } } 创建service: package com.ls.ribbon.service; //import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService {     @Autowired     RestTemplate restTemplate;     public String hiService(String name) {         return restTemplate.getForObject("http://EUREKA-CLIENT/hi?name="+name,String.class);     } } 演示效果: 注册中心: http://localhost:8084/hi?name=li 这说明当我们通过调用restTemplate.getForObject(“http://EUREKA-CLIENT /hi?name=“+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。 七、服务消费者(Feign) 7.1 什么是Feign Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。 简而言之: Feign 采用的是基于接口的注解 Feign 整合了ribbon 7.2 准备工作 启动eureka,端口为8081; 启动eureka-client 两次,端口分别为8082 、8083. 7.3 创建一个Feign服务 7.1 新建Feign模块 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <parent>         <artifactId>springcloud</artifactId>         <groupId>com.ls.springcloud</groupId>         <version>0.0.1-SNAPSHOT</version>     </parent>     <modelVersion>4.0.0</modelVersion>     <artifactId>Feign</artifactId>     <dependencies>         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>         </dependency>         <!--引入feign 依赖,包含ribbon负载均衡,也包含Hystrix服务容错。-->         <!--Spring Cloud Feign在构建被@FeignClient注解修饰的服务客户端是,会为每一个客户端都创建一个feign.Logger实例,我们可以利用该日志对象进行Log分析。-->         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-openfeign</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project> 7.2 添加配置文件 server:   port: 8085   enable-self-preservation: false spring:   application:     name: feign eureka:   instance:     #注册中心地址     hostname: localhost   client:     serviceUrl:       defaultZone: http://${eureka.instance.hostname}:8081/eureka/ 7.3 创建Controller package com.ls.controller; import com.ls.service.FeignService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class IndexController {    @Autowired    private FeignService feignService;    @RequestMapping(value = "/hi",method = RequestMethod.GET)    public String hi(String name) {       return feignService.hi(name);    }     } 7.4 创建service @Repository @FeignClient(value = "EUREKA-CLIENT") public interface FeignService {    @RequestMapping(value = "/hi", method = RequestMethod.GET)    public String hi(@RequestParam(value = "name") String name); } 7.5 创建启动类 @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class SericeFeignApp {    public static void main(String[] args) {       SpringApplication.run(SericeFeignApp.class, args);    } } 7.6 运行结果: http://localhost:8085/hi?name=li 刷新交替显示8082 8083 八、 Hystrix断路器 在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。 为了解决这个问题,业界提出了断路器模型。 8.1 什么是Hystrix Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图: 较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。 断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。 这篇文章基于上一篇文章的工程,首先启动上一篇文章的工程,启动eureka工程;启动eureka工程,它的端口为8082。 8.2 在ribbon使用断路器 改造ribbon代码,在pom文件添加依赖: <dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> 改造Service: package com.ls.ribbon.service; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService {     @Autowired     RestTemplate restTemplate;     @HystrixCommand(fallbackMethod = "error")     public String hiService(String name) {         return restTemplate.getForObject("http://EUREKA-CLIENT/hi?name="+name,String.class);     }     public String error(String name){         return "参数为:"+name+",调用EUREKA-CLIENT 失败啦!";     } } 修改Controller: @EnableEurekaClient @EnableCircuitBreaker // 开启hystrix断路器 @SpringBootApplication public class DemoApplication {     public static void main(String[] args) {         SpringApplication.run(DemoApplication.class, args);     }     @Bean     @LoadBalanced     RestTemplate restTemplate() {         return new RestTemplate();     } } 运行结果: 关闭8082端口,访问会回调error方法: http://localhost:8084/hi?name=li 8.3 在Feign中使用断路器 Feign是自带断路器的,在D版本的Spring Cloud中,它没有默认打开。需要在配置文件中配置打开它,在配置文件加以下代码: feign:   hystrix:     enabled: true 基于service-feign工程进行改造,只需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定类就行了: 改造Service: package com.ls.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Repository @FeignClient(value = "EUREKA-CLIENT",fallback=SchedualServiceHiHystric.class) public interface FeignService {    @RequestMapping(value = "/hi", method = RequestMethod.GET)    public String hi(@RequestParam(value = "name") String name); } SchedualServiceHiHystric: package com.ls.service; import org.springframework.stereotype.Component; @Component public class SchedualServiceHiHystric implements FeignService {     public String hi(String name) {         return "feign ... name:" + name + " 系統錯誤,调用接口失败~!!";     } } 运行结果:http://localhost:8085/hi?name=li

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值