1.搭建Springcloud项目
项目结构如下:
我们将在base、demo和turbine模块搭建
base(9005)、demo(9002)为正常项目
turbine(8031)为监控模块
2.项目配置
该项目使用nacos作为注册和配置中心,具体使用方法不做累述
1.base模块配置(demo模块相同):
-
maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </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>
-
修改配置文件
management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
-
修改配置文件
@EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker @RestController @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @MapperScan({"com.manage.cloud.mapper"}) public class BaseApplication { @Value("${server.port}") private String port; public static void main(String\[\] args) { SpringApplication.run(BaseApplication.class, args); } @RequestMapping("/hi") @HystrixCommand(fallbackMethod = "hiError") public String home(@RequestParam(value = "name", defaultValue = "evan") String name) { return "hi " + name + " ,i am from port:" + port; } public String hiError(String name) { return "hi,"+name+",sorry,error!"; } }
2. Turbine配置
-
maven依赖,Turbine要排除Eureka依赖,否则启动报错
<!--集群监控--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> <version>1.4.7.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-client</artifactId> </exclusion> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </exclusion> </exclusions> </dependency> <!--注册中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${nacos.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <exclusions> <exclusion> <groupId>org.hdrhistogram</groupId> <artifactId>HdrHistogram</artifactId> </exclusion> </exclusions> </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>
-
配置文件
management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*" hystrix: dashboard: proxy-stream-allow-list: "*" turbine: app-config: base,demo aggregator: clusterConfig: default clusterNameExpression: new String("default") combine-host: true instanceUrlSuffix: default: actuator/hystrix.stream
-
启动类
@SpringBootApplication @EnableDiscoveryClient @RestController @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker @EnableTurbine public class TurbineApplication { public static void main(String\[\] args) { SpringApplication.run(TurbineApplication.class, args); } }
3.测试
1.分别启动demo/base/turbin项目
启动后可以在Nacos查看是否启动成功
2.访问dashboard页面
http://localhost:8031/hystrix
3.查看集群监控
4.调用服务
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END