美文网首页
springboot+Spring Cloud-hystrix整

springboot+Spring Cloud-hystrix整

作者: 小怪兽打葫芦娃 | 来源:发表于2019-07-29 15:43 被阅读0次

Dubbo入门案例(一)
Dubbo入门案例(二)
Dubbo入门案例(三)
Dubbo入门案例(四)
Dubbo入门案例(五)
Dubbo入门案例(六)
Dubbo入门案例(七)
Dubbo入门案例(八)

① 工程boot-user-service-provider添加pom文件

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

② 在BootUserServiceProviderApplication类上增加@EnableHystrix来启用hystrix starter:

package com.maweiqi;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

// 开启基于注解的dubbo功能
@EnableDubbo
@EnableHystrix
@SpringBootApplication
public class BootUserServiceProviderApplication {

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

}

③ 在工程boot-user-service-provider的HelloServiceImpl添加注解@HystrixCommand

package com.maweiqi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.maweiqi.service.HelloService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * HelloServiceImpl
 *
 * @Author: 马伟奇
 * @CreateTime: 2019-07-18
 * @Description:
 */
@Service
public class HelloServiceImpl implements HelloService {
    @HystrixCommand
    @Override
    public String sayHello(String name)  {
        int a = 1/0;
        return "8082 hello " + name;
    }
}

运行,启动provider工程

① 工程boot-user-service-consumer,添加pom文件

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

② BootUserServiceConsumerApplication添加注解@EnableHystrix

package com.maweiqi;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@EnableDubbo
@EnableHystrix
@SpringBootApplication
public class BootUserServiceConsumerApplication {

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

}

③ 添加注解@HystrixCommand(fallbackMethod = "hello"),进行容错处理

package com.maweiqi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maweiqi.service.HelloService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * HelloController
 *
 * @Author: 马伟奇
 * @CreateTime: 2019-07-19
 * @Description:
 */
@Controller
@RequestMapping("/demo")
public class HelloController {
    @Reference
    private HelloService helloService;
    @HystrixCommand(fallbackMethod = "hello")
    @RequestMapping("/hello")
    @ResponseBody
    public String getName(String name){
        //远程调用
        String result = helloService.sayHello(name);
        System.out.println(result);
        return result;
    }

    public String hello(String name) {
        return "hystrix fallback value";
    }

}

运行:http://localhost:8088/demo/hello?name=cc

image.png

相关文章

网友评论

      本文标题:springboot+Spring Cloud-hystrix整

      本文链接:https://www.haomeiwen.com/subject/bkbbrctx.html