springcloud learning 8 - hystrix

Learn from the station b springcloud, now summarize the summary removal of a small error appearing in the video, some of the error-prone places were reminded
b outbound links: https://www.bilibili.com/video/av55304977

data link:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
extraction code: 21ru

On a link: https://blog.csdn.net/qq_40893824/article/details/103336449

The following list summarizes: sub-project → pom → application → → entity class interfaces → handler → startup class

Implementation details:
1. Create a module module, named hystrix
2. The module's POM file, add the code:

<dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.0.7.RELEASE</version>
        </dependency>

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

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

    </dependencies>

3. Chong application.yml in resource, add code:

server:
  port: 8060
spring:
  application:
    name: hystrix
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true
management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'

4. In java package record com.southwind, creating class HystrixApplication.java in southwind start, fill in the code:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
/*
@EnableCircuitBreaker:声明启用数据监控
@EnableHystrixDashboard:声明启用可视化数据监控
*/
public class HystrixApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixApplication.class,args);
    }
}

5.southwind next record package entity, to which the replication Student
create packages feign under 6.southwind, the creation of interfaces: FeignProviderClient

package com.southwind.feign;

import com.southwind.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collection;

@FeignClient(value = "provider")
public interface FeignProviderClient {

    //声明接口
    @GetMapping("/student/findAll")
    public Collection<Student> findAll();

    @GetMapping("/student/index")
    public  String index();
}

7. The package record in southwind controller, create FeignHandler.java
add code:

package com.southwind.controller;

import com.southwind.entity.Student;
import com.southwind.feign.FeignProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/hystrix")
public class HystrixHandler {

    @Autowired
    private FeignProviderClient feignProviderClient;

    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return feignProviderClient.findAll();
    }

    @GetMapping("/index")
    public String index(){
        return feignProviderClient.index();
    }
}

8. Check:
entering http: // localhost: 8060 / actuator / hystrix.stream
Here Insert Picture Description
enter http: // localhost: 8060 / hystrix / index
Here Insert Picture Description
also found http: // localhost: 8060 / actuator / hystrix.stream in:
Here Insert Picture Description
visual interfaces:
http: // localhost: 8060 / hystrix
Here Insert Picture Description
Here Insert Picture Description

On a link: https://blog.csdn.net/qq_40893824/article/details/103336449

Published 42 original articles · won praise 2 · Views 1188

Guess you like

Origin blog.csdn.net/qq_40893824/article/details/103439025