十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Hystrix作为一款服务治理框架,被使用的很多,但是官方已经不再维护,取而代之的是Resilience4j框架。
创新互联建站是一家专业提供牡丹企业网站建设,专注与做网站、成都做网站、HTML5、小程序制作等业务。10年已为牡丹众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。所以本文简单介绍Hystrix的使用,把重点放在下一篇的Resilience4j的讲解上。
1、Eureka服务注册和发现
2、一个服务提供者waiter-service
3、基于Feign实现的调用者customer-service,调用waiter-service的方法,服务治理在customer-service上使用。
4、Hsytrix dashboard 用于监控服务调用情况。
1、启动文件:
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import java.util.TimeZone;
@SpringBootApplication
@EnableJpaRepositories
@EnableCaching
@EnableDiscoveryClient
public class WaiterServiceApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(WaiterServiceApplication.class, args);
}
}
2、POM文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
jane.spring.com
waiter-service
0.0.1-SNAPSHOT
waiter-service
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-cache
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
io.micrometer
micrometer-registry-prometheus
org.joda
joda-money
1.0.1
org.jadira.usertype
usertype.core
6.0.1.GA
com.fasterxml.jackson.datatype
jackson-datatype-hibernate5
2.9.8
org.apache.commons
commons-lang3
com.h3database
h3
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
3、配置文件:
spring.application.name=waiter-service
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
info.app.author=DigitalSonic
info.app.encoding=@project.build.sourceEncoding@
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
server.port=8091
4、方法文件
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/coffee")
@Slf4j
public class CoffeeController {
@Autowired
private CoffeeService coffeeService;
@GetMapping("/{id}")
public Coffee getById(@PathVariable Long id) {
Coffee coffee = coffeeService.getCoffee(id);
log.info("Coffee {}:", coffee);
return coffee;
}
}
5、启动后Eureka效果
1、启动文件
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
@SpringBootApplication
@Slf4j
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class CustomerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerServiceApplication.class, args);
}
@Bean
public CloseableHttpClient httpClient() {
return HttpClients.custom()
.setConnectionTimeToLive(30, TimeUnit.SECONDS)
.evictIdleConnections(30, TimeUnit.SECONDS)
.setMaxConnTotal(200)
.setMaxConnPerRoute(20)
.disableAutomaticRetries()
.setKeepAliveStrategy(new CustomConnectionKeepAliveStrategy())
.build();
}
}
2、pom文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
jane.spring.com
customer-service
0.0.1-SNAPSHOT
customer-service
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.1.1.RELEASE
org.springframework.cloud
spring-cloud-starter-openfeign
io.github.openfeign
feign-httpclient
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.joda
joda-money
1.0.1
org.apache.commons
commons-lang3
org.apache.httpcomponents
httpclient
4.5.7
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
3、配置文件
server.port=8090
spring.application.name=customer-service
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
feign.client.config.default.connect-timeout=500
feign.client.config.default.read-timeout=500
feign.hystrix.enabled=true
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
4、控制器url
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping("/customer")
@Slf4j
public class CustomerController {
@Autowired
private CoffeeService coffeeService;
@GetMapping("/coffee")
@HystrixCommand(fallbackMethod = "fallbackMethod")
public Coffee getCoffee() {
Coffee list = coffeeService.getById(1l);
log.info("Read coffee: {} coffee", list);
return list;
}
public Coffee fallbackMethod() {
log.warn("Fallback ......");
return null;
}
}
5、fegin客户端
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient(name = "waiter-service", contextId = "coffee",
qualifier = "coffeeService", path="/coffee",
fallback = FallbackCoffeeService.class)
// 如果用了Fallback,不要在接口上加@RequestMapping,path可以用在这里
public interface CoffeeService {
@GetMapping("/{id}")
Coffee getById(@PathVariable Long id);
}
6、运行后,eureka效果
1、启动文件:
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardDemoApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardDemoApplication.class, args);
}
@Bean
public ServletRegistrationBean ServletRegistrationBeangetServlet() {
HystrixMetricsStreamServlet streamServlet =new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean =new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
2、配置文件
server.port=9090
3、pom文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
jane.spring.cloud
hystrix-dashboard-demo
0.0.1-SNAPSHOT
hystrix-dashboard-demo
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
3、启动后效果如下
4、将地址输入上述文本框,并点击Monitor Stream按钮
http://127.0.0.1:8090/actuator/hystrix.stream
5、调用如下请求:
6、查看Hystrix-dashboard 效果
7、停止waiter-service服务,再次调用,可以看到错误
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。