1. 程式人生 > >spring cloud (六) 將一個普通的springcloud項目 非feign或ribbon項目,改造成turbine可聚合監聽的項目

spring cloud (六) 將一個普通的springcloud項目 非feign或ribbon項目,改造成turbine可聚合監聽的項目

static utf [] snapshot hello plugins reporting poi map

改造之前一個項目 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監控面板上

spring cloud (六) 將一個普通的springcloud項目 非feign或ribbon項目,改造成turbine可聚合監聽的項目