spring cloud 将一个普通的springcloud项目 非feign或ribbon项目,改造成turbine可聚合监听的项目
Posted 冬天不眠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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监控面板上
以上是关于spring cloud 将一个普通的springcloud项目 非feign或ribbon项目,改造成turbine可聚合监听的项目的主要内容,如果未能解决你的问题,请参考以下文章
spring cloud 将一个普通的springcloud项目 非feign或ribbon项目,改造成turbine可聚合监听的项目