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

浙江坤宇建设有限公司网站免费app制作网站

浙江坤宇建设有限公司网站,免费app制作网站,傻瓜建站,东莞网站推广优化建设目录 1. sentinel使用场景 2. sentinel组成 3. sentinel dashboard搭建 4. sentinel客户端详细使用 4.1 引入依赖 4.2 application.properties增加dashboard注册地址 4.3 手动增加限流配置类 4.4 rest接口及service类 4.5 通过dashboard动态配置限流规则 1. sentinel使…

目录

1. sentinel使用场景

2.  sentinel组成

3. sentinel dashboard搭建

 4. sentinel客户端详细使用

4.1 引入依赖

4.2 application.properties增加dashboard注册地址

4.3 手动增加限流配置类

4.4 rest接口及service类

4.5 通过dashboard动态配置限流规则


1. sentinel使用场景

限流、熔断、监控、动态规则配置

2.  sentinel组成

由两部分组成,

第一个是dashboard监控仪表盘,单独的jar,官网下载后启动,可监控所有服务、动态发现服务、配置限流策略、熔断等;

第二个是sentinel的客户端核心包,供微服务引用,注册到dashboard仪表盘,引入相关pom及设置相关配置即可;

3. sentinel dashboard搭建

启动命令

java -Dserver.port=8400 -Dcsp.sentinel.dashboard.server=localhost:8400 -Dproject.name=hj-sentinel -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=sentinel -jar sentinel-dashboard-1.8.6.jar

启动成功,地址栏输入localhost:8400, 如图:

 

 4. sentinel客户端详细使用

4.1 引入依赖

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2.2.5.RELEASE</version>
</dependency>

4.2 application.properties增加dashboard注册地址

spring.cloud.sentinel.transport.dashboard=localhost:8400

4.3 手动增加限流配置类

package hj.example.sampleprovider.sample.config;import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.ArrayList;
import java.util.List;/*** @Description: sentinel 限流规则配置类**/
@Configuration
public class SentinelRulesConfig {@Beanpublic void initFlowQpsRules() {List<FlowRule> rules = new ArrayList<>();FlowRule flowRule = new FlowRule();flowRule.setResource("sentinelTest");flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);flowRule.setCount(1);flowRule.setLimitApp("default");flowRule.setClusterMode(false);flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);rules.add(flowRule);FlowRule flowRule1 = new FlowRule();flowRule1.setResource("sayHello");flowRule1.setGrade(RuleConstant.FLOW_GRADE_QPS);flowRule1.setCount(1);flowRule1.setLimitApp("default");flowRule1.setClusterMode(false);flowRule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);rules.add(flowRule1);FlowRuleManager.loadRules(rules);}
}

4.4 rest接口及service类

其中sentinelTest为rest接口限流,sayHello为方法限流

package hj.example.sampleprovider.sample.controller;import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSONObject;
import hj.example.sampleprovider.sample.HelloServiceImpl;
import hj.example.sampleprovider.sample.config.SentinelRulesConfig;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** @Description: TODO**/
@RestController
public class SentinelTestController {@Autowiredprivate HelloServiceImpl helloService;@RequestMapping("/testClean/{id}")public ResponseEntity<Object> testClean(@PathVariable("id") String id) {String resultStr = String.format("test clean id: %s [%s]", id, DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss ssss"));return new ResponseEntity<>(resultStr , HttpStatus.OK);}@RequestMapping("/testSentinelDynamicDashboard")public ResponseEntity<Object> testSentinelDynamicDashboard() {String resultStr = String.format("testSentinelDynamicDashboard [%s]", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss sss"));return new ResponseEntity<>(resultStr, HttpStatus.OK);}@RequestMapping("/sayHello")public ResponseEntity<Object> sayHelloTest() {String helloStr = helloService.sayHello("sayHelloTest");return new ResponseEntity<>(helloStr, HttpStatus.OK);}@SentinelResource(value = "sentinelTest", blockHandler = "sentinelTestHandler")@RequestMapping("/sentinelTest")public ResponseEntity<Object> sentinelTest() {System.out.println(" sentinelTest :" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss sss"));return new ResponseEntity<>("sentinelTest ", HttpStatus.OK);}public ResponseEntity<Object>  sentinelTestHandler(BlockException e) {System.out.println("被限流了");return new ResponseEntity<>("========sentinelTestHandler 被限流了:" + JSONObject.toJSONString(e), HttpStatus.OK);}
}
package hj.example.sampleprovider.sample;import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import hj.example.sample.IHelloService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import test.SentinelTest;import javax.xml.bind.ValidationException;
import java.util.Date;@DubboService
public class HelloServiceImpl implements IHelloService {@Value("${dubbo.application.name}")private String serviceName;@SentinelResource(value = "sayHello", blockHandler = "sayHelloBlockHandler")public String sayHello(String name) {System.out.printf("[%s]: Hello, %s%n", serviceName, name);return String.format("[%s]: Hello, %s", serviceName, name);}public String sayHelloBlockHandler(String name, BlockException e) throws ValidationException {System.out.println("sayHello 被限流了。name:" + name + ",被限流了:" + JSON.toJSONString(e));return "sayHello 被限流了。name:" + name + ",被限流了:" + JSON.toJSONString(e);}public void sentinelTestMethod() {try(Entry entry = SphU.entry("sentinelTestMethod")) {System.out.println("hello sentinel : " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss sss"));}catch (BlockException e) {e.printStackTrace();}}public void sentinelTestHandler(BlockException e) throws ValidationException {e.printStackTrace();throw new ValidationException(e.getMessage());}
}

4.5 通过dashboard动态配置限流规则

http://www.yayakq.cn/news/241696/

相关文章:

  • 邯郸论坛网站建设企业培训课程设置
  • 有服务器有域名如何做网站湖南长沙网站建设公司电话
  • 公司网站域名怎么注册百度网址安全中心怎么关闭
  • 外贸建站模版域名怎么卖
  • 做gif动图的网站犯法吗中国建设之乡是哪里
  • 做网站欢迎页什么意思双语网站后台怎么做
  • 潘多拉固件建设网站电子产品展示网站模板
  • 寻找集团网站建设杭州网页设计工作室
  • idea 做网站登录依波手表价格 官方网站
  • 苏州企业做网站h5响应式网站建设
  • 深圳坑梓网站建设公司深圳市招投标信息网
  • 网站icp备案信息查询哪个网站可以做室内设计
  • 建大型网站公司wordpress添加统计
  • 园林建设网站东莞著名网站建设企业
  • 南平购物网站开发设计常德百竞seo
  • 新化 网站开发做订餐网站数据库应该有哪些表
  • 网站建设及第三方支付视觉营销的网站设计
  • 一元购物网站开发移动互联网应用的使用情况
  • 网站建设 福步 2018wordpress怎么给分类标签写标题
  • 手机管理网站模板下载安装seo站内优化技巧
  • 深圳哪个招聘网站好产品设计公司推荐
  • 龙岗高端网站设计专家有哪些vue做的网站
  • 最早做淘宝客的网站管理的核心是什么
  • 营销型网站托管坦克大战网站开发课程设计报告
  • 手表怎么在网站做推广提高工作效率图片
  • 网站建设的分析修改WordPress上传图片时间
  • 正规排名网站推广公司怎么修改网页上的内容
  • 成立网站要多少钱营销传播方式有哪些
  • 苏州网站建设2万起wordpress百家号主题
  • 织梦网站怎样做百度主动推送宣传片制作公司前景