SpringCloud(Finchley版)2 - Ribbon+Hystrix

一, 简介

    Ribbon 是一个负载均衡客户端, 能够很好的控制HTTP和TCP的一些行为;java

    在微服务架构中, Ribbon 组件负责 每一个独立服务 之间的 相互通讯调用;git

    Hystrix 断路由处理; web

    若是单个服务出现问题, 调用这个服务就会出现线程阻塞, 此时如有大量的请求涌入, Servlet容器的线程资源会被消耗完毕, 致使服务瘫痪.spring

    服务与服务之间的依赖性, 故障会传播, 会对整个微服务系统形成灾难性的严重后果, 这就是服务故障的“雪崩”效应. apache

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

二, 准备工作

    1, 启动注册中心;   2, 启动服务B;app

三, 建立 cloud-c 服务,实现服务调用功能

    1, cloud-c pom.xml负载均衡

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.gy.cloud</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>cloud-c</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>cloud-c</name>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  
        <!-- netflix-ribbon 该包能够不用引用, 其实 eureka-client 已经自动引入了该包 -->
        <!-- 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        -->
        <!-- 熔断, 服务调用失败后的保护组件 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

</project>

    2, cloud-c application.ymlmaven

server:
  port: 8763

spring:
  application:
    name: service-c

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

    3, cloud-c CloudCApplication 启动类 : spring-boot

package com.gy.cloud.cloudc;

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.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class CloudCApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudCApplication.class, args);
        System.out.println("=== 消费服务C启动成功 ===");
    }

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

    4,  cloud-c  Controller 接口 :

package com.gy.cloud.cloudc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HiController {

    @Autowired
    private HiService hiService;

    @GetMapping("hi")
    public Object hi(String name) {
        name = name == null ? "SERVICE-C" : name;
        return hiService.hi(name);
    }


}

    5, 调用 SERVICE-B

package com.gy.cloud.cloudc;

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 HiService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hi(String name) {
        System.out.println(name);
        String hi = restTemplate.getForObject("http://SERVICE-B/hi?name=" + name, String.class);
        System.out.println(hi);
        return hi;
    }
    // 熔断: 当服务B调用失败, 执行此方法
    public String hiError(String name) {
        return "Hi, " + name +", Sorry Error !";
    }

}

    6,  启动 SERVICE-C , 访问 :   http://localhost:8763/hi

    7,  断开 SERVICE-B , 访问 :   http://localhost:8763/hi

学习文档

方志朋的博客 :  https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/

项目源码:  https://gitee.com/ge.yang/spring-demo/tree/master/cloud

相关文章
相关标签/搜索