前言:以maven多模块化的方法搭建 【项目源码我放文章最后了哦~】
一、SpringCloud入门 - 搭建环境
1.创建maven项目:
2.导入依赖: - 限定springboot和springcloud版本
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<springboot.version>2.0.5.RELEASE</springboot.version>
</properties>
<!--管理springboot和springcloud的版本-->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
SpringCloud入门 - Eureka注册中心
1.创建普通maven项目:
2.导入依赖:
<dependencies>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--Eureka服务端支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3.配置:
4.启动入库类:
二、SpringCloud入门 - 服务提供者
1.公共代码 - domain,query,接口,util ...
2.服务提供者
3.导入依赖:
<dependencies>
<!--公共代码依赖-->
<dependency>
<groupId>com.zhengqing</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--eureka客户端支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
4.配置
5.启动类 - 入口:
6.编写业务代码:
7.启动测试:http://127.0.0.1:8001/provider/user/1
刷新Eureka注册中心 - 可称为Eureka的服务端 --> 可看到刚注册的提供者
注意事项: 自我保护引发的错误 - Eureka自我保护机制导致,不影响使用.
如何显示ip地址: --> 客服端实现
三、SpringCloud入门 - 服务消费者-基础
注意:这里与注册中心无关
1.创建一个普通maven项目
2.导入依赖:
<dependencies>
<!--公共代码依赖-->
<dependency>
<groupId>com.zhengqing</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
3.配置
4.编写业务:
5.启动类:
6.启动测试: http://127.0.0.1:9001/comsumer/user/1
四、SpringCloud入门加强 - 注册中心集群
为什么需要集群? --> 如果只有一个注册中心服务器,会存在单点故障所以要集群
操作: 拷贝一份
搭建集群:
1.配置映射hosts
2.配置 :
①EurekaServer_7001中
②EurekaServer_7002中
③User_provider_8001中
3.重新启动测试:
7001中可看见7002
7002中可看见7001
8001中测试正常
再回到7001或7002中刷新,有8001提供者 --> 配置正确!
五、SpringCloud入门加强 - 服务消费者(负载均衡)
为什么需要负载均衡?
为了提供并发量,有时同一个服务提供者可以部署多个。这个客户端在调用时要根据一定的负责均衡策略完成负载调用。
Spring cloud中常见负载均衡实现技术
1. Ribbon
2. Feign
Ribbon负载均衡
Ribbon是Netflix发布的云中间层服务开源项目,主要功能是提供客户端负载均衡算法。Ribbon客户端组件提供一系列完善的配置项,如,连接超时,重试等。简单的说,Ribbon是一个客户端负载均衡器,我们可以在配置文件中列出load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器,我们也很容易使用Ribbon实现自定义的负载均衡算法。
集成原理:
- I -> 单服务提供者操作
1.创建普通maven项目:
2.导入依赖:
<dependencies>
<!--公共代码依赖-->
<dependency>
<groupId>com.zhengqing</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--服务调用-->
<!--eureka客户端支持 - 服务消费者也要从注册中心获取可用服务列表 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 客服端负载均衡实现-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
3. 配置 :
4.启动类:
5.编写业务代码:
- I -> 多服务提供者负载高级操作
1.拷贝一份
2.pom依赖:
<dependencies>
<!--公共代码依赖-->
<dependency>
<groupId>com.zhengqing</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--eureka客户端支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3.配置:
4.编写代码:
5.启动类:
6.测试: - 启动2个provider: 即和
启动consumer测试:http://127.0.0.1:9002/comsumer/user/1刷新会发现name数据会变化
负载均衡策略:
① IRule:通过配置不同IRule的子类,可以选择不同负载均衡策略,也就是从服务列表以特定策略选择一个服务来完成调用。当然也可以自定义。所以负载均衡策略可以分为内置和自定义。
② 内置负载均衡策略
内置负载均衡规则类
规则描述
RoundRobinRule(默认)
简单轮询服务列表来选择服务器。它是Ribbon默认的负载均衡规则。
AvailabilityFilteringRule
对以下两种服务器进行忽略:
(1)在默认情况下,这台服务器如果3次连接失败,这台服务器就会被设置为“短路”状态。短路状态将持续30秒,如果再次连接失败,短路的持续时间就会几何级地增加。
注意:可以通过修改配置loadbalancer.<clientName>.connectionFailureCountThreshold来修改连接失败多少次之后被设置为短路状态。默认是3次。
(2)并发数过高的服务器。如果一个服务器的并发连接数过高,配置了AvailabilityFilteringRule规则的客户端也会将其忽略。并发连接数的上线,可以由客户端的<clientName>.<clientConfigNameSpace>.ActiveConnectionsLimit属性进行配置。
WeightedResponseTimeRule
为每一个服务器赋予一个权重值。服务器响应时间越长,这个服务器的权重就越小。这个规则会随机选择服务器,这个权重值会影响服务器的选择。
ZoneAvoidanceRule
以区域可用的服务器为基础进行服务器的选择。使用Zone对服务器进行分类,这个Zone可以理解为一个机房、一个机架等。
BestAvailableRule
忽略哪些短路的服务器,并选择并发数较低的服务器。
RandomRule
随机选择一个可用的服务器。
Retry
重试机制的选择逻辑
测试:在 中
Feign负载均衡
注意:从这个开始写代码的时候 之前类和配置文件 中的USER_PROVIDER都要换成USER-PROVIDER
ex:9002中,8001中需要换一下
为什么呢?? 因为要报错,不允许使用下划线!!!!!!!!!!!!!!!!!!!!
温馨小提示:前面的可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻。
Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。Feign整合了Ribbon和Hystrix(关于Hystrix我们后面再讲),可以让我们不再需要显式地使用这两个组件。
总起来说,Feign具有如下特性:
①可插拔的注解支持,包括Feign注解和JAX-RS注解;
②支持可插拔的HTTP编码器和解码器;
③支持Hystrix和它的Fallback;
④支持Ribbon的负载均衡;
⑤支持HTTP请求和响应的压缩。
这看起来有点像我们springmvc模式的Controller层的RequestMapping映射。这种模式是我们非常喜欢的。Feign是用@FeignClient来映射服务的。Feign是以接口方式进行调用,而不是通过RestTemplate来调用,feign底层还是ribbo,它进行了封装,让我们调用起来更加happy.
操作:
1.创建一个普通maven项目:
2.导入依赖:
<dependencies>
<!--公共代码依赖-->
<dependency>
<groupId>com.zhengqing</groupId>
<artifactId>User_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--服务调用-->
<!--eureka客户端支持 - 服务消费者也要从注册中心获取可用服务列表 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- feign的支持-可不加,因为有存在的 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
3.配置:
4.编写代码:
①公共代码部分:
导入依赖:
<dependencies> <!--springboot支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- feign的支持-可不加,因为有存在的 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
代码:
注意:要和服务提供者里面访问地址和参数等保持一致!
② 代码:
5.启动类:
6.测试:- 启动注册中心 - 启动服务提供者 -启动服务消费者 http://127.0.0.1:9003/comsumer/user/1
我们也可以控制台打印了...
最后小结: 当对同一个服务部署多个时,就要涉及负载均衡调用了 --> 可以选择Ribbon和Feign 【一般使用Feign 因为通常使用接口类!!】