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

重庆航运建设发展有限公司 网站华联股份股票

重庆航运建设发展有限公司 网站,华联股份股票,mac不能使用wordpress,北京空间优化平台1. sentinel服务负载均衡测试 sentinel默认开启了负载均衡的轮询模式,为了测试sentinel服务负载均衡的效果,需要先创建两个服务提供者和一个服务消费者。 1.1. 分别创建两个服务提供者-支付服务9003、9004 1. 添加pom依赖: 提供者只需要将…

1. sentinel服务负载均衡测试

sentinel默认开启了负载均衡的轮询模式,为了测试sentinel服务负载均衡的效果,需要先创建两个服务提供者和一个服务消费者。

1.1. 分别创建两个服务提供者-支付服务9003、9004

1. 添加pom依赖: 提供者只需要将自己注册到服务注册中心即可,因此只需要添加spring-cloud-starter-alibaba-nacos-discovery。

	<dependencies><!--自定义api--><dependency><groupId>com.atguigu.springboot</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><!--nacos服务发现--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--springboot web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--日常通用jar包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2. 编写yaml配置文件: 注意给两个服务分配不同的端口9003、9004

server:port: 9003spring:application:name: nacos-payment-providercloud:nacos:discovery:server-addr: localhost:8848management:endpoints:web:exposure:include: "*"

3. 创建主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9003 {public static void main(String[] args) {SpringApplication.run(PaymentMain9003.class,args);}
}

4. 创建业务类

package com.atguigu.alibaba.controller;import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;/*** @author 强浩* @version 1.0.0* Copyright(c) YTANG All Rights Reserved* @className* @project 管理系统* @date 2022年09月14日*/
@RestController
@Slf4j
public class PaymentController {@Value("${server.port}")private String serverPort;//模拟数据库public static HashMap<Long, Payment> hashMap = new HashMap<>();static {hashMap.put(1L, new Payment(1L,"28a8c1e3bc2742d8848569891fb42181"));hashMap.put(2L,new Payment(2L,"bba8c1e3bc2742d8848569891ac32182"));hashMap.put(3L,new Payment(3L,"6ua8c1e3bc2742d8848569891xt92183"));}//传入id获取数据@GetMapping(value = "/paymentSQL/{id}")public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id){Payment payment = hashMap.get(id);CommonResult<Payment> commonResult = new CommonResult(200, "from mysql,serverPort:  " + serverPort, payment);return commonResult;}
}

2. 创建服务消费者-订单服务84

1. 添加pom依赖: 因为之后需要使用sentinel进行限流,所以需要添加sentinel的依赖

    <dependencies><dependency><groupId>com.atguigu.springboot</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2. 配置application.yaml文件:

server:port: 84spring:application:name: nacos-order-consumercloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:dashboard: localhost:8080port: 8719server_url:payment_provider: http://nacos-payment-provider

3. 创建主启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class OrderMain84 {public static void main(String[] args) {SpringApplication.run(OrderMain84.class,args);}
}

4. 创建RestTemplate服务调用配置类:

package com.atgugui.alibaba.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;/*** @author 强浩* @version 1.0.0* Copyright(c) YTANG All Rights Reserved* @className* @project 管理系统* @date 2022年09月14日*/
@Configuration
public class ApplicationContextConfig {@Bean@LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();}
}

5. 创建业务类: 注入RestTemplate进行远程服务调用

package com.atgugui.alibaba.controller;import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
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 javax.annotation.Resource;/*** @author 强浩* @version 1.0.0* Copyright(c) YTANG All Rights Reserved* @className* @project 管理系统* @date 2022年09月14日*/
@RestController
@Slf4j
public class CircleBreakerController {public static final String SERVER_URL = "http://nacos-payment-provider";@Resourceprivate RestTemplate restTemplate;@GetMapping("/consumer/fallback/{id}")public CommonResult<Payment> fallback(@PathVariable("id") Long id){CommonResult<Payment> result = restTemplate.getForObject(SERVER_URL + "/paymentSQL/" + id, CommonResult.class, id);if(id == 4){throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");}else if(result.getData() == null){throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");}return result;}
}

5. 测试访问: 查看负载均衡的轮询效果
访问 http://localhost:84/consumer/fallback/1,多次访问,通过观察端口号可以发现,对支付服务进行了轮询访问。

2. fallback-业务错误处理

fallback只负责业务异常

1. 添加@SentinelResource注解,并配置fallback参数,指定服务降级处理方法

    @GetMapping("/consumer/fallback/{id}")@SentinelResource(value = "fallback", fallback = "handlerFallback")//指定fallback服务降级处理方法public CommonResult<Payment> fallback(@PathVariable("id") Long id){CommonResult<Payment> result = restTemplate.getForObject(SERVER_URL + "/paymentSQL/" + id, CommonResult.class, id);if(id == 4){throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");}else if(result.getData() == null){throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");}return result;}public CommonResult<Payment> handlerFallback(@PathVariable Long id,Throwable e){Payment payment = new Payment(id,"null");return new CommonResult<>(444,"兜底异常handlerFallback,exception内容  "+e.getMessage(),payment);}

2. 测试: http://localhost:84/consumer/fallback/5,在没有降级处理的时候会直接报错,添加了fallback后进入降级处理方法。
在这里插入图片描述

3. blockHandler-限流规则处理

blockHandler处理违反了限流规则的请求

1. 给fallback方法资源添加限流规则
在这里插入图片描述

2. 配置@SentinelResource的blockHandler参数指定流控服务降级方法

    @GetMapping("/consumer/fallback/{id}")@SentinelResource(value = "fallback", blockHandler = "blockHandler")public CommonResult<Payment> fallback(@PathVariable("id") Long id){CommonResult<Payment> result = restTemplate.getForObject(SERVER_URL + "/paymentSQL/" + id, CommonResult.class, id);if(id == 4){throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");}else if(result.getData() == null){throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");}return result;}public CommonResult<Payment> blockHandler(@PathVariable Long id, BlockException blockException){Payment payment = new Payment(id,"null");return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException  "+blockException.getMessage(),payment);}

3. 测试:http://localhost:84/consumer/fallback/5
在这里插入图片描述

4. 服务熔断fallback和blockHandler同时配置

blockHandler和fallback 都进行了配置,则被限流降级而抛出BlockException时只会进入blockHandler处理逻辑。也就是说会优先处理服务限流,保证高可用,不要卡死服务器。

@RestController
@Slf4j
public class CircleBreakerController {public static final String SERVER_URL = "http://nacos-payment-provider";@Resourceprivate RestTemplate restTemplate;@GetMapping("/consumer/fallback/{id}")@SentinelResource(value = "fallback", blockHandler = "blockHandler", fallback = "handlerFallback")public CommonResult<Payment> fallback(@PathVariable("id") Long id){CommonResult<Payment> result = restTemplate.getForObject(SERVER_URL + "/paymentSQL/" + id, CommonResult.class, id);if(id == 4){throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");}else if(result.getData() == null){throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");}return result;}public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {Payment payment = new Payment(id,"null");return new CommonResult<>(444,"兜底异常handlerFallback,exception内容  "+e.getMessage(),payment);}public CommonResult<Payment> blockHandler(@PathVariable Long id, BlockException blockException){Payment payment = new Payment(id,"null");return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException  "+blockException.getMessage(),payment);}
}

5. exceptionsToIgnore忽略指定异常

@RestController
@Slf4j
public class CircleBreakerController {public static final String SERVER_URL = "http://nacos-payment-provider";@Resourceprivate RestTemplate restTemplate;@GetMapping("/consumer/fallback/{id}")@SentinelResource(value = "fallback", blockHandler = "blockHandler", fallback = "handlerFallback",exceptionsToIgnore = IllegalArgumentException.class)public CommonResult<Payment> fallback(@PathVariable("id") Long id){CommonResult<Payment> result = restTemplate.getForObject(SERVER_URL + "/paymentSQL/" + id, CommonResult.class, id);if(id == 4){throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");}else if(result.getData() == null){throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");}return result;}public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {Payment payment = new Payment(id,"null");return new CommonResult<>(444,"兜底异常handlerFallback,exception内容  "+e.getMessage(),payment);}public CommonResult<Payment> blockHandler(@PathVariable Long id, BlockException blockException){Payment payment = new Payment(id,"null");return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException  "+blockException.getMessage(),payment);}}

设置了忽略IllegalArgumentException异常,当出现该异常时不会进入降级逻辑。
在这里插入图片描述

6. sentinel整合openFeign实现服务熔断降级

一般在消费端添加feign组件实现服务降级:在84消费端添加openFeign的依赖,实现服务的熔断降级。

1. 引入openFeign的pom依赖: 这里因为版本冲突问题,需要将devtools组件取消掉,目前具体冲突原因还不知道。

<dependencies><dependency><groupId>com.atguigu.springboot</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--添加openFeign依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>0.2.2.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency>--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2. 修改yaml文件,添加fegin对sentinel的支持:

server:port: 84spring:application:name: nacos-order-consumercloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:dashboard: localhost:8080port: 8719server_url:payment_provider: http://nacos-payment-providerfeign:sentinel:enabled: true

3. 在主启动类开启openFeign:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderMain84 {public static void main(String[] args) {SpringApplication.run(OrderMain84.class,args);}
}

4. 编写业务类: 使用@FeignClient注解,指定要进行访问的微服务名称,和发生熔断时所要调用的fallback方法

@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackServiceImpl.class)
public interface PaymentService {@GetMapping(value = "/paymentSQL/{id}")public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}

5. 服务降级方法实现类: 相当于给PaymentService增加了一个实现类,在发生熔断的情况下自动调用方法的fallback实现,来快速返回结果,防止系统卡死。

@Service
public class PaymentFallbackServiceImpl implements PaymentService {@Overridepublic CommonResult<Payment> paymentSQL(Long id) {return new CommonResult<>(44444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial"));}
}

6. 现在就可以使用openFeign进行远程服务调用,我们只需要像之前一样将服务接口注入到controller层中,即可实现无感的远程服务调用了:

@RestController
@Slf4j
public class CircleBreakerController {//注入openFeign远程调用接口@Resourceprivate PaymentService paymentService;@GetMapping(value = "/consumer/paymentSQL/{id}")public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id){CommonResult<Payment> result = paymentService.paymentSQL(id);return result;}
}

7. 测试: 访问 http://localhost:84/consumer/paymentSQL/2,此时将9003服务提供者断开,84消费侧自动降级,不会被耗死。
在这里插入图片描述

7. 各熔断框架比较

SentinelHystrixresilience4j
隔离策略信号量隔离(并发线程数限流)线程池隔商/信号量隔离信号量隔离
熔断降级策略基于响应时间、异常比率、异常数基于异常比率基于异常比率、响应时间
实时统计实现滑动窗口(LeapArray)滑动窗口(基于RxJava)Ring Bit Buffer
动态规则配置支持多种数据源支持多种数据源有限支持
拓展性多个扩展点插件的形式接口的形式
基于注解的支持支持支持支持
限流基于QPS,支持基于调用关系的限流有限的支持Rate Limiter
流量整形支持预热模式匀速器模式、预热排队模式不支持简单的Rate Limiter模式
系统自适应保护支持不支持不支持
控制台提供开箱即用的控制台,可配置规则、查看秒级监控,机器发观等简单的监控查看不提供控制台,可对接其它监控系统

文章转载自:
http://inquisitor.jftL.cn
http://unweight.jftL.cn
http://saumur.jftL.cn
http://geopolitist.jftL.cn
http://oneiromancy.jftL.cn
http://alkylate.jftL.cn
http://participancy.jftL.cn
http://vesicate.jftL.cn
http://idiodynamic.jftL.cn
http://intercourse.jftL.cn
http://socializee.jftL.cn
http://hayride.jftL.cn
http://absentation.jftL.cn
http://highflyer.jftL.cn
http://tracing.jftL.cn
http://undress.jftL.cn
http://resplendently.jftL.cn
http://hassel.jftL.cn
http://voyager.jftL.cn
http://amateur.jftL.cn
http://skid.jftL.cn
http://touriste.jftL.cn
http://dendritic.jftL.cn
http://balliness.jftL.cn
http://laticifer.jftL.cn
http://pollinic.jftL.cn
http://paddlewheeler.jftL.cn
http://caliduct.jftL.cn
http://tucker.jftL.cn
http://piratic.jftL.cn
http://liquory.jftL.cn
http://scolophore.jftL.cn
http://interacinous.jftL.cn
http://feverish.jftL.cn
http://lime.jftL.cn
http://kilometric.jftL.cn
http://zincite.jftL.cn
http://kronos.jftL.cn
http://bandsaw.jftL.cn
http://scug.jftL.cn
http://helladic.jftL.cn
http://please.jftL.cn
http://pagandom.jftL.cn
http://vaginate.jftL.cn
http://immunodiffusion.jftL.cn
http://paraboloid.jftL.cn
http://nethermore.jftL.cn
http://unclench.jftL.cn
http://sacristy.jftL.cn
http://attabal.jftL.cn
http://chapstick.jftL.cn
http://lancers.jftL.cn
http://conoscope.jftL.cn
http://billhead.jftL.cn
http://tripodal.jftL.cn
http://headline.jftL.cn
http://chafe.jftL.cn
http://towable.jftL.cn
http://lifeway.jftL.cn
http://pickerel.jftL.cn
http://kiddie.jftL.cn
http://peachy.jftL.cn
http://rotadyne.jftL.cn
http://motorcycle.jftL.cn
http://beagle.jftL.cn
http://latten.jftL.cn
http://sirach.jftL.cn
http://guestimate.jftL.cn
http://toner.jftL.cn
http://apogamy.jftL.cn
http://reticulocytosis.jftL.cn
http://iarovize.jftL.cn
http://luncheteria.jftL.cn
http://thoroughness.jftL.cn
http://somatotype.jftL.cn
http://bronchiole.jftL.cn
http://undoubled.jftL.cn
http://exequial.jftL.cn
http://fragile.jftL.cn
http://fluxmeter.jftL.cn
http://univallate.jftL.cn
http://marsh.jftL.cn
http://galactoscope.jftL.cn
http://denticare.jftL.cn
http://robotology.jftL.cn
http://daughterly.jftL.cn
http://lax.jftL.cn
http://swashbuckler.jftL.cn
http://mexicali.jftL.cn
http://authoritatively.jftL.cn
http://microsystem.jftL.cn
http://indiscreetly.jftL.cn
http://revivatory.jftL.cn
http://maximate.jftL.cn
http://porcupine.jftL.cn
http://astronomic.jftL.cn
http://diabolic.jftL.cn
http://confessed.jftL.cn
http://semifinalist.jftL.cn
http://defoliate.jftL.cn
http://www.dt0577.cn/news/104841.html

相关文章:

  • 防止迷路请收藏地址github潜江seo
  • 自己做的网站能放到织梦上小程序搭建
  • 网站开发百灵鸟优化seo搜索引擎推广什么意思
  • 网站建设宣传册揭阳百度快照优化排名
  • 广西营销型网站建设公司百度收录
  • 建设公司官网的请示杭州seo论坛
  • 备案审核网站显示500国际外贸网络交易平台
  • 衡水企业做网站推广电商怎么做如何从零开始
  • 河北区做网站公司免费seo刷排名
  • 阿拉善盟住房与城乡建设局网站seo查询外链
  • 用u盘做网站b站在线观看
  • wordpress 生成静态页面seo的内容有哪些
  • 手机网站免费模板旅游产品推广有哪些渠道
  • 沧州网站建设一网美联互联网营销模式有哪些
  • wordpress微信群发助手seo关键词排名网络公司
  • 创意设计素描图片seo是干什么的
  • 响水哪家专业做网站网站推广软件免费版大全
  • 如何做网站品类51外链代发网
  • 做旅游的网站那个便宜seo网站建设优化
  • 北京网站开发怎么做比百度还强大的搜索引擎
  • 如何充实网站内容电商运营的基本流程
  • 网站迁移 域名设置揭阳新站seo方案
  • 湖北企业建站系统信息培训课程
  • 做网站的外包需要分享客户信息雅思培训班价格一览表
  • 石家庄做网站时光windows优化大师提供的
  • 建设银行员工网站网站优化什么意思
  • 楚雄 网站建设武汉网站关键词推广
  • 江门网站制作方案定制上海网站建设方案
  • 如何向alexa提交网站南宁整合推广公司
  • 网站的开发语言汕头网站建设