Springcloud、eureka、config、zipkin、hystrix-dashboard、zuul
啓動順序
eureka-server > config-server > zipkin-server > hystrix-dashboard > order > stock > zuul
github:
https://github.com/mengyaoyu/springcloud-practice
eureka-server 註冊中心
訪問地址:http://localhost:8761/
1、application.properties
server.port=8761
spring.application.name=eureka-server
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
2、pom.xml
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3、註解開啓
@EnableEurekaServer
config-server 配置中心
1、application.properties
server.port=8888
spring.application.name=config-server
eureka註冊地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
gitlab配置
git地址
spring.cloud.config.server.git.uri=http://192.168.0.175/java_niwodai_risk/engine-web.git
path
spring.cloud.config.server.git.search-paths=config-repo
spring.cloud.config.server.git.username=yumengyao
spring.cloud.config.server.git.password=yumengyao123
分支
spring.cloud.config.server.git.default-label=config
2、pom.xml
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--config-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3、註解開啓
@EnableEurekaClient
@EnableConfigServer
zipkin-server 監控
訪問地址:http://localhost:9411/
1、application.properties
server.port=9411
spring.application.name=zipkin-server
eureka註冊地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
actuator配置
management.metrics.web.server.auto-time-requests=false
2、pom.xml
<!--zipkin-server-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.11.1</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-log4j2</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.11.1</version>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3、註解開啓
@EnableZipkinServer
@EnableEurekaClient
Hystrix Turbine Dashboard 監控
訪問地址:http://localhost:2001/hystrix
分析地址:http://localhost:2001/turbine.stream
1、application.properties
server.port=2001
spring.application.name=hystrix-dashboard
eureka註冊地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
指定聚合哪些集羣,多個使用","分割,默認爲default
turbine.aggregator.cluster-config=default
表明監控哪些服務
turbine.app-config=order,stock
turbine.cluster-name-expression=new String("default")
這裏和service啓動類裏的 registrationBean.addUrlMappings("/hystrix.stream")一致
turbine.instanceUrlSuffix=hystrix.stream
2、pom.xml
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--turbine-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<!--hystrix-dashboard-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3、註解開啓
@EnableEurekaClient
//斷路由
@EnableHystrix
//斷路由儀表盤
@EnableHystrixDashboard
//Turbine
@EnableTurbine
order 訂單服務
訪問地址:http://localhost:8081
1、bootstrap.properties
先於application.properties加載獲取配置信息
default config-server
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.fail-fast=true
配置動態刷新
management.endpoints.web.exposure.include=refresh,health,info
2、application.properties
server.port=8081
spring.application.name=order
eureka
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
zipkin 地址
spring.zipkin.base-url=http://localhost:9411
zipkin 採集百分比
spring.sleuth.sampler.probability=1.0
hystrix 熔斷開啓
feign.hystrix.enabled=true
3、pom.xml
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4、註解開啓
@EnableFeignClients
@EnableCircuitBreaker
@EnableEurekaClient
5、hystrix.stream
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
6、@RefreshScope 參數刷新
在使用參數的類上添加註解 @RefreshScope
@Value("${app}")獲取參數配置
7、Feign調用和熔斷配置
@FeignClient(value = "stock",fallback = StockServiceHystric.class)
zuul getway 網關
訪問地址:http://localhost:8080/api/order/index 訂單服務
訪問地址:http://localhost:8080/api/stock/index 庫存服務
1、application.properties
server.port=8080
spring.application.name=getway
eureka
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
zipkin 地址
spring.zipkin.base-url=http://localhost:9411
zipkin 採集百分比
spring.sleuth.sampler.probability=1.0
zuul前綴
zuul.prefix=/api
訂單
zuul.routes.order.path=/order/**
zuul.routes.order.serviceId=order
庫存
zuul.routes.stock.path=/stock/**
zuul.routes.stock.serviceId=stock
2、pom.xml
<!--zuul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3、註解開啓
@EnableEurekaClient
@EnableZuulProxy