一.eureka注册中心的使用
1.创建注册服务器
1.在pom文件中添加eureka依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
2.在启动类上添加注解@EnableEurekaServer
3.修改配置文件
eureka: server: enable-self-preservation: false instance: hostname: eureka1 client: register-with-eureka: false # fetch-registry: false #
2.各个服务向注册中心注册
1.添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2.在application.yml配置文件中添加配置
spring: application: name: item-service #注册后的服务名 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka
二.服务之间的调用@FeignClient
通过@FeignClient注解
1.添加openfeign依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2.使用注解进行匹配
@FeignClient中的name属性是确定调用的那个服务
此注解实现了Ribbon的负载均衡
#重试参数的配置 ribbon: MaxAutoRetries: 1 #重试次数,默认0 MaxAutoRetriesNextServer: 2 #更换服务器次数,默认1 # 超过多长时间后重试 # ReadTimeout---等待响应的超时时间,默认1000ms #OkToRetryOnAllOperations--是否对所有类型请求都重试,默认只对GET请求 #ConnectTimeout--于后台服务器建立连接的等待超时时间,默认是1000ms