资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

11、RestTemplate+Ribbon整合断路器Hys-创新互联

在微服务架构中,根据业务需求拆分成一个个的微小服务,然后服务与服务之间可以相互RPC远程调用。在Spring Cloud可以使用RestTemplate+Ribbon或者Feign来进行RPC远程调用。为了保证服务高可用性,单个服务通常会进行集群部署。由于网络原因或者自身的原因,服务并不能保证百分之一百可用,如果服务方出现问题,调用这个服务就会出现线程阻塞,此时若有出现大量请求,导致服务方瘫痪。这时断路器就派上用场了。
11、RestTemplate+Ribbon整合断路器Hys
当对某个服务的调用的不可用达到一个阀值(Hystric 默认是5秒20次) 断路器将会被自动被打开。断路打开后, fallback方法可以直接返回一个预先设置的固定值。

北票网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。成都创新互联从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

1、 新建项目sc-eureka-client-consumer-ribbon-hystrix,对应的pom.xml文件


    4.0.0

    spring-cloud
    sc-eureka-client-consumer-ribbon
    0.0.1-SNAPSHOT
    jar

    sc-eureka-client-consumer-ribbon
    http://maven.apache.org

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            

        
    

    
        UTF-8
        1.8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        

        
                org.springframework.boot
                spring-boot-starter-web
        

        

        
                org.springframework.cloud
                spring-cloud-starter-netflix-hystrix
                2.0.1.RELEASE
        

    

备注:spring-cloud-starter-hystrix已经在spring cloud 2.x标注成过期。推荐使用spring-cloud-starter-netflix-hystrix
11、RestTemplate+Ribbon整合断路器Hys
2、 新建spring boot 启动类ConsumerApplication.java

package sc.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class ConsumerHystrixApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConsumerHystrixApplication.class, args);

    }

}

可以看出这个启动类只是《服务发现&服务消费者Ribbon》的启动类添加多了一个注解EnableHystrix(启动断路器)

3、 编写服务类,并添加断路器注解

package sc.consumer.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.github.andrewoma.dexx.collection.ArrayList;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import sc.consumer.model.User;
import sc.consumer.service.UserService;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getUserError")
    @Override
    public Map getUser(Long id) {
        return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/getUser/{1}", Map.class, id);
    }

    public Map getUserError(Long id){
        Map map = new HashMap();
        map.put("code", "000000");
        map.put("msg", "ok");
        User u = new User();
        u.setId(-1L);
        u.setUserName("failName");
        map.put("body", u);
        return map;
    }

    @HystrixCommand(fallbackMethod = "listUserError")
    @Override
    public Map listUser() {
        return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/listUser", Map.class);
    }

    public Map listUserError(){
        Map map = new HashMap();
        map.put("code", "000000");
        map.put("msg", "ok");
        map.put("body", new ArrayList());
        return map;
    }

    @HystrixCommand(fallbackMethod = "addUserError")
    @Override
    public Map addUser(User user) {
        return restTemplate.postForObject("http://sc-eureka-client-provider:8200/user/addUser", user, Map.class);
    }

    public Map addUserError(User user){
        Map map = new HashMap();
        map.put("code", "000000");
        map.put("msg", "ok");
        map.put("body", 0);
        return map;
    }

    @HystrixCommand(fallbackMethod = "updateUserError")
    @Override
    public Map updateUser(User user) {
        restTemplate.put("http://sc-eureka-client-provider:8200/user/updateUser",user);
        Map map = new HashMap();
        map.put("code", "000000");
        map.put("msg", "ok");
        return map;
    }

    public Map updateUserError(User user){
        Map map = new HashMap();
        map.put("code", "000000");
        map.put("msg", "ok");
        map.put("body", 0);
        return map;
    }

    @HystrixCommand(fallbackMethod = "deleteUserError")
    @Override
    public Map deleteUser(Long id) {
        restTemplate.delete("http://sc-eureka-client-provider:8200/user/deleteUser/{id}", id);
        Map map = new HashMap();
        map.put("code", "000000");
        map.put("msg", "ok");
        return map;
    }

    public Map deleteUserError(Long id){
        Map map = new HashMap();
        map.put("code", "000000");
        map.put("msg", "ok");
        map.put("body", 0);
        return map;
    }

}

添加HystrixCommand注解,对应的参数fallbackMethod值为当方式服务方无法调用时,返回预设值得方法名。

4、 新建配置文件bootstrap.yml和application.yml
bootstrap.yml

server:
    port: 5600

application.yml

spring:
    application:
        name: sc-eureka-client-consumer-ribbon-hystrix

eureka:
    instance:
        hostname: 127.0.0.1
    client:
        #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
        registerWithEureka: true
        #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
        fetchRegistry: true
        serviceUrl:
            defaultZone: http://127.0.0.1:5001/eureka/

5、 其他项目文件参加下图

11、RestTemplate+Ribbon整合断路器Hys

6、 分别启动注册中心sc-eureka-server和服务提供者sc-eureka-client-provider

7、 启动sc-eureka-client-consumer-ribbon-hystrix,并验证是否启动成功
方式一:查看日志
11、RestTemplate+Ribbon整合断路器Hys

方式二:查看注册中心是否注册成功

11、RestTemplate+Ribbon整合断路器Hys
8、 验证断路器是否起作用
(1) 服务提供者正常时访问:
http://127.0.0.1:5600/cli/user/getUser/4
11、RestTemplate+Ribbon整合断路器Hys
正常返回数据库里的数据
(2) 服务提供者关闭时访问:
http://127.0.0.1:5600/cli/user/getUser/4
11、RestTemplate+Ribbon整合断路器Hys
对比两个返回的数据,可以看出服务提供者关闭时,返回的数据是在程序写死的数据,如下图:
11、RestTemplate+Ribbon整合断路器Hys
其他方法可以自行按照以上方式进行验证。

公众号:java乐园

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:11、RestTemplate+Ribbon整合断路器Hys-创新互联
本文网址:http://cdkjz.cn/article/deeosg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220