• spring cloud (六) 将一个普通的springcloud项目 非feign或ribbon项目,改造成turbine可聚合监听的项目


    改造之前一个项目 service-a

    1 pom.xml添加如下

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

    完整的pom

    <?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.example</groupId>
        <artifactId>Spring-Cloud-Service-A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>Spring-Cloud-Service-A</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            
            
        </dependencies>
    
        <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>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

    2 启动类添加注解 @EnableHystrix

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

    3 配置文件添加如下配置

    server.port=8082
    spring.application.name=service-a
    eureka.client.service-url.defaultZone=http://localhost:8080/eureka/
    
    eureka.instance.metadata-map.cluster=main
    
    management.server.port=9003
    management.endpoints.web.exposure.include="*"
    management.endpoints.web.cors.allowed-origins="*"
    management.endpoints.web.cors.allowed-methods="*"

    4 编写一个测试类  添加需要熔断的方法

    @RestController
    public class HystrixTestC {
    @GetMapping("/hget")
    @HystrixCommand(fallbackMethod="getFallback")
    public String getHystrixTest(String message) {
    if("error".equals(message)) {
    System.err.println(1/0);
    }
    return "hello world:"+message;
    }
    public String getFallback(String message) { // 此时方法的参数 与get()一致
    return "hello world 1/0:"+message;
    }
    }

    5 启动项目

    6 修改turbine项目中配置文件  加入service-a

    server.port=7032
    spring.application.name=turbine
    
    eureka.client.service-url.defaultZone=http://127.0.0.1:8080/eureka/
    
    turbine.app-config=feign-consumer,service-a
    # dan ge setting
    #turbine.cluster-name-expression=new String("default")
    #turbine.combine-host-port=true
    
    #ji qun setting 
    turbine.aggregator.clusterConfig=MAIN
    turbine.cluster-name-expression=metadata['cluster']

    7 重启turbine项目

    8 访问 

     服务顺利出现在turbine监控面板上

  • 相关阅读:
    ETL利器Kettle实战应用解析系列一【Kettle使用介绍】
    彻底理解webservice SOAP WSDL
    5天玩转C#并行和多线程编程 —— 第三天 认识和使用Task
    5天玩转C#并行和多线程编程 —— 第一天 认识Parallel
    Data Leakage 因果性
    一张图,关于 Bayes error rate,贝叶斯错误率等的分析
    玩转Node.js单元测试
    Fundebug上线Node.js错误监控啦
    聊聊"jQuery is not defined"
    深究WeixinJSBridge未定义之因
  • 原文地址:https://www.cnblogs.com/syscn/p/9741097.html
Copyright © 2020-2023  润新知