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

1、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容器注册自定义类

2、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=*的时候,所有的服务都不会自动创建路由规则,这个时候需要通过前面的配置进行相关路由配置了。

3、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服务容错  @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

猜你喜欢

转载自www.cnblogs.com/zhuwenjoyce/p/9663324.html