十二、服务注册与发现总结
12.1 注册中心
-
Eureka
- 搭建注册中心
-
引入依赖
<!-- eureka相关依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
-
配置application.yml文件
#配置高可用集群(Eureka Server) eureka: client: register-with-eureka: true #是否将自己注册到注册中心,默认是true service-url: defaultZone: http://127.0.0.1:8000/eureka/ #配置暴露给Eureka Client的请求地址 #默认请情况下,eureka剔除失效服务时间间隔为90s且存在自我保护的机制, #所以不能有效而迅速的剔除失效节点,这样对测试或是开发都会造成困扰 #解决办法:配置关闭自我保护,设置剔除无效节点的时间间隔 server: enable-self-preservation: false #关闭自我保护 eviction-interval-timer-in-ms: 4000 #剔除时间间隔,单位:毫秒 instance: instance-id: ${spring.cloud.client.ip-address}:${server.port} #ip:端口号 prefer-ip-address: true #使用IP地址注册 ```
-
通过@EnableEurekaServer激活Eureka Server端配置
package cn.ebuy.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //激活Eureka Server端配置 public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } }
- 服务注册(服务提供者和服务消费者都要进行服务注册)
-
引入依赖
<!-- eureka注册中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
配置注册中心地址
#配置集群(Eureka Client) eureka: client: healthcheck: true #开启健康检查(依赖spring-boot-actuator) service-url: defaultZone: http://127.0.0.1:9000/eureka/,http://127.0.0.1:8000/eureka/ #配置集群 instance: prefer-ip-address: true #使用ip地址注册 lease-expiration-duration-in-seconds: 10 #eureka client发送心跳给server端后,续约到期时间(默认是90秒)
-
Consul
- 搭建注册中心
-
下载安装consul
-
启动consul服务
consul agent -dev -client=0.0.0.0
- 服务注册
-
引入依赖
<!--SpringCloud提供的基于Consul的服务发现--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--actuator用于心跳检查--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
配置application.yml文件指定请求的地址
spring: #配置consul cloud: consul: host: 127.0.0.1 #ConsulServer请求地址 port: 8500 #ConsulServer端口号(默认8500) discovery: register: true #是否注册 instance-id: ${spring.application.name}-2 #实例id service-name: ${spring.application.name} #服务实例的名称 port: ${server.port} #服务实例的端口号 health-check-path: /actuator/health #健康检查路径 health-check-interval: 15s #健康检查时间间隔(默认10s) prefer-agent-address: true #开启ip地址注册 ip-address: ${spring.cloud.client.ip-address} #当前微服务请求的ip
12.2 服务调用
- Ribbon
-
通过Ribbon结合RestTemplate方式进行服务调用,只需要在声明RestTemplate的方法上添加注解@LoadBalanced即可。
/** * 配置RestTemplate交给spring管理 * 基于Ribbon的服务调用与负载均衡 * @return */ @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
-
可以通过{服务名称}.ribbon.NFLoadBalancerRuleClassName配置负载均衡策略。
##需要调用的微服务名称 ebuy-product: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #负载均衡策略
- Feign
-
服务消费者引入相关依赖
<!-- feign调用服务 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
-
通过@FeignClient声明一个调用远程微服务接口
package cn.ebuy.order.feign; import cn.ebuy.order.pojo.EasybuyProduct; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; //指定调用的微服务名称,以fallback声明降级方法 @FeignClient(name = "ebuy-product") public interface OrderFeignClient { //调用的请求路径 @RequestMapping(value = "/product/{id}",method = RequestMethod.GET) public EasybuyProduct findById(@PathVariable("id")Long id); }
-
在启动类上通过@EnableFeignClients激活Feign
package cn.ebuy.order; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableFeignClients public class OrderApplication { /** * 配置RestTemplate交给spring管理 * 基于Ribbon的服务调用与负载均衡 * @return */ @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } }