当前位置: 首页 > news >正文

哈尔滨建设网站成本/营销手段

哈尔滨建设网站成本,营销手段,优化网站聊城,网站建设合同印花税hystrix主要作用在服务消费者,进行应用的保护,当请求的服务请求超时时,做出相应的处理,避免客户端一直进行请求等待,避免在高并发的情况出现服务器死机(请求过多,内存不足) 接下来的…

hystrix主要作用在服务消费者,进行应用的保护,当请求的服务请求超时时,做出相应的处理,避免客户端一直进行请求等待,避免在高并发的情况出现服务器死机(请求过多,内存不足)

接下来的通过一个案例对hystrix的使用进行说明,案例完成的功能:

服务消费者根据Id调用服务提供者的接口,获取User表单的对应的记录,若请求超时则返回id为-1的User记录

一、基于Ribbon

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version><mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version><mysql-connector.version>5.1.39</mysql-connector.version>
</properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.3.5.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId><version>1.3.5.RELEASE</version></dependency></dependencies>

2、application.yml

server:port: 8089
spring:application:name: customer-user
eureka:client: serviceUrl: defaultZone: http://user:zj123@localhost:8761/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
provider-user: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

3、Controller调用类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhuojing.bean.User;@RestController
public class UserController {@Autowiredprivate RestTemplate restTemplate;@GetMapping(value="/simple/{id}",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")@HystrixCommand(fallbackMethod = "findByIdFallback")public User findUserById(@PathVariable Long id){//服务提供者地址 PROVIDER-USERreturn this.restTemplate.getForObject("http://PROVIDER-USER/simple/"+id, User.class);}/*** 断路器模式,当请求的消费者provider-user超时的情况下,就会直接调用此方法,此方法的参数和返回类型必须和findUserById方法一致,请求超时时间为1秒* @param id* @return*/public User findByIdFallback(Long id){User user = new User();user.setId(0L);return user;}}

hystrix的默认的超时时间为1秒,若自定hystrix超时时间有一下两种方式

a、@HystrixCommand注解配置

@HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})

b、在application.yml配置

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

4、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class AppCusRibbonPropertiesTest {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(AppCusRibbonPropertiesTest.class, args);}
}

二、dashboard的使用

1、pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboard</artifactId><version>1.3.5.RELEASE</version></dependency></dependencies>

2、application.yml

server:
port: 8030

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@EnableHystrixDashboard
@SpringBootApplication
public class DashboardApplication {public static void main(String[] args) {SpringApplication.run(DashboardApplication.class, args);}
}

启动后访问:http:/ /localhost:8030/hystrix 进入首页,在地址栏上输入http:/ /localhost:8089/hystrix.stream(hystrix.stream)进行监控,在使用了hystrix的服务调用后才有数据。

三、Turbine的使用,Turbine是对微服务集群的监听

1、pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-turbine</artifactId><version>1.3.5.RELEASE</version></dependency></dependencies>

2、application.yml

server:port: 8031
spring:application:name: microservice-hystrix-turbine
eureka:client:serviceUrl:defaultZone: http://user:zj123@localhost:8761/eurekainstance:prefer-ip-address: true
turbine:aggregator:clusterConfig: default  #默认defualt,只有一个服务时候可以写服务名称的大写CUSTOMER-USERappConfig: customer-userclusterNameExpression: "'default'"

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;@EnableTurbine
@SpringBootApplication
public class TurbineApplication {public static void main(String[] args) {SpringApplication.run(TurbineApplication.class, args);}
}

hystrix 主页面中将localhost:8031/turbine.stream?cluster=CUSTOMER-USER填入,便可进行数据的图像化。

http://www.lbrq.cn/news/1342549.html

相关文章:

  • 怎么网站做二维码/黑科技引流工具
  • 专业的网站建设/sem优化技巧
  • 做兼职什么网站好/专业seo站长工具
  • 俄罗斯网站模版/国内新闻今日头条
  • 大型服装网站建设/中国互联网域名注册服务机构
  • 广告设计网站排行榜前十名有哪些/网站营销网站营销推广
  • 重庆建设部网站/seo综合查询是什么
  • 网站建设教程自学/海口seo计费
  • 做推送的网站除了秀米还有/鼓楼网站seo搜索引擎优化
  • 微博账号滚动图网站怎么做/seo优化公司信
  • 网站原型图大小/最新消息今天的新闻
  • 前端做项目网站/如何获取网站的seo
  • 衢州网站建设有限公司/整站优化seo
  • 电子商务网站建设可用性/网页设计制作网站代码
  • 网站开发要学多久/济南疫情最新消息
  • 甘肃省建设银行网站/永久免费开网店app
  • 南昌电商网站设计/优化营商环境的意义
  • 网站建设与规划结论/网站推广方法有哪些
  • 上饶网站优化/微信推广文案
  • 游戏大全免费版入口/快排seo软件
  • 木门行业做网站有什么好处/全国疫情最新情况公布
  • 网站项目建设的组织机构/seo哪个软件好
  • 武汉网站建设027/网站建设设计
  • 做视频网站的公司有哪些/百度后台登录
  • 什么叫网站根目录/企业策划咨询公司
  • 免费网站如何做推广方案/国产搜什么关键词最好看
  • 移动端网站模板怎么做的/seo文案范例
  • mvc5 网站开发之学 pdf/google谷歌搜索
  • 商务网站建设的一般流程图/目录搜索引擎有哪些
  • 彩票网站开发制作需要什么/外贸网站免费推广
  • LeetCode每日一题,2025-8-10
  • 深入浅出DBSCAN:基于密度的聚类算法详解与Python实战
  • B.10.01.6-DDD领域驱动设计:从理论到落地的完整指南
  • 分析报告:基于字节连续匹配技术的KV缓存共享实施可能性及其扩展
  • Android 开发问题:The specified child already has a parent.
  • 时序分解 | MATLAB实现SAO-VMD雪消融算法优化变分模态分解