hystrix微服务部署

devtools/2024/10/18 6:08:05/

目录

一.启动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/devtools/120441.html

相关文章

docker相关命令

构建镜像 sudo docker build -t your_image_name:tag .如果你希望容器在后台运行&#xff0c;可以使用 -d 参数启动容器 sudo docker run -d your_image_name重新构建镜像和启动容器后台运行 sudo docker-compose up --build -d运行容器 sudo docker-compose up停止服务 s…

从零开始Ubuntu24.04上Docker构建自动化部署(二)Docker-安装docker-compose

安装docker compose 下载 sudo curl -SL https://github.com/docker/compose/releases/download/v2.29.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose 授权 sudo chmod x /usr/local/bin/docker-compose 查看版本 sudo docker-compose --version 创建一…

【CSS in Depth 2 精译_043】6.5 CSS 中的粘性定位技术 + 本章小结

当前内容所在位置&#xff08;可进入专栏查看其他译好的章节内容&#xff09; 第一章 层叠、优先级与继承&#xff08;已完结&#xff09;第二章 相对单位&#xff08;已完结&#xff09;第三章 文档流与盒模型&#xff08;已完结&#xff09;第四章 Flexbox 布局&#xff08;已…

Service和Endpoints

在 Kubernetes 中&#xff0c;Service 和 Endpoints 是两个非常重要的资源对象&#xff0c;它们共同用于定义和管理集群内部的服务发现和网络通信。下面详细介绍这两个资源对象的功能及其相互关系。 Service Service 是 Kubernetes 中用于定义抽象逻辑服务的资源对象。它提供…

【SQL】未订购的客户

目录 语法 需求 示例 分析 代码 语法 SELECT columns FROM table1 LEFT JOIN table2 ON table1.common_field table2.common_field; LEFT JOIN&#xff08;或称为左外连接&#xff09;是SQL中的一种连接类型&#xff0c;它用于从两个或多个表中基于连接条件返回左表…

影响 Linux、Unix 系统的 CUPS 漏洞可导致 RCE

在经过大量炒作和第三方过早泄露信息之后&#xff0c;安全研究员 Simone Margaritelli 公布了有关通用 UNIX 打印系统 (CUPS) 中的四个零日漏洞的详细信息。 这些漏洞可被远程、未经身份验证的攻击者滥用&#xff0c;在易受攻击的 Linux 和类 Unix 系统上实现代码执行。 CUPS…

Python绘图库----turtle(海龟)

Python库 python程序的集合&#xff0c;这些程序都有不同的功能。Python库就相当于一个工具箱&#xff0c;里面有各种工具&#xff0c;工具的功能都有所不同。 Python绘图库 Python绘图库----turtle&#xff08;海龟&#xff09;&#xff0c;turtle 具有绘画功能&#xff0c…

MacOS配置python环境

下载 Python 前往网站下载对应芯片和python版本的 installer。 配置环境变量 sudo vim ~/.bash_profile添加一下内容&#xff0c;注意修改文件名。 export PATH"/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}" alias python"/Library/Fr…