首页 文章 精选 留言 我的

精选列表

搜索[分布式SQL],共10000篇文章
优秀的个人博客,低调大师

spring cloud微服务分布式云架构--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/eureka instance: prefer-ip-address: true instance-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 { @Autowired private RestTemplate restTemplate; @GetMapping(value="/simple/{id}",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8") @HystrixCommand(fallbackMethod = "findByIdFallback") public User findUserById(@PathVariable Long id){ //服务提供者地址 PROVIDER-USER return 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 @LoadBalanced public 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/eureka instance: prefer-ip-address: true turbine: aggregator: clusterConfig: default #默认defualt,只有一个服务时候可以写服务名称的大写CUSTOMER-USER appConfig: customer-user clusterNameExpression: "'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填入,便可进行数据的图像化。

优秀的个人博客,低调大师

分布式架构实战--ActiveMQ的安装与使用(单节点)

具体内容请参考样例代码和视频教程: http://www.roncoo.com/course/view/85d6008fe77c4199b0cdd2885eaeee53 IP:192.168.4.101 环境:CentOS 6.6、JDK7 1、 安装JDK并配置环境变量(略) JAVA_HOME=/usr/local/java/jdk1.7.0_72 2、 下载Linux版的ActiveMQ(当前最新版apache-activemq-5.11.1-bin.tar.gz) $ wget http://apache.fayea.com/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz 3、 解压安装 $ tar -zxvf apache-activemq-5.11.1-bin.tar.gz $ mv apache-activemq-5.11.1 activemq-01 如果启动脚本activemq没有可执行权限,此时则需要授权(此步可选) $ cd /home/wusc/activemq-01/bin/ $ chmod 755 ./activemq 4、 防火墙中打开对应的端口 ActiveMQ需要用到两个端口 一个是消息通讯的端口(默认为61616) 一个是管理控制台端口(默认为8161)可在conf/jetty.xml中修改,如下: <bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start"> <!-- the default port number for the web console --> <property name="host" value="0.0.0.0"/> <property name="port" value="8161"/> </bean> # vi /etc/sysconfig/iptables 添加: -A INPUT -m state --state NEW -m tcp -p tcp --dport 61616 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8161 -j ACCEPT 重启防火墙: # service iptables restart 5、 启动 $ cd /home/wusc/activemq-01/bin $ ./activemq start 6、 打开管理界面:http://192.168.4.101:8161 默认用户名和密码为:admin/admin 登录后进入 7、 安全配置(消息安全) ActiveMQ如果不加入安全机制的话,任何人只要知道消息服务的具体地址(包括ip,端口,消息地址[队列或者主题地址],),都可以肆无忌惮的发送、接收消息。关于ActiveMQ安装配置http://activemq.apache.org/security.html ActiveMQ的消息安全配置策略有多种,我们以简单授权配置为例: 在conf/activemq.xml文件中在broker标签最后加入以下内容即可: $ vi /home/wusc/activemq-01/conf/activemq.xml <plugins> <simpleAuthenticationPlugin> <users> <authenticationUser username="wusc" password="wusc.123" groups="users,admins"/> </users> </simpleAuthenticationPlugin> </plugins> 定义了一个wusc用户,密码为wusc.123,角色为users,admins 设置admin的用户名和密码: $ vi /home/wusc/activemq-01/conf/jetty.xml <bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint"> <property name="name" value="BASIC" /> <property name="roles" value="admin" /> <property name="authenticate" value="true" /> </bean> 确保authenticate的值为true(默认) 控制台的登录用户名密码保存在conf/jetty-realm.properties文件中,内容如下: $ vi /home/wusc/activemq-01/conf/jetty-realm.properties # Defines users that can access the web (console, demo, etc.) # username: password [,rolename ...] admin: wusc.123, admin 注意:用户名和密码的格式是 用户名 : 密码 ,角色名 重启: $ /home/wusc/activemq-01/bin/activemq restart 设置开机启动: # vi /etc/rc.local 加入以下内容 ## ActiveMQ su - wusc -c '/home/wusc/activemq-01/bin/activemq start' 8、 MQ消息生产者也与消息消费者的Demo样例讲解与演示 关注公众号:

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码