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

口碑好的赣州网站建设糗百网站开发

口碑好的赣州网站建设,糗百网站开发,做网站实训心得体会,网站背景自动切换本文所使用的组件:Nacos(服务中心和注册中心)、OpenFeign(服务调用)、Sentinel(限流、降级)、Hystrix(熔断) 项目结构: service-provider:提供服…

本文所使用的组件:Nacos(服务中心和注册中心)、OpenFeign(服务调用)、Sentinel(限流、降级)、Hystrix(熔断)

项目结构:

  1. service-provider:提供服务的微服务。

  2. service-consumer:消费服务的微服务。

1. 添加依赖

在两个服务的pom.xml文件中添加Spring Cloud Alibaba、Nacos、Sentinel、Hystrix和OpenFeign的依赖。

<dependencies><!-- Spring Cloud Alibaba Nacos Discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- Spring Cloud Alibaba Sentinel --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!-- Hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!-- Spring Cloud OpenFeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- Spring Boot Web Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

2. Docker-compose.yml文件

创建docker-compose.yml文件,启动Nacos和Sentinel。

version: '3'
services:nacos-server:image: nacos/nacos-server:latestcontainer_name: nacos-serverports:- "8848:8848"environment:- MODE=standalone- SPRING_DATASOURCE_PLATFORM=mysqlsentinel-server:image: apache/incubating-sentinel-dashboard:latestcontainer_name: sentinel-serverports:- "8080:8080"- "8719:8719"
  • 部署在云服务中需要打开8080、8848端口,端口冲突可以更换端口。

3. service-provider(提供服务的微服务)

3.1 启动类

创建启动类并添加nacos注册客户端。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
public class ServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(ServiceProviderApplication.class, args);}
}

3.2 控制器

创建控制器提供服务。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ServiceController {@GetMapping("/provider")public String provider() {return "Hello from Service Provider";}
}

4. service-consumer(消费服务的微服务)

4.1 OpenFeign客户端

创建Feign客户端用于调用service-provider

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(name = "service-provider", fallback = ServiceFallback.class)
public interface ServiceClient {@GetMapping("/provider")String provider();
}@Component
public class ServiceFallback implements ServiceClient {@Overridepublic String provider() {// 服务降级逻辑return "Fallback response from Service Consumer";}
}

4.2 控制器

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConsumerController {private final ServiceClient serviceClient;public ConsumerController(ServiceClient serviceClient) {this.serviceClient = serviceClient;}@GetMapping("/consumer")@HystrixCommand(commandKey = "consumerCommand", fallbackMethod = "fallback")@SentinelResource(value = "serviceClientCall", blockHandler = "blockHandler")public String consumer() {return serviceClient.provider();}public String fallback() {// 熔断降级逻辑return "Service is down, hystrix fallback";}public String blockHandler(BlockException ex) {// Sentinel降级逻辑return "Service is blocked by Sentinel";}
}
  • @HystrixCommand注解用于指定熔断器的命令键和降级方法,@SentinelResource注解用于指定资源名和降级方法。

5. application.yml

在两个服务添加application.yml文件。

spring:cloud:nacos:discovery:server-addr: nacos-server:8848config:server-addr: nacos-server:8848sentinel:transport:dashboard: sentinel-server:8080datasource:ds1:type: filefile:filePath: /path/to/your/sentinel/ruleshystrix:command:default:execution:isolation:strategy: THREADthread:timeoutInMilliseconds: 3000
  • filePath:文件路径

6. 配置规则

在Sentinel Dashboard(http://localhost:8080)中添加限流规则:

资源名:serviceClientCall
限流模式:QPS
阈值:10

总结:上述为一套完整的服务治理流程,对于某些场景下可以使用guava框架去实现单体限流。主要学习思想,本文所使用的组件可替换为其他具有相关功能的组件。

不积跬步,无以至千里 --- xiaokai

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

相关文章:

  • dede做的网站弹广告制作网页的软件免费
  • 临夏网站建设公司it培训班
  • 网站开发连接效果杭州 网站外包
  • 江阴百度推广公司滨州seo排名
  • 网站seo策划方案案例分析百度网盘app怎么打开链接
  • 做网站图片ps用哪种字体网站开发 跨平台开发技术
  • 建设银行手机银行网站用户名是什么意思教育类企业网站
  • 织梦网站怎么居中石家庄网站建设是什么意思
  • 青岛网站建设开发外包免费简约ppt模板
  • 网站推广的实际案例简易东莞网站制作公司
  • 佛山茶叶网站建设多媒体网页设计教程
  • 网上做结婚照的网站dw个人网页模板
  • 绵阳市住房 和城乡建设局网站荣成城市规划建设局网站
  • 网站制作ppt模板站长工具百度
  • 企业 网站 设计网站开发与管理内容
  • 如何做 网站映射网站的建设与预算
  • 重庆百度网站排名网站被k了怎么办
  • 二手网站建设方案广州公司建站
  • 如何做网站的薪酬调查aspnet东莞网站建设价格
  • 电子商务网站的建设和维护网站下载链接怎么做
  • 儒枫网网站建设广告推广哪个平台好
  • 手机购物网站模版网络域名费用多少钱
  • 广西哪家公司做网站的建设人行官方网站下载
  • 网站里的个人中心下拉列表怎么做优秀北京网站建设
  • 做网站不给提供ftp企业申请域名
  • ps做网站的时候分辨率是wordpress函数调用函数
  • 永州网站建设收费情况国外ip代理
  • 做三国的网站宁波seo整站优化
  • 在自己的网站里做讲课视频河南招投标信息网
  • 东营微信网站制作容桂销售型网站建设