1.基本介绍
spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
Spring Cloud的子项目,大致可分成两类,一类是对现有成熟框架”Spring Boot化”的封装和抽象,也是数量最多的项目;第二类是开发了一部分分布式系统的基础设施的实现,如Spring Cloud Stream扮演的就是kafka, ActiveMQ这样的角色。对于我们想快速实践微服务的开发者来说,第一类子项目就已经足够使用,如:
Spring Cloud Netflix
是对Netflix开发的一套分布式服务框架的封装,包括服务的发现和注册(Eureka),负载均衡(Ribbon)、断路器(Hystrix)、REST客户端、请求路由(Zuul)等。
Spring Cloud Config
将配置信息中央化保存, 配置Spring Cloud Bus可以实现动态修改配置文件
Spring Cloud Bus
分布式消息队列,是对Kafka, MQ的封装
Spring Cloud Security
对Spring Security的封装,并能配合Netflix使用
Spring Cloud Zookeeper
对Zookeeper的封装,使之能配置其它Spring Cloud的子项目使用
Spring Cloud Eureka
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件中的一部分,它基于Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能。
**
2.使用RestTemplate + ribbon
**
例子:创建服务中心和两个demo服务项目,项目中的远程调用使用RestTemplate(还可以使用 Feign)
(1)eureka-server项目是服务中心
创建项目时选择 Eureka-Server
配制文件如下:
server:
port: 8576
spring:
application:
name: eureka-server // 服务名
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false // 是否把自己注册进去
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类:
package com.fh.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer // 声明当前项目为注册中心 - 服务治理中心
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
(2) cloud-move 项目
配置文件:
server:
port: 8600
spring:
application:
name: cloud-move
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8576/eureka/
启动类:
package com.fh.move;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@SpringBootApplication
public class MoveApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MoveApplication.class, args);
}
}
接口类:
package com.fh.move.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("login")
public class Login {
@Autowired
private RestTemplate restTemplate;
@GetMapping("my")
public String login(){
return "hello world";
}
@GetMapping("movie")
public String movie(){
String s = restTemplate.getForObject("http://CLOUD-USER/user/userInfo", String.class); // 使用服务名代替ip和端口
return "hello world + " + s;
}
}
注:当注册中心中服务名 CLOUD-USER 对应多个服务时,会使用负载均衡的轮询策略去调用.
(3) cloud-user
配置文件:
server:
port: 9000
spring:
application:
name: cloud-user
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8576/eureka/
接口类:
package com.fh.user.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController {
@GetMapping("userInfo")
public String userInfo(){
return "xiao-Ming";
}
}
启动类:
package com.fh.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
**
3.使用RestTemplate + ribbon + hystrix 避免雪崩**
当某个服务名下的所有服务宕机时,
(1)服务刚挂掉时,报错为 connect time out
(2) 服务挂掉一会时,报错为 负载均衡 某服务名 失败
为解决这种情况,加入了断路器( hystrix ), 设置返回默认数据,,提示错误信息
第一步:引入hystrix:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
第二步:开启断路保护功能: @EnableCircuitBreaker
第三步:在方法上标注(@HystrixCommand(fallbackMethod = “movieHystrix”)) 来指定出错时调用哪个方法。真实方法无法返回时,会使用熔断方法,返回兜底数据
4.使用 Feign+ ribbon + hystrix
第一步:导包
`<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
第二步:启动类添加 @EnableFeignClients 注解 开启feigin
第三步:创建一个远程调用的接口,接口上添加注解 (@FeignClient(name =“product-service”) ),引号中是服务中心中注册的服务名
注:name=“注册服务名称” 同“spring.application.name” 一致
例如:
@FeignClient(value="CLOUD-USER")
public interface UserInterface{
@GetMapping("userInfo")
public String userInfo(){ };
}
注:@FeignClient用于通知Feign组件对该接口进行代理(不需要编写接口实现),使用者可直接通过@Autowired注入。属性可以是name,也可以是value都是表示被调用的相应的服务名称
接口中的方法请求方式、方法名、参数都要和远程调用者保持一致,注意该方法不需要实现
第四步: 启动类中开启断路保护功能:@EnableCircuitBreaker
第五步:开启 Feign 对 Hystrix 的支持
feign:
hystrix:
enabled: true
第六步:在@FeignClient 中添加熔断时调用的方法 (@FeignClient(value=“CLOUD-USER”, fallback=指定这个接口的异常处理类,异常处理类必须实现这个接口) )
例如:
@Component
public Class UserFeignException implements UserInterface {
@Override
public String userInfo(){
return "服务出错";
};
}