step1:修改hosts的ip地址映射,创建eureka集群
可参考:https://www.cnblogs.com/noneplus/p/11374883.html
step2:创建服务提供者
- pom依赖
4.0.0
zkrun.top
msc-provider-5001
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
1.8
Greenwich.SR2
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
- application.yaml
server:
port: 5001
spring:
application:
name: msc-provider #应用名称
eureka:
client:
service-url:
defaultZone: http://eureka4001.com:4001/eureka/,http://eureka4002.com:4002/eureka/,http://eureka4003.com:4003/eureka/
instance:
instance-id: msc-provider-5001
prefer-ip-address: true #访问路径可以显示IP地址
- controller
package zkrun.top.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping(value = "/info/get")
public String response()
{
return "msc_provider_5001";
}
}
- 主类
package zkrun.top;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class App_msc_provider_5001
{
public static void main(String[] args)
{
SpringApplication.run(App_msc_provider_5001.class, args);
}
}
step3:创建服务消费者【重要】
- pom参考
4.0.0
zkrun.top
msc-consumer-9001
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
1.8
Greenwich.SR2
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
- application.yaml
server:
port: 9001
spring:
application:
name: consumer-9001
eureka:
client:
serviceUrl:
defaultZone: http://eureka4001.com:4001/eureka/,http://eureka4002.com:4002/eureka/,http://eureka4003.com:4003/eureka/
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
使用Feign作为客户端调用工具
service接口
package zkrun.top.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("msc-provider") //@FeignClient("服务名") @Component public interface Hystrix_FeignService { @RequestMapping(value = "/info/get") public String request(); }
controller
package zkrun.top.controller; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import zkrun.top.service.Hystrix_FeignService; @RestController public class Hystrix_FeignController { @Autowired Hystrix_FeignService hystrix_feignService; @RequestMapping(value = "/feign/info/get",method = RequestMethod.GET) @HystrixCommand(fallbackMethod = "hystrix_fallback") public String request() { return this.hystrix_feignService.request(); } public String hystrix_fallback() { return "当前服务故障,服务熔断已启动!"; } }
主类(注解有点多)
package zkrun.top; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker @EnableFeignClients public class App_Consumer_9001 { public static void main(String[] args) { SpringApplication.run(App_Consumer_9001.class, args); } }
step4:Turbine监控服务
pom(spring-cloud-starter-netflix-turbine)
4.0.0 zkrun.top msc-turbine-dashboard-8001 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE 1.8 Greenwich.SR2 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-netflix-hystrix org.springframework.cloud spring-cloud-starter-netflix-hystrix-dashboard org.springframework.cloud spring-cloud-starter-netflix-turbine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin application.yaml
server:
port: 8001
spring:
application:
name: service-turbine
eureka:
client:
serviceUrl:
defaultZone: http://eureka4001.com:4001/eureka/,http://eureka4002.com:4002/eureka/,http://eureka4003.com:4003/eureka/
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
turbine:
app-config: consumer-9002,consumer-9001
aggregator:
clusterConfig: default
clusterNameExpression: new String("default")
combine-host: true
instanceUrlSuffix:
default: actuator/hystrix.stream
主类(注解)
package zkrun.top.zkrun.top; 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; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker @EnableTurbine public class TurbineService { public static void main(String[] args) { SpringApplication.run(TurbineService.class,args); } }
测试
启动eureka集群,provider,两个consumer
客户端正常访问
登录监控面板:
http://localhost:8001/hystrix/
使用Jmeter做访问测试:
- 线程数1000
- 填写http访问相关信息
查看监控面板反应:
代码参考:
https://github.com/HCJ-shadow/Turbine-Dashboard