hystrix微服务部署

embedded/2024/10/5 20:07:59/

目录

一.启动nacos和redis

1.查看是否有nacos和redis

 二.开始项目

hystrix1%E5%B7%A5%E7%A8%8B%EF%BC%88%E4%BF%AE%E6%94%B9%E4%B8%80%E4%B8%8B%E5%B7%A5%E7%A8%8B%E7%9A%84%E6%B3%A8%E5%86%8C%E5%90%8D%E5%AD%97%EF%BC%89-toc" style="margin-left:40px;">1.hystrix1工程(修改一下工程的注册名字)

2.运行登录nacos网站查看运行效果(默认密码nacos,nacos) 

hystrix2%E5%B7%A5%E7%A8%8B-toc" style="margin-left:40px;">3.开启第二个项目 hystrix2工程

hystrix2%E5%B7%A5%E7%A8%8B%C2%A0%EF%BC%88%E6%A8%A1%E6%8B%9F%E7%86%94%E6%96%AD%EF%BC%89-toc" style="margin-left:40px;">4.关闭第二个项目 hystrix2工程 (模拟熔断)

hystrix-toc" style="margin-left:0px;">三.应用hystrix

hystrix1%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96-toc" style="margin-left:0px;">1.在第一个工程里hystrix1添加依赖

2.开启Hystrix

hystrix1%E7%9A%84%E5%B7%A5%E5%85%B7%E7%B1%BBResult-toc" style="margin-left:40px;"> 3.修改第一个项目hystrix1的工具类Result

hystrix1%E7%9A%84%E6%8E%A7%E5%88%B6%E7%B1%BB-toc" style="margin-left:40px;">4.修改第一个项目hystrix1的控制类

hystrix1%E7%9A%84application.yml%E6%96%87%E4%BB%B6-toc" style="margin-left:40px;"> 5.修改第一个项目hystrix1的application.yml文件

6.先同时启动两个项目,在postman进行测试 

3.6.1连接成功的情况

3.6.2连接失败的情况(提示) 

四.添加仪表盘(很少用到,了解)

hystrix1%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96-toc" style="margin-left:40px;">1.在第一个工程里hystrix1添加依赖

 2.添加配置类(固定写法)

3.启动类添加注解@EnableHystrixDashboard

4启动项目,访问如下地址

​编辑 五.我的项目结构

hystrix1-toc" style="margin-left:0px;">1.第一个工程hystrix1

hystrix2-toc" style="margin-left:0px;">2.第一个工程hystrix2


 

一.启动nacos和redis

1.查看是否有nacos和redis

docker ps -a

2.启动nacos和redis 

docker start nacos
docker start redis-6379
docker ps

 二.开始项目

这里用的项目过程在我的另一个博客中OpenFeign微服务部署-CSDN博客

我是粘贴复制后重写的名字,hystrix1对应SpringSessiondemo,hystrix2对应SpringSessiondemo1,

hystrix2工程(没有代码改变)先启动之后断开(模拟宕机),

hystrix1%E5%B7%A5%E7%A8%8B%EF%BC%88%E4%BF%AE%E6%94%B9%E4%B8%80%E4%B8%8B%E5%B7%A5%E7%A8%8B%E7%9A%84%E6%B3%A8%E5%86%8C%E5%90%8D%E5%AD%97%EF%BC%89">1.hystrix1工程(修改一下工程的注册名字)

2.运行登录nacos网站查看运行效果(默认密码nacos,nacos) 

hystrix2%E5%B7%A5%E7%A8%8B">3.开启第二个项目 hystrix2工程

用postman测试

hystrix2%E5%B7%A5%E7%A8%8B%C2%A0%EF%BC%88%E6%A8%A1%E6%8B%9F%E7%86%94%E6%96%AD%EF%BC%89">4.关闭第二个项目 hystrix2工程 (模拟熔断)

hystrix">三.应用hystrix

hystrix1%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96" style="background-color:transparent;">1.在第一个工程里hystrix1添加依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2.开启Hystrix

在第一个工程里hystrix1的启动类上添加开启Hystrix的注解

@EnableHystrix

 此时完成上述两个操作后,再次关闭第二个工程里hystrix2,页面也是报错,但不是“连接超时”的错误,而是“熔断类型”的错误。为了让用户体验度好一些,报错信息不暴露给用户,我们完成下面的编码。

hystrix1%E7%9A%84%E5%B7%A5%E5%85%B7%E7%B1%BBResult"> 3.修改第一个项目hystrix1的工具类Result

package com.jr.util;import lombok.Data;import java.util.HashMap;
import java.util.Map;@Data
public class Result {private Integer code;private String message;private Map<String, Object> map = new HashMap<>();private Result() {}public static Result ok() {Result r = new Result();r.setCode(ResultCode.SUCCESS.getCode());r.setMessage(ResultCode.SUCCESS.getMessage());return r;}public static Result error() {Result r = new Result();r.setCode(ResultCode.ERROR.getCode());r.setMessage(ResultCode.ERROR.getMessage());return r;}public Result setMessage(String message) {this.message = message;return this;}public Result put(String key, Object value) {map.put(key, value);return this;}public Object get(String key) {return map.get(key);}}

 

hystrix1%E7%9A%84%E6%8E%A7%E5%88%B6%E7%B1%BB">4.修改第一个项目hystrix1的控制类

package com.jr.controller;import com.jr.entry.UserDto;
import com.jr.service.IUserService;
import com.jr.util.Result;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate IUserService userService;@GetMapping@HystrixCommand(fallbackMethod = "infoHystrix") //一旦熔断了,就去执行infoHystrix方法。public Result info() {UserDto user = userService.info();return Result.ok().put("data", user);}public Result infoHystrix() {return Result.error().setMessage("被熔断了");}@GetMapping("/{id}")public Result id(@PathVariable String id) {  //url传值UserDto userDto = userService.id(id);return Result.ok().put("data", userDto);}@PostMapping("/add")public Result add(@RequestBody UserDto user) { //对象传值UserDto userDto = userService.add(user);return Result.ok().put("data", userDto);}
}

hystrix1%E7%9A%84application.yml%E6%96%87%E4%BB%B6"> 5.修改第一个项目hystrix1的application.yml文件

向其中添加以下代码

feign:hystrix:enabled: true

6.先同时启动两个项目,在postman进行测试 

3.6.1连接成功的情况

3.6.2连接失败的情况(提示) 

四.添加仪表盘(很少用到,了解)

1.在第一个工程里hystrix1添加依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 2.添加配置类(固定写法)

package com.jr.config;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.servlet.http.HttpServlet;@Configuration
public class HystrixConfig {@Beanpublic ServletRegistrationBean<HttpServlet> httpServletServletRegistrationBean() {ServletRegistrationBean<HttpServlet> result = new ServletRegistrationBean<>(new HystrixMetricsStreamServlet());result.addUrlMappings("/actuator/hystrix.stream");return result;}}

3.启动类添加注解@EnableHystrixDashboard

4启动项目,访问如下地址

地址栏访问的地址:http://localhost:100/hystrix

地址2:http://localhost:100/actuator/hystrix.stream

点击下面的Monitor Stream

 一开始建立的时候全是0,我们需要去访问我们的项目

 五.我的项目结构

hystrix1">1.第一个工程hystrix1

hystrix2">2.第一个工程hystrix2


http://www.ppmy.cn/embedded/123523.html

相关文章

论文 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust

这篇论文研究了使用提示 (Prompting) 方法微调预训练语言模型&#xff0c;以提高其在对抗样本攻击下的鲁棒性。论文的主要贡献如下&#xff1a; 1.MVP 比 MLP-FT 更鲁棒&#xff1a; 论文比较了 MVP (Model-tuning Via Prompts) 和传统的 MLP-FT (Fine-tuning with an MLP head…

鸿蒙HarmonyOS NEXT 电商APP开发,打造你的专属购物商城

2024年年初&#xff0c;鸿蒙HarmonyOS Next星河版强势发布&#xff0c;随着鸿蒙系统的普及和应用场景的拓展&#xff0c;市场需求将持续增加。鸿蒙系统已经应用于华为的智能手机、平板电脑、智能家居等多个领域&#xff0c;并有望在未来拓展到智能汽车、物联网等更多领域。这为…

记一次vue路由跳转登陆之前的页面,参数丢失问题

一、背景 vue3.0&#xff0c;项目登陆之前访问某个可访问的页面&#xff0c;当跳转到需要登陆才能访问的页面时&#xff0c;跳转到登陆页面&#xff0c;登陆后再跳转到登陆之前需要登陆才能访问的页面&#xff0c;跳转时发现参数丢失了。 A页面&#xff08;无需登陆&#xff…

TypeScript 封装 Axios 1.7.7

随着Axios版本的不同&#xff0c;类型也在改变&#xff0c;以后怎么写类型&#xff1f; yarn add axios1. 封装Axios 将Axios封装成一个类&#xff0c;同时重新封装request方法 重新封装request有几个好处&#xff1a; 所有的请求将从我们定义的requet请求中发送&#xff…

2024.9.29 问卷数据分析

最近拿到了一份受众回访的问卷数据&#xff0c;排到的任务是对它进行数据探索。 其实对于问卷数据的处理我只在参加正大杯那次做过&#xff08;正大杯拿了校三&#xff09;&#xff0c;可见这个处理水平还有待提高&#xff08;当然是各种原因促成的结果&#xff09;&#xff0…

DenseNet算法:口腔癌识别

本文为为&#x1f517;365天深度学习训练营内部文章 原作者&#xff1a;K同学啊 一 DenseNet算法结构 其基本思路与ResNet一致&#xff0c;但是它建立的是前面所有层和后面层的密集连接&#xff0c;它的另一大特色是通过特征在channel上的连接来实现特征重用。 二 设计理念 三…

数据网格:数据去中心化的特征

在现代的数据管理架构理念中&#xff0c;常常会谈及数据网格&#xff0c;将它用来解决大规模、复杂数据环境下的数据管理和利用问题。本文将探讨数据网格的概念以及数据去中心化和数据网格的紧密联系。 一数据网格 数据网格定义&#xff1a;数据网格将数据视为一种产品&#x…

MyBatis 如何实现延迟加载?深度探讨 MyBatis 的延迟加载:如何优化数据访问效率

在当今的应用程序开发中&#xff0c;尤其是与数据库交互时&#xff0c;性能成为了重中之重。频繁的数据库访问会导致响应时间变慢&#xff0c;甚至影响用户体验。为了优化数据访问&#xff0c;MyBatis 提供了延迟加载&#xff08;Lazy Loading&#xff09;的强大功能。本文将详…