第一步:搭建父项目,创建一个Maven项目,父项目不写代码,直接删除src

第二步:父项目需要的包

// 打包方式
<packaging>pom</packaging>
<!--SpringBoot,帮子项目管理依赖的版本号-->
<parent><groupId> org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version>
</parent>
<!--SpringCloudalibaba需要的依赖-->
<dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.1.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR3</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
<!--一些公共的依赖-->
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.58</version></dependency>
</dependencies>

第三步:创建子项目springcloudalibaba-user-server

user项目功能1.项目启动时要注册到nacos,启动nacos的方式,到下载的bin目录下,输入命令:startup.cmd -m standalone启动,功能2:限流,熔断,功能4:限流持久化。功能5:nacos的远程配置中心,功能6:三层架构

1.user模块需要导包
<dependencies><dependency><groupId>com.alibaba.cloud </groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--        配置中心客户端--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!--sentinel的包--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!--Sentinel和Nacos做持久的--><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId><version>1.5.2</version></dependency>
<!-- 数据库的jar包 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version>
</dependency>
<!--mybaties,和spring的集合包-->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version>
</dependency>
</dependencies>
2.yaml配置

2.1 bootstrap.yml

server:port: 1010
spring:application:name: user-servercloud:sentinel:transport:dashboard: localhost:1111 #senticel服务控制台地址nacos:discovery:server-addr: 127.0.0.1:8848    #注册中心地址config:server-addr: localhost:8848 #配置中心file-extension: yaml #配置文件格式prefix: application-user #配置前缀 ,默认使用sring.application.namegroup: DEFAULT_GROUP #默认分组namespace: 1415d50f-943a-469c-af50-10ca5f8dc91b #命名空间的idprofiles:active: dev

2.2 application.yaml

spring:cloud:sentinel:transport:dashboard: localhost:1111 #senticel服务控制台地址datasource:flow:nacos: #限流持久配置server-addr: localhost:8848    #使用nacos的持久dataId: application-user-dev   #获取限流的数据源的dataIdgroupId: DEFAULT_GROUPrule-type: flow #类型:限流namespace: 1415d50f-943a-469c-af50-10ca5f8dc91b # 因为我们在test里放的这个,持久化限流机制
连接数据库的配置
spring:datasource:password: 364911driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///movie?useUnicode=true&characterEncoding=utf-8username: root
mybatis:type-aliases-package: cn.zwj.domain

2.启动类

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

3.controller层代码

@RefreshScope  //刷新配置
@RestController
public class UserController {@Value("${testTement}")private String testTement;@GetMapping("/user/{id}") // blockHandler里有许多规则,包括限流等@SentinelResource(value = "这就是取一个名字", blockHandler = "getBlockHandlerUserById", fallback = "getfallback")public User getUserById(@PathVariable Long id) {int i = 1/0; // 设置一个异常return User.builder().name("陈涛").intro("又在打来福了" + testTement).build();}// 限流与阻塞处理 : 参数要和 被降级的方法参数一样 ,注意限流的优先级比熔断要高,比如,同时有限流和熔断,一定会执行限流public User getBlockHandlerUserById(@PathVariable Long id, BlockException ex) {// 来判断究竟是哪个异常if (ex instanceof FlowException) {return User.builder().name("流控异常").build();} else if (ex instanceof DegradeException) {return User.builder().name("熔断异常").build();} else if (ex instanceof ParamFlowException) {return User.builder().name("热点异常").build();} else if (ex instanceof SystemBlockException) {return User.builder().name("系统异常").build();} else if (ex instanceof AuthorityException) {return User.builder().name("授权异常").build();}return User.builder().name("陈涛").intro("把来福咬了,我要限流" + testTement).id(id).build();}public User getfallback(@PathVariable Long id, Throwable ex) {ex.printStackTrace();return User.builder().name("这里是熔断处理").build();}
}

具体步骤:user模块的远程配置中心和持久化限流如何配置

1.要配置远程中心:第一步点击+,如果需要配置命名空间,可以先创建一个命名空间,命名空间id可以不写,可以使用默认的,然后再在这个命名空间添加配置文件如application-user-dev.yaml

2.配置限流持久化

2.1 到安装目录下输入 java -jar -Dserver.port=1111 sentinel-dashboard-1.6.0.jar

2.2 第一步:在配置列表增加配置如下:就点击+号

[

{

"resource": "这就是取一个名字",

"limitApp": "default",

"grade": 1,

"count": 1,

"strategy": 0,

"controlBehavior": 0,

"clusterMode": false

}

]

其中resouce就是要找对那个资源进行限流,

就是@SentinelResource这个注解的value值

第四步:springcloudalibaba-order-server模块

模块功能:1.需要配置远程配置 2.要通过openfeign掉user时做负载均衡和熔断处理

1.导包

<dependencies><dependency><groupId>com.alibaba.cloud </groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--        配置中心客户端--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>cn.zwj</groupId><artifactId>springcloudalibaba-user-common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--sentinel的包--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
</dependencies>

2.启动类

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

3.controller层

@RestController
@RefreshScope
public class OrderController {@Autowiredprivate UserFeign userFeign;@GetMapping("/order/{id}")public User getUserById(@PathVariable Long id) {return userFeign.getUserById(id);}
}

为了通过openfeign用负载均衡的方式,给user发请求。所有我们要创建一个接口如userfeign

打上@feignclient注解

@FeignClient(value = "user-server", fallbackFactory = FallbackFactoryOrder.class)
public interface UserFeign {@GetMapping("/user/{id}")User getUserById(@PathVariable Long id);
}

value为我们要发请求服务名,GetMapping为对方user要接收的参数@PathVariable Long id,形参也是,我们在conreoller层发请求时,先把这个接收注入,再给他传值。

要实现熔断fallbackFactory,写一个类去继承

FallbackFactory<UserFeign>的泛型,泛型就是你发请求的接口
@Component
public class FallbackFactoryOrder implements FallbackFactory<UserFeign> {@Overridepublic UserFeign create(Throwable throwable) {return new UserFeign() {@Overridepublic User getUserById(Long id) {return User.builder().name("返回兜底数据").build();}};}
}

必须要加@Component注解,这样才能给spring管理

springcloudalibaba项目的搭建相关推荐

  1. 1-4 云商城项目工程搭建

    1-4 云商城项目工程搭建  接下来我们就来搭建下云商城的项目工程结构. 1.工程结构划分   因为我们搭建的是微服务项目,所以整体的项目结构会非常的零散.虽然零散但是程序的复用性会非常高,所以首先在 ...

  2. 黑马学成在线--项目环境搭建

    完整版请移步至我的个人博客查看:https://cyborg2077.github.io/ 学成在线–项目环境搭建 学成在线–内容管理模块 学成在线–媒资管理模块 学成在线–课程发布模块 学成在线–认 ...

  3. 谷粒商城笔记+踩坑(1)——架构、项目环境搭建、代码生成器

     导航: 谷粒商城笔记+踩坑汇总篇_谷粒商城笔记踩坑6_vincewm的博客-CSDN博客 目录 1.项目介绍 1.1 微服务架构图 1.2. 微服务划分图 2.项目环境搭建 2.1. 虚拟机搭建环境 ...

  4. 美多后台管理和项目环境搭建

    美多后台管理 Django框架已经提供了一个Admin管理后台,但是Admin的本身的页面可修改的页面布局效果比较少,无法满足公司定制页面需求,这时候就需要独立开发一套后台管理系统,满足公司对后台数据 ...

  5. Android组件化打造知乎日报系列(一)—— 项目架构搭建

    Android组件化打造第三方知乎日报系列(一)-- 项目架构搭建 本节完整代码可以前往github查看,项目地址:github.com/N0tExpectEr- Android组件化打造知乎日报系列 ...

  6. (三) Angular2项目框架搭建心得

    前言: 在哪看到过angular程序员被React程序员鄙视,略显尴尬,确实Angular挺值得被调侃的,在1.*版本存在的几个性能问题,性能优化的"潜规则"贼多,以及从1.*到2 ...

  7. 从壹开始前后端分离【 .NET Core2.2 +Vue2.0 】框架之六 || API项目整体搭建 6.1 仓储+服务+抽象接口模式...

    前言 1.@LearningCoding 小伙伴关于用Sqlsugar在mysql数据库上的研究成果: sqlsugarcore支持mysql等数据库,在DbContext里面只需要设置dbtype为 ...

  8. 搭建项目_跟着团子学SAP PS:项目模版搭建

    项目结构是整个PS模块最为核心的主数据,因为项目结构的搭建体现了企业管理项目的思路(按什么维度分解WBS等)以及颗粒度(WBS的层级,活动的多少等),而最终我们会将讨论成型的项目结构在SAP中以模版的 ...

  9. linux基本项目环境搭建

    文章目录 linux基本项目环境搭建 java环境搭建 获取java源码包 解压 配置环境变量 使配置文件生效 验证 nginx安装 下载(java也可用此方法下载) 解压 配置(configure) ...

最新文章

  1. 【337天】我爱刷题系列096(2018.01.08)
  2. 逃离迷茫和枯燥,让自己快乐起来
  3. 软件工程小组第三次正式会议
  4. 主存和cache每一块相等_笔记:cpu中的cache(一)
  5. td不允许自己扩展_V神原文详解:通过及时性检测器(TD)解决区块链的51%攻击问题...
  6. 宝塔如何备份网站_宝塔备份网站怎样还原_服务器备份数据恢复教程
  7. 上海的雨什么时候才可以停...
  8. Mutex对象是操作系统级?
  9. 【小贴士】zepto find元素以及ios弹出键盘可能让你很头疼
  10. linux vi 软件 下载,在Linux系统中下载安装Vim 8
  11. java8 API 中文 翻译版 java帮助文档下载
  12. Word XP 中目录的编制方法,Word XP 功能键使用大全
  13. 08.29web自动化测试
  14. 微博推荐算法学习(Weibo Recommend Algolrithm)
  15. JS:offsetParent屬性
  16. 性的短暂而致真情永失
  17. calendar 5 android版,Calendars 5 by Readdle
  18. linux 编辑文件 cat 跳到指定行,Linux基础命令(二)
  19. Eclipse条件断点的使用
  20. U盘在自己电脑读不出来,但其它电脑的可以读,别的u盘在自己电脑又可以识别的解决方法

热门文章

  1. Apple Silicon M1 MacBook Air上手详细评测M1的强大!
  2. LinkedList 类 的简单应用
  3. 如何将PPT进行压缩?简单的方法介绍
  4. 定制材料 Pd基聚多巴胺包裹碳纳米管/Fe或Cr单原子链填充Cu纳米管/Fe@CuNT和Cr@CuNT复合结构/氧化钼包裹碳纳米管纳米复合纤维
  5. stm32f103移植ucosIII系统
  6. 想做AR/VR相关创新项目,有什么好方向?要怎么做?
  7. 配色分享|梵高的薄荷绿
  8. 国际志愿者日 我们与爱同行
  9. 电源完整性PI原理知识1
  10. 网络安全之内网信息收集