springcloud之使用feign消费服务并且使用hystrix进行熔断

springcloud之使用feign消费服务并且使用hystrix进行熔断

    • 服务消费(新建一个feign工程并且使用hystrix)
      • 项目截图
      • 配置文件
      • 代码
    • 监控
      • 配置文件
      • 代码
    • 操作和验证
      • 启动服务
      • 监控服务
    • 连接地址

服务消费(新建一个feign工程并且使用hystrix)

项目截图

springcloud之使用feign消费服务并且使用hystrix进行熔断_第1张图片

配置文件

pom.xml文件


<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">
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.4.RELEASEversion>
        <relativePath/>
    parent>

    <modelVersion>4.0.0modelVersion>

    <groupId>xiejx.cngroupId>
    <artifactId>eureka-consumer-feign-hystrixartifactId>
    <version>1.0.0version>
    <packaging>jarpackaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
            <version>2.0.1.RELEASEversion>
        dependency>

    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Dalston.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>

application.yml

spring:
  application:
    name: eureka-consumer
server:
  port: 3001
#server.port=${random.int[10000,19999]}
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka
logging:
  file: ./logs/${spring.application.name}.log
feign:
  hystrix:
    enabled: true

配置文件需要注意的就是需要打开feign.hystrix.enable的配置并且springboot2.0以上需要手动依赖netflix-hystrix

代码

启动的主类

package jfun;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

/**
 * @Author MIV
 * @Date 2019/4/15
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableFeignClients
public class FeignHystrixApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(FeignHystrixApp.class).web(true).run(args);
    }
}

服务接口

package jfun;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@FeignClient(name = "eureka-client", fallback = DcClientFailBack.class)
public interface DcClient {

    @HystrixCommand
    @GetMapping("/dc")
    String consumer();
}

熔断实现

package jfun;

import org.springframework.stereotype.Component;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@Component
public class DcClientFailBack implements DcClient {

    @Override
    public String consumer() {
        System.out.println("熔断,默认回调函数");
        return "熔断,默认回调函数";
    }
}

服务层

package jfun;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@Service
public class DcService {
    @Autowired
    DcClient dcClient;

    public String dc() {
        return dcClient.consumer();
    }


}

控制器

package jfun;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@RestController
public class DcController {

    @Autowired
    DcService dcService;

    @GetMapping("/consumer")
    public String dc() {
        return dcService.dc();
    }

}

监控

配置文件

pom.xml


<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.0modelVersion>

    <groupId>xiejx.cngroupId>
    <artifactId>hystrix-dashboardartifactId>
    <version>1.0.0version>
    <packaging>jarpackaging>

    <parent>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-parentartifactId>
        <version>Dalston.SR1version>
        <relativePath />
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-hystrixartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-hystrix-dashboardartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
    dependencies>


project>

application.yml

spring:
  application:
    name: hystrix-dashboard
server:
  port: 4000

代码

package jfun;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@EnableHystrixDashboard
@SpringCloudApplication
public class HystrixDashboardApp {
    public static void main(String[] args) {
        System.out.println("======================访问管理hystrix======================");
        System.out.println("http://localhost:4000/hystrix");
        System.out.println("监控feign:http://localhost:3001/hystrix.stream");
        System.out.println("======================访问管理hystrix======================");

        SpringApplication.run(HystrixDashboardApp.class, args);
    }
}

操作和验证

启动服务

  1. 启动eureka-server服务(服务发现)
  2. 启动eureka-client(注册服务)
  3. 启动eureka-consumer-feign-hystrix(服务消费)
  4. 启动hystrix-dashboard(服务监控)

请求http://localhost:3001/consumer进行服务消费,可以看到服务正常消费
然后关闭eureka-client,可以看到服务正常熔断,符合预期。

监控服务

进入http://localhost:4000/hystrix查看监控面板

springcloud之使用feign消费服务并且使用hystrix进行熔断_第2张图片
输入需要监控的服务,点击Monitor Stream进行监控
springcloud之使用feign消费服务并且使用hystrix进行熔断_第3张图片

如果此页面加载不出来,可以重新访问http://localhost:3001/consumer,然后再一次请求页面

连接地址

  • Spring Cloud构建微服务架构
  • Github 代码地址

你可能感兴趣的:(SpringCloud,微服务)