SpringCloud注解和配置以及pom依赖说明

在本文中说明了pom依赖可以支持什么功能,以及支持什么注解,引入该依赖可以在application.properties中添加什么配置。

1、SpringCloud 的pom依赖

序号 pom依赖 说明 支持注解 支持配置application.properties
1

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
<relativePath/> 
</parent>

spring-boot-starter-parent是Spring Boot的核心启动器,
包含了自动配置、日志和YAML等大量默认的配置。
引入之后相关的starter引入就不需要添加version配置,
spring boot会自动选择最合适的版本进行添加。

@SpringBootApplication
@Configuration
@RequestBody
@RestController
@ComponentScan(basePackages={"com.xx","com.yy"})

server.port=1111
2

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

使用dependencyManagement进行版本管理
注意:这里的高版本Edgware.SR4不兼容spring-boot-starter-parent的高版本2.x,只能是1.x的高版本,比如1.5.16.RELEASE

   
3

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

支持HTTP调用方式,包含了Spring Boot预定义的一些Web开发的常用依赖包
如: spring-webmvc,Tomcat....

   
4

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>

   @SpringCloudApplication spring.application.name=eureka-service
5

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

eureka注册中心依赖  @EnableEurekaServer

eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
eureka.client.fetch-registry=false
eureka.client.fetch-registry=false

6

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

引入eureka 客户端依赖

@EnableDiscoveryClient
@EnableZuulProxy

 
7

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

引入ribbon 依赖 ,用来实现客户端的负载均衡,用在client客户端项目  

ribbon.ConnectTimeout=500
ribbon.ReadTimeout=5000
hello-service.ribbon.ConnectTimeout=500

8

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

引入hystrix 依赖 ,用来实现服务容错保护。当发现请求的服务端崩溃,就采用容错机制  @EnableCircuitBreaker hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
9

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

引入feign 依赖,包含ribbon负载均衡,也包含Hystrix服务容错。

Spring Cloud Feign在构建被@FeignClient注解修饰的服务客户端是,会为每一个客户端都创建一个feign.Logger实例,我们可以利用该日志对象进行Log分析。

 @EnableFeignClients

feign.compression.request.enabled=true;
feigan.compression.response.enabled=true;

10

<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-actuator</artifactId> 
</dependency>

监控模块,实时和定时监控服务的各项状态和可用性    
11

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

引入zuul依赖 , 它依赖了spring-boot-starter-actuator/spring-boot-starter-hystrix/spring-boot-starter-ribbon  @EnableZuulProxy

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.url=http://localhost:9001

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=hello-service

12

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分。
比如要访问my_test分支下的spring-cloud-config-file目录下的sam-dev.properties配置文件,访问url:
http://localhost:7001/sam/dev/my_test

 @EnableConfigServer

#配置Git仓库的地址
spring.cloud.config.server.git.uri=https://gitlab.com/xxx/xxx.git
#配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.server.git.search-paths=spring-cloud-config-file
#这里配置你的Git仓库的用户名
spring.cloud.config.server.git.username=xxx
#这里配置你的Git仓库的密码
spring.cloud.config.server.git.password=123

13

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

 分布式服务中管理配置文件的客户端,服务端是spring-cloud-config-server  @RefreshScope

bootstrap.properties:
#{profile}
spring.cloud.config.profile=dev
#{label} git分支名字
spring.cloud.config.label=master
#config server uri
spring.cloud.config.uri=http://localhost:7001

14

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

 在所有需要链路跟踪的项目中都加上这个依赖。   @EnableZipkinServer

除了sleuth本身是链路中心的除外,其余参与链路追踪的分布式系统都需要添加如下配置:
#指定zipkin服务端的url
spring.zipkin.base-url=http://localhost:9411
#设定样本收集的比率为100%
spring.sleuth.sampler.percentage=1.0

15

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.3.0.RELEASE</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

修改源文件后系统自动重启    
16

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

告诉Maven包含Spring特定的Maven插件,用于构建和部署SpringBoot应用程序。
注意,这个要放在build->plugins节点下。

   

17

<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.10</version>
<configuration>
<baseImage>java</baseImage>
<imageName>example</imageName>
</configuration>
</plugin>

部署以及持续集成。
注意,这个要放在build->plugins节点下。

   
18

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

使用Java Persistence API ( JPA )

@Entity // 这是一个JPA类
@Table(name="t_user") //映射到哪张表
@Id //主键
@Column(name="user_id",nullable=false) //对应表里哪个字段,不允许为空
@Repository //这是一个DAO类

 
19

<dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.1-901.jdbc4</version>
</dependency>

Postgres JDBC驱动程序    
 20

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-rsa</artifactId>
</dependency>

 加密解密相关包  

spring.datasource.password: "{cipher}d495ce8603af9676450736e119"
spring.cloud.config.server.encrypt.enabled=false

2、SpringCloud相关注解

序号 注解 说明
1  @SpringBootApplication SpringBoot启动类注解,启动类需有main方法
2  @EnableEurekaServer 用来指定该项目为Eureka的服务注册中心
3  @EnableCircuitBreaker 开启断路器,实现服务容错保护
4  @EnableDiscoveryClient 让服务使用eureka服务器 实现服务注册和发现
5  @SpringCloudApplication

相当于3个注解:@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker

6  @Configuration 相当于定义spring配置文件的xmlns域,以及支持@Bean在本类中的注册。
7  @EnableFeignClients Spring Cloud Feign 通过@EnableFeignClients来开启spring cloud feign的支持功能
不仅包含Spring Cloud ribbon负责均衡功能,也包含Spring Cloud
Hystrix服务容错功能,还提供了一种声明式的Web服务客户端定义方式。
8  @RequestBody 使接收到的字段值自动映射到注解声明的复合对象中
9  @RestController  @RestController = @Controller + @ResponseBody
10  @ComponentScan(basePackages={"com.xx","com.yy"}) 指定扫描包
11  @EnableZuulProxy

开启网关路由服务功能。
注意:一旦要请求的controller类实现了某个接口,恰好这个接口有自定义的@RequestMapping("/xxx")值,那么在发起请求时,完整的url不能省略这个@RequestMapping值:http://localhost:5555/hello-service/xxx/myHello

12  @Bean 向spring容器注册自定义类
13  @EnableConfigServer 开启Spring Cloud Config 的服务端功能。为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分。
14  @EnableZipkinServer 用于开启Zipkin Server功能:分布式框架中的如果发生异常可链路追踪.
15

@RequestMapping(value="/url",method=RequestMethod.POST)
public void getUrl(@PathVariable("name") String name){ … }

如何动态配置url路径,以及从路径中取值
16  @RefreshScope 对需要刷新的属性使用@Value注解,同时将类使用@RefreshScope注解进行标记

3、SpringCloud的application.properties相关设置

序号 application.properties配置 说明
1 server.port=1111 设置web项目服务端口号
2 spring.application.name=eureka-service 设置服务名称
3 eureka.instance.hostname=localhost 设置服务主机IP
4 eureka.client.register-with-eureka=false false: 注册中心不需要注册自己。true(默认): 需要注册自己
5 eureka.client.fetch-registry=false false: 注册中心不需要去发现服务。true(默认): 需要发现服务
6 eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka 设置服务注册中心的URL,这里的${}是占位符。最终显示例如:http://localhost:1111/eureka
7 ribbon.ConnectTimeout=500 全局配置负载均衡超时设置 ribbon.<key>=<value>
8 ribbon.ReadTimeout=5000 全局配置负载均衡读超时设置
9 hello-service.ribbon.ConnectTimeout=500 为某服务指定的负载均衡超时设置 @FeignClient(value="hello-service")
10 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000 hystrix.command.default.xxx进行全局配置
11 hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=5000 hystrix.command.<commandKey>.xxx进行指定配置,这里的<commandKey>可以为方法名
12

feign.compression.request.enabled=true;
feigan.compression.response.enabled=true;

请求压缩配置,支持对请求和响应进行GZIP压缩,以减少通信过程中的性能损耗。
13

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.url=http://localhost:9001

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=hello-service

zuul.routes.hello-service.path=/hello-service/**
zuul.routes.hello-service.serviceId=hello-service

请求示例:http://localhost:5555/api-a/feign-consumer
请到http://localhost:9001这个url地址找**(/feign-consumer)匹配的http接口,因为我把这个url的服务命名为api-a了。
推荐使用serviceId来代替url地址。
注意:zuul.routes.api-a.url=hello-service也能实现功能,但是它不能进行正常的负载均衡和容错保护。
不配置默认路由规则。当zuul.ignored-services=*的时候,所有的服务都不会自动创建路由规则,这个时候需要通过前面的配置进行相关路由配置了。

14

#配置Git仓库的地址
spring.cloud.config.server.git.uri=https://gitlab.com/xxx/xxx.git
#配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.server.git.search-paths=spring-cloud-config-file
#这里配置你的Git仓库的用户名
spring.cloud.config.server.git.username=xxx
#这里配置你的Git仓库的密码
spring.cloud.config.server.git.password=123

 SpringCloud自己创建的管理配置中心的服务端配置
15

spring.profiles.active=default

指定服务运行什么配置,比如application-dev.properties,就设置值为dev
16

spring.cloud.config.server.encrypt.enabled=false

显式关闭输出属性的解密。
17

spring.datasource.password: "{cipher}d495ce8603af9676450736e119"

SpringCloud配置服务器要求所有已加密的属性前面加上{cipher}

springCould注解和配置以及pom依赖的更多相关文章

  1. SpringCloud注解和配置以及pom依赖说明

    在本文中说明了pom依赖可以支持什么功能,以及支持什么注解,引入该依赖可以在application.properties中添加什么配置. 1.SpringCloud 的pom依赖 序号 pom依赖 说 ...

  2. Bean 注解(Annotation)配置(3)- 依赖注入配置

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

  3. SpringBoot第二集:注解与配置(2020最新最易懂)

    2020最新SpringBoot第二集:基础注解/基础配置(2020最新最易懂) 一.Eclipse安装SpringBoot插件 Eclipse实现SpringBoot开发,为便于项目的快速构建,需要 ...

  4. (转)maven配置之pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. Spring注解和配置方式

    Spring提供了一个org.springframework.beans.factory.FactoryBean工厂类接口,用户可以通过实现该接口定制实例化Bean的逻辑. 从Spring3.0开始, ...

  6. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  7. Spring3+SpingMVC+Hibernate4全注解环境配置

    Spring3+SpingMVC+Hibernate4全注解环境配置 我没有使用maven,直接使用Eclipse创建动态Web项目,jar包复制在了lib下.这样做导致我马上概述的项目既依赖Ecli ...

  8. 使用纯注解与配置类开发springMVC项目,去掉xml配置

    最近拜读了杨开振老师的书,深入浅出springBoot2.x,挖掘了很多以前被忽略的知识, 开发一年多,工作中一直用传统springmvc的开发,基本都还是用的传统的xml配置开发, 看到书里有提到, ...

  9. struts 多文件上传 annotation注解(零配置)+ ajaxfileupload + 异步 版本

    [本文简介] struts 多文件上传.基于”零配置“+"ajaxfileupload" 的一个简单例子. [导入依赖jar包] jquery-1.7.2.js : http:// ...

随机推荐

  1. 通过示例学习PYTORCH

    注意:这是旧版本的PyTorch教程的一部分.你可以在Learn the Basics查看最新的开始目录. 该教程通过几个独立的例子较少了PyTorch的基本概念. 核心是:PyTorch提供了两个主 ...

  2. 问题描述:Navicat连不上MySQL数据库

    发现Navicat连不上MySQL后我首先觉得是MySQL的服务没有打开, 然后再cmd里面 启动MySQL服务 输入: net start mysql 发现没有此服务,然我网上找了一下 先初始化My ...

  3. 【CF792E】Colored Balls(数论分块)

    题目链接 大意 有\(N\)种颜色的球,第\(i\)种球有\(Ai\)个,要求把球分成几个集合,使得: 一个集合里的球只能有一种颜色. 任意两个集合的球的数量相差不能超过1. 求这些球至少需要分几个集 ...

  4. 帆软报表(finereport)JS实现cpt中详细单元格刷新

    1.刷新固定单元格  setInterval(function(){ //获取第二行第 5 列 E2 单元格对象 var _changeCell = $("tr[tridx=1]" ...

  5. 5.Flink实时项目之业务数据准备

    1. 流程介绍 在上一篇文章中,我们已经把客户端的页面日志,启动日志,曝光日志分别发送到kafka对应的主题中.在本文中,我们将把业务数据也发送到对应的kafka主题中. 通过maxwell采集业务数 ...

  6. 添加删除系统右键菜单(就是上下文菜单,也就是Context Menu)中的一些选项

    随着电脑安装的东西越来越多,右侧菜单也原来越长,很不方面.所以打算清理一下 我删除的大约以下几个,友好一点的都可以配置.当然也可以通过注册表直接删除. 特:注册表备份,即导入导出,避免一失足成千古恨. ...

  7. java中的运算符介绍

    运算符&和&&的区别&运算符有两种用法:(1)按位与:(2)逻辑与. &&运算符是短路与运算.逻辑与跟短路与的差别是非常巨大的,虽然二者都要求运算符左右 ...

  8. 图解python | 面向对象编程

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/56 本文地址:http://www.showmeai.tech/article-det ...

  9. 内网流量操控---pingtunnel建立icmp隧道

    一.pingtunnel工作原理 在上面的实验环境中,我们将分别在攻击机kali 2020和webserver上部署pingtunnel工具,在量太主机之间实现icmp隧道,再在kali2020上监听 ...

  10. 业务人员可以进行自助ETL操作?这款BI工具你值得拥有

    ​ETL是什么? ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.E ...