Spring cloud uses feign consumer service and uses hystrix to fuse

Keywords: Spring Maven Apache xml

Spring cloud uses feign consumer service and uses hystrix to fuse

Service consumption (create a feign project and use hystrix)

Project screenshots

configuration file

pom.xml file

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

    <modelVersion>4.0.0</modelVersion>

    <groupId>xiejx.cn</groupId>
    <artifactId>eureka-consumer-feign-hystrix</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- load balancing-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

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

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </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

It should be noted that the configuration file feign.hystrix.enable needs to be opened and the configuration of spring boot 2.0 and above needs to rely on netflix-hystrix manually

Code

Main class started

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);
    }
}

Service interface

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();
}

Fusing realization

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("Fuse, default callback function");
        return "Fuse, default callback function";
    }
}

Service layer

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();
    }


}

Controller

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();
    }

}

Monitor

configuration file

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>

    <groupId>xiejx.cn</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>


</project>

application.yml

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

Code

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("======================Access management hystrix======================");
        System.out.println("http://localhost:4000/hystrix");
        System.out.println("Monitor feign:http://localhost:3001/hystrix.stream");
        System.out.println("======================Access management hystrix======================");

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

Operation and verification

Startup service

  1. Start the Eureka server service (service discovery)
  2. Start Eureka client (Registration Service)
  3. Launch Eureka consumer feign hystrix (service consumption)
  4. Start hystrix dashboard (service monitoring)

Request http://localhost:3001/consumer for service consumption. You can see the normal service consumption
Then close the Eureka client, and you can see that the service is normal and meets the expectation.

Monitoring service

Go to http://localhost:4000/hystrix to view the monitoring panel


Enter the service to be monitored and click Monitor Stream to monitor

If this page cannot be loaded, you can visit http://localhost:3001/consumer again, and then request the page again

Connection address

Posted by Immortal55 on Wed, 27 Nov 2019 08:05:56 -0800