SpringCloud注解和配置以及pom依赖说明
本文内容纲要:
在本文中说明了pom依赖可以支持什么功能,以及支持什么注解,引入该依赖可以在application.properties中添加什么配置。
1、SpringCloud的pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>
<version>1.5.16.RELEASE</version>
<relativePath/>
</parent>
spring-boot-starter-parent是SpringBoot的核心启动器,
包含了自动配置、日志和YAML等大量默认的配置。
引入之后相关的starter引入就不需要添加version配置,
springboot会自动选择最合适的版本进行添加。
@SpringBootApplication
@Configuration
@RequestBody
@RestController
@ComponentScan(basePackages={"com.xx","com.yy"})
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<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
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
</dependency>
支持HTTP调用方式,包含了SpringBoot预定义的一些Web开发的常用依赖包
如:spring-webmvc,Tomcat....
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
eureka.client.fetch-registry=false
eureka.client.fetch-registry=false
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
@EnableDiscoveryClient
@EnableZuulProxy
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
ribbon.ConnectTimeout=500
ribbon.ReadTimeout=5000
hello-service.ribbon.ConnectTimeout=500
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
引入feign依赖,包含ribbon负载均衡,也包含Hystrix服务容错。
SpringCloudFeign在构建被@FeignClient注解修饰的服务客户端是,会为每一个客户端都创建一个feign.Logger实例,我们可以利用该日志对象进行Log分析。
feign.compression.request.enabled=true;
feigan.compression.response.enabled=true;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
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
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分。
比如要访问my_test分支下的spring-cloud-config-file目录下的sam-dev.properties配置文件,访问url:
http://localhost:7001/sam/dev/my_test
#配置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
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
bootstrap.properties:
#{profile}
spring.cloud.config.profile=dev
#{label}git分支名字
spring.cloud.config.label=master
#configserveruri
spring.cloud.config.uri=http://localhost:7001
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
</dependency>
除了sleuth本身是链路中心的除外,其余参与链路追踪的分布式系统都需要添加如下配置:
#指定zipkin服务端的url
spring.zipkin.base-url=http://localhost:9411
#设定样本收集的比率为100%
spring.sleuth.sampler.percentage=1.0
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
<version>1.3.0.RELEASE</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>
</plugin>
告诉Maven包含Spring特定的Maven插件,用于构建和部署SpringBoot应用程序。
注意,这个要放在build->plugins节点下。
<plugin>
<groupId>com.spotify</groupId>
<artifactId>
<version>0.4.10</version>
<configuration>
<baseImage>java</baseImage>
<imageName>example</imageName>
</configuration>
</plugin>
部署以及持续集成。
注意,这个要放在build->plugins节点下。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
</dependency>
@Entity//这是一个JPA类
@Table(name="t_user")//映射到哪张表
@Id//主键
@Column(name="user_id",nullable=false)//对应表里哪个字段,不允许为空
@Repository//这是一个DAO类
<dependency>
<groupId>postgresql</groupId>
<artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<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 |
6 | @Configuration | 相当于定义spring配置文件的xmlns域,以及支持@Bean在本类中的注册。 |
7 | @EnableFeignClients | SpringCloudFeign通过@EnableFeignClients来开启springcloudfeign的支持功能不仅包含SpringCloudribbon负责均衡功能,也包含SpringCloudHystrix服务容错功能,还提供了一种声明式的Web服务客户端定义方式。 |
8 | @RequestBody | 使接收到的字段值自动映射到注解声明的复合对象中 |
9 | @RestController | @RestController=@Controller+@ResponseBody |
10 | @ComponentScan(basePackages={"com.xx","com.yy"}) | 指定扫描包 |
11 | @EnableZuulProxy | 开启网关路由服务功能。 |
12 | @Bean | 向spring容器注册自定义类 |
13 | @EnableConfigServer | 开启SpringCloudConfig的服务端功能。为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分。 |
14 | @EnableZipkinServer | 用于开启ZipkinServer功能:分布式框架中的如果发生异常可链路追踪. |
15 | @RequestMapping(value="/url",method=RequestMethod.POST) |
如何动态配置url路径,以及从路径中取值 |
16 | @RefreshScope | 对需要刷新的属性使用@Value注解,同时将类使用@RefreshScope注解进行标记 |
3、SpringCloud的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; |
请求压缩配置,支持对请求和响应进行GZIP压缩,以减少通信过程中的性能损耗。 |
13 | zuul.routes.api-a.path=/api-a/** |
请求示例:http://localhost:5555/api-a/feign-consumer |
14 | #配置Git仓库的地址 |
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} |
本文内容总结:
原文链接:https://www.cnblogs.com/zhuwenjoyce/p/9663324.html