使用Nacos作注册中心,整合SpringCloud Alibaba Sentinel的限流熔断的框架搭建

Posted 架构师智库

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Nacos作注册中心,整合SpringCloud Alibaba Sentinel的限流熔断的框架搭建相关的知识,希望对你有一定的参考价值。


今天,就不讲理论了

理论的学习我们社群里天天都有文字直播

感兴趣的同学,拉到底部

扫码进群

---开始---

首先准备三个项目

我这里是

调用关系:microservice-robot --》 microservice-myuser --》 microservice-order

# SpringCloud Alibaba 整合三部曲# 一般三步:加依赖、加注解、加配置

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
<groupId>com.itmuch.cloud</groupId><artifactId>spring-cloud-learning-rancher</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging>
<modules><module>microservice-robot</module><module>microservice-order</module><module>microservice-myuser</module></modules>
<properties><spring-cloud.version>Greenwich.RELEASE</spring-cloud.version><spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version><dubbo.version>2.7.6</dubbo.version>
<com.mapabc.common.version>1.0.2</com.mapabc.common.version><com.mapabc.model.version>1.0.1</com.mapabc.model.version><com.mapabc.protobuf.version>1.0.1</com.mapabc.protobuf.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties>
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version></parent>
<dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>

<!-- 1. nacos-增加配置中心的依赖 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>
<!-- 2. nacos-服务发现功能依赖 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- 2. 引入open-foreign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</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-test</artifactId><scope>test</scope></dependency>
<!-- Spring boot actuator to expose metrics endpoint --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Micormeter core dependecy --><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-core</artifactId></dependency><!-- Micrometer Prometheus registry --><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency>
</dependencies>
<dependencyManagement><dependencies>
<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId></dependency>

<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
<!--SpringCloud--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--SpringCloud Alibaba--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency>
</dependencies></dependencyManagement>
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><dependencies><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><version>1.2.5.RELEASE</version></dependency></dependencies></plugin><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuration><configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile><overwrite>true</overwrite><verbose>true</verbose></configuration><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-generator</artifactId><version>1.0.0</version></dependency></dependencies></plugin></plugins></build></project>


application.yml




spring:application:name: microservice-robotcloud:sentinel:transport:# port: 8719dashboard: 192.168.213.1:8080nacos:discovery:server-addr: 192.168.213.1:8848config:        server-addr: 192.168.213.1:8848        feign:sentinel:    enabled: true


这里简单只需要实现A->B->C的一种微服务调用关系即可

通过feign 进行调用



@FeignClient(name = "microservice-order", fallback = FallbackRemoteClient.class) public interface RemoteClient {     CallResult getGood(); }


@Slf4j@Componentpublic class FallbackRemoteClient implements RemoteClient{
@Overridepublic CallResult getGood(){          log.info("调用  微服务失败");return CallResult.failure("调用 微服务失败"); }
}


添加一个Controller


@RefreshScope@RestControllerpublic class MyController {




@RequestMapping(value="/callmyuser", method={RequestMethod.GET})public CallResult query3() throws Exception { System.out.println(" ...... call 。。。。" );return CallResult.success(remoteClient.getGood()); }



}

启动nacos


# 启动nacosstartup.cmd

启动sentinel


# 启动sentineljava -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

使用Nacos作注册中心,整合SpringCloud Alibaba Sentinel的限流熔断的框架搭建


使用Nacos作注册中心,整合SpringCloud Alibaba Sentinel的限流熔断的框架搭建


使用Nacos作注册中心,整合SpringCloud Alibaba Sentinel的限流熔断的框架搭建

停止microservice-order微服务的时候,则看到了这个界面


使用Nacos作注册中心,整合SpringCloud Alibaba Sentinel的限流熔断的框架搭建

需要获取源代码的可以找腾哥

有任何问题欢迎进群讨论:腾哥的微信:419644560

【架构师智库】

电报群每日动态推送架构领域的技术、方法、工具和理念资讯!






以上是关于使用Nacos作注册中心,整合SpringCloud Alibaba Sentinel的限流熔断的框架搭建的主要内容,如果未能解决你的问题,请参考以下文章

微服务Spring Cloud Alibaba之Nacos篇, Nacos 就是注册中心 + 配置中心的组合

SpringBoot整合Nacos注册中心

SpringBoot整合Nacos注册中心

Springcloud 整合Alibaba的Nacos实现服务注册和配置中心

71 nacos 实现微服务的注册与发现

微服务Nacos整合SpringCloud(注册中心配置中心)