使用SpringCloud服务熔断hystrix及服务监控dashboard中遇到的问题

版权声明:本文为博主原创文章,欢迎转载并注明出处 https://blog.csdn.net/wender/article/details/86248851

SpringCloud版本为 Dalston.SR1

创建maven工程:billparent-all

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>com.zkld.bill</groupId>
    <artifactId>billparent-all</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>bill-recharge-rest-provider-8001-hystrix</module>
        <module>bill-recharge-rest-consumer-hystrix-dashboard-9001</module>
    </modules>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
    </properties>


    <dependencyManagement>

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.0.4</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.31</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.0</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>


        </dependencies>

    </dependencyManagement>

    <build>
        <finalName>bill_recharge</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimit>$</delimit>
                    </delimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

创建熔断器module:bill-recharge-rest-provider-8001-hystrix

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>
    <parent>
        <groupId>com.zkld.bill</groupId>
        <artifactId>billparent-all</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>bill-recharge-rest-provider-8001-hystrix</artifactId>
    <name>bill-recharge-rest-provider-8001-hystrix</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <!-- 断路器 hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <!-- 将微服务provider侧注册进eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>
RechargeController
package com.zkld.bill.recharge.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 */
@RestController
@RequestMapping(value="/recharge")
public class RechargeController {


    /**
     * http://localhost:8001/recharge/notify/get/1
     *
     * {"id":1,"userCode":"1001","operator":"UU","chargeTime":"2019-01-02 10:36:55","phone":"12312345678","txOrderNo":"tx_order_no_abc","orderNo":"order_no_abc","amount":200,"retResult":"100","sign":"sign_abc","nonstr":"noStr","createTime":"2019-01-02 10:36:56","updateTime":"2019-01-02 10:36:58"}
     * @param id
     * @return
     */
    @RequestMapping(value="/notify/get/{id}", method = { RequestMethod.GET})
    @HystrixCommand(fallbackMethod = "receiveNotifyGet_hystrix")
    public String receiveNotifyGet(@PathVariable("id") Long id){
        System.out.println("execute receiveNotifyGet("+id+") in provider-8001_hystrix");
        String rn = "{'id':"+id+",'msg':'hello world'}";
        if(null == rn) {
            throw new RuntimeException("未找到ID为" + id + "的信息");
        }
        return rn;
    }

    public String receiveNotifyGet_hystrix(@PathVariable("id") Long id){
        System.out.println("execute receiveNotifyGet_hystrix("+id+") in provider-8001_hystrix");
        String rn = "{'id':"+id+",'msg':'未找到ID为" + id + "的信息'}";
        return rn;
    }


}
RechargeProvider8001Hystrix_App
package com.zkld.bill.recharge;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication(scanBasePackages = {"com.zkld.bill.recharge", "com.zkld.bill.dao", "com.zkld.bill.service"})
@MapperScan(value = "com.zkld.bill.dao")
@EnableEurekaClient       //服务注册
@EnableDiscoveryClient    //服务发现
@EnableCircuitBreaker     //对hystrixR熔断机制的支持
@EnableHystrix
public class RechargeProvider8001Hystrix_App {

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

}

application.yml

server:
  port: 8001

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.zkld.bill.facade.entity         # 所有Entity别名类所在包
  mapper-locations:
    - classpath:mybatis/mapper/**/*.xml                     # mapper映射文件

spring:
  application:
    name: billRechargeProvider                              #不能包括下划线
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.jdbc.Driver                # mysql驱动包
#    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/bill                   # 数据库名称
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

#客户端注册进eureka服务列表内
eureka:
  client:
    service-url:
      defaultZone: http://localhost:87611/eureka  #单机配置
  instance:
    instance-id: bill_recharge_provider_8001_hystrix     #自定义服务名称
    prefer-ip-address: true                              #访问路径可以显示IP

创建监控module:bill-recharge-rest-consumer-hystrix-dashboard-9001

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">
    <parent>
        <artifactId>billparent-all</artifactId>
        <groupId>com.zkld.bill</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>bill-recharge-rest-consumer-hystrix-dashboard-9001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- Ribbon相关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- feign相关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- hystrix和 hystrix-dashboard相关 -->
        <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>
    </dependencies>

</project>
RechargeConsumerHystrixDashboard9001_App
package com.zkld.bill.recharge;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class RechargeConsumerHystrixDashboard9001_App {
    public static void main(String[] args) {
        SpringApplication.run(RechargeConsumerHystrixDashboard9001_App.class, args);
    }
}

application.yml

server:
  port: 9001

验证

http://localhost:8001/recharge/notify/get/1

可以正常访问

http://localhost:8001/hystrix.stream

火狐提示下载,但下载时,发现文件一直在下载,不断变化

http://localhost:9001/hystrix

遇到的问题:

http://localhost:8001/hystrix.stream

报Whitelabel Error Page  404等无效页面

hystrix.stream

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Jan 08 19:58:28 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

解决办法:删除module bill-recharge-rest-provider-8001-hystrix pom文件中如下不一致的配置

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


.................

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

SpringCloud开发常见的坑就是版本匹配的问题,每个module的pom文件,SpringCloud版本和SpringBoot版本要统一,各组件版本也要按官方示例的版本(包括group,artifact,version完全一致)

猜你喜欢

转载自blog.csdn.net/wender/article/details/86248851
今日推荐