Spring Cloud 从入门到入门

参考:https://blog.csdn.net/hellozpc/article/details/83692496

参考:https://www.fangzhipeng.com/spring-cloud.html

Spring Cloud 

spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理事件总线、全局锁、决策竞选、分布式会话等。运行环境就简单,另外说明的是

spring cloud 是基于spring boot 的。

1.1 注册中心

spring cloud netflix 的 eureka 是一个服务注册和发现模块。包括服务端与客户端。启动一个注册中心,只需要一个注解 @EnableEurekaServer ,这个注解注释到 spring boot 的启动工程类上

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}

Eureka 是一个高可用的组件,没有后端缓存,每一个实例注册之后都要向注册中心发送心跳,在默认情况下eureka server 也是一个 eureka client ,必须指定一个server.

application.yml 配置

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.2 创建一个服务提供者( eureka client)

当 client 向 server 注册时,它会提供一些元数据,例如主机和端口,url 。每个 client 实例接受心跳消息,如果心跳超时,则通常将该实例从注册服务中删除。

eureka client 需要依赖 spring-boot-start-web 

@SpringBootApplication
@EnableEurekaClient
public class ServiceHiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApplication.class, args);
    }
}

2.1 ribbon  是一个负载均衡客户

可以很好的控制 http 和 tcp 的一些行为。 Feign 默认集成了 ribbon首先需要导入ribbon 的依赖,在工程的启动类中通过 @EnableDiscoveryClient 向服务中心注册。并且向程序的 ioc 注入一个 bean ,

restTemplate 并通过@LoadBalance 注解表明这个 restTemplate 开启负载均衡的功能。

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }
}

3.1 Feign 是一个声明式的 http 客户端

它使得些Htpp 客户端变得简单。使用 Feign 只需要创建一个接口并注解。它具有可拔插的注解特性,可使用feign 注解和 JAX-RS 注解。Feign 支持可拔插的编码和解析器。Feign 默认集成了Ribbon ,并和Eureka 结合,默认实现了负载均很的效果。

  • Feign 是采用基于接口的注解
  • Feign 整合了ribbon

需要在程序的启动类上加 @EnableFeignClients 注解开启 Feign 的功能。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }
}

定义一个Feign 接口,通过@FeignClient("服务名")来指定调用哪个服务。

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

4.1 Hystrix

在微服务中,根据业务来拆分成一个个的服务,服务与服务之间可以互相调用RPC ,在spring cloud 可以用 RestTemplate + Ribbon 和 Feign 来调用。为了保证高可用,单个服务通常会集群部署,由于网络的原因或者自身的原因,服务并不能保证

100% 可以。如果单个服务出问题,调用这个服务的线程会阻塞,此时若大量的请求涌入,Servlet 容器的线程资源会被消耗完毕,导致服务瘫痪。服务于服务之间的依赖性,故障会传播,对整个微服务系统造成严重的后果,这就是服务故障的“雪崩”效应

为了解决该问题,断路器提出来

Netflix 开源 Hystrix 组件,实现了断路器模式,Spring Cloud 对这一组件进行了整合。在微服务架构中,一个请求需要调用多个服务是常见的

 较底层的服务如果故障,会导致连锁故障。当对特定的服务调用到达一个阈值(Hystrix 是 5 秒20次)断路器将会被打开。

 断路器打开后,可用避免连锁故障, fallback 方法可直接返回一个固定的值。

在程序的启动类加@EnableHystrix 注解开启 Hystrix

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

在方法上加@HystrixCommand 注解,该注解对方法创建了熔断功能,并指定熔断方法。

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
}

4.2 Feign 中使用断路器

Feign 是自带断路器的,需要配置开启断路器,默认关闭

feign.hystrix.enabled=true
@FeignClient(value = "服务名称",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

创建类继承接口,重写接口的方法

@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}

 5.1 Zuul

在微服务架构中,需要几个基础的服务治理组件,包括服务注册发现、服务消费、负载均衡、断路器、智能路由、配置管理等共同组成一个简单的微服务系统。

 Zuul 的主要功能是路由 转发和过滤器,路由功能是微服务的一部分,比如 /api/user/转发到 user 服务 。api/shop 转发到shop 服务。zuul默认和ribbon 结合实现了负载均衡的功能。

原文地址:https://www.cnblogs.com/baizhuang/p/11385264.html