spring-boot版本:2.0.3 ,spring-cloud版本:Finchley.RELEASE
eureka-server:
配置文件:
spring.application.name=spring-cloud-eureka
server.port=1001
#表示是否将自己注册到Eureka Server,默认为true
eureka.client.register-with-eureka=false
#表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=false
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
# 默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka
启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaSerberApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaSerberApplication.class, args);
}
}
eureka-provider-1:
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
p配置文件:
spring.application.name=spring-cloud-producer
server.port=1002
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka
f服务:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is 1 first messge";
}
}
复制eureka-provider-1到eureka-provider-2,修改端口为1004.
eureka-consumer-hystrix-dashboard:
启动类:
@SpringBootApplication
//启用服务注册与发现
@EnableDiscoveryClient
//:启用feign进行远程调用
@EnableFeignClients
@RestController
//启用Hystrix Dashboard
@EnableHystrixDashboard
//启用熔断
@EnableCircuitBreaker
public class EurekaConsumerApplication {
@Autowired
HelloRemote helloRemote;
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
// @RequestMapping("/hello/{name}")
// public String index(@PathVariable("name") String name) {
// return helloRemote.hello(name);
// }
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
配置文件:
#在注册中心显示的app名称
spring.application.name=spring-cloud-consumer
server.port=1003
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka
#开启熔断机制
feign.hystrix.enabled=true
#访问地址
#http://localhost:1003/hystrix
gateway-service-zuul-simple:
启动类:
@SpringBootApplication
//支持网关路由。
@EnableZuulProxy
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
配置文件:
spring.application.name=gateway-service-zuul
server.port=8888
#这里的配置表示,访问/it/** 直接重定向到http://www.ityouknow.com/**
#所要配置的路径可以指定一个正则表达式来匹配路径,
# 因此,/user/*只能匹配一级路径,
# 但是通过/user/**可以匹配所有以/user/开头的路径
#zuul.routes.baidu.path=/it/**
#zuul.routes.baidu.url=http://www.ityouknow.com/
#启动spring-cloud-producer,重新启动gateway-service-zuul-simple,
# 访问:http://localhost:8888/hello/hello?name=%E5%B0%8F%E6%98%8E,
#zuul.routes.hello.path=/hello/**
#zuul.routes.hello.url=http://localhost:1002/
#但是如果后端服务多达十几个的时候,每一个都这样配置也挺麻烦的,
# spring cloud zuul已经帮我们做了默认配置。默认情况下,
# Zuul会代理所有注册到Eureka Server的微服务,
# 并且Zuul的路由规则如下:http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的serviceId/**会被转发到serviceId对应的微服务。
#http://localhost:8888/spring-cloud-producer/hello?name=%E5%BC%A0%E4%B8%89
#以下两行的配置,会在访问http://localhost:8888/producer/hello?name=sasa
# 或者http://localhost:8888/spring-cloud-producer/hello?name=sasa
# 的时候返回正确结果
#以下两行如果注释的话,则访问http://localhost:8888/spring-cloud-producer/hello?name=sasa 也OK
zuul.routes.api-a.path=/producer/**
zuul.routes.api-a.serviceId=spring-cloud-producer
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka
测试:启动 eureka-server,eureka-provider-1,eureka-provider-2,eureka-consumer-hystrix-dashboard,
gateway-service-zuul-simple.
进入eureka-server控制台:http://localhost:1001/
防蚊浏览器地址:http://localhost:8888/producer/hello?name=sasa
发现返回结果在hello sasa,this is 2 first messge 和hello sasa,this is 1 first messge 反复切换,表明:eureka-provider-1,eureka-provider-2已经构建成一个集群,实现了负载均衡。
浏览器访问:http://localhost:1003/hystrix,输入http://localhost:8888/hystrix.stream,查看统计报表页:
因为我用的版本为:spring-boot版本:2.0.3 ,spring-cloud版本:Finchley.RELEASE,所以gateway-service-zuul-simple项目中启动类中的bean一定要加,不然在查看统计报表页的时候会报错:Unable to connect to Command Metric Stream.