概述
服务发现与注册
相关组件
- 服务端eurake service ,注册中心,类似于dubbo 的zookeeper,作为服务消费者和生产者的微服务客户端连接到eurake service,维持心跳连接 。
- 客户端 ,包含服务提供者和服务消费者,服务生产者向eurake servcie 注册,更新,取消服务,服务消费者向eurake service 获取注册的服务然后通过远程调用调用生产者提供的服务。通常一个客户端既是服务提供者,也是服务消费者
基本使用
1. 服务中心搭建 eurake servcie
1. pom添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. application.yml 添加配置
server:
port: 9000
eureka:
instance: #Eureka实例注册配置项,这些是无论Eureka是做Server,Client都需要的公共配置项。对应的配置类为org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean
hostname: localhost
server: # 作为注册中心才需要配置相关属性 EurekaServerConfigBean
# 自我保护模式开启
enableSelfPreservation: true
# 自我保护阈值0.49,当超过49%的服务器连接不上开启
renewalPercentThreshold: 0.49
# 主动剔除定时器间隔时间3000毫秒
evictionIntervalTimerInMs: 3000
# 是否开启只读缓存,不开启
useReadOnlyResponseCache: false
# 读写缓存失效时间
responseCacheAutoExpirationInSeconds: 30
# eureka 实例名称
client: # 作为Eureka Client才需要配置的选项,即服务需要向注册中心注册或使用注册中心中的服务时才需要配置这些选项 EurekaClientConfigBean
register-with-eureka: false
# 不向注册中心注册自己
fetch-registry: false
# 是否检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心访问地址
3. 启动类开启注册中心注释 `@EnableEurekaServer`
@EnableEurekaServer
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = "zxxx.eureka.server")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2. 服务注册
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
- 添加配置
server:
port:8081
spring:
application:
name: user-api #注册的服务名称,服务消费者根据这个值获取对应的服务
eureka:
instance:
instance-id: user-api-8081
prefer-ip-address: true
# 访问路径可以显示 IP
client:
service-url:
defaultZone: http://localhost:9000/eureka/ # 注册中心访问地址
- 提供接口(与提供给前端的controller 接口一样)
- 启动类开启服务器注册功能: 添加
@EnableEurekaClient注解
2. 服务发现 (结合Feign使用)
- pom 添加依赖
- 添加eurake clien 依赖
- 添加 openfeign 依赖
- 添加配置
- 添加eurake 相关配置,同服务生产者配置一样
- 添加 feign 配置
feign: hystrix: enabled: false client: config: default: # 服务名,default表示所有服务 connectTimeout: 30000 readTimeout: 30000 - 定义接口,编写FeignClient 接口,既可以写在服务提供方,也可以写在服务消费方,写在服务提供方会更通用些,其他消费者就可以通过添加依赖后直接使用
// @FeignClient 注解指定调用的微服务名称,value 指定的值为 服务提供方的 application-name的值 @FeignClient(value="USER-API") public interface UserFeignService{ // @RequestMapping 配置接口实现的访问路径 @RequestMapping("/provider/user/get/{id}") public User get(@PathVariable("id") Integer id); } - 启动类添加
@EnableFeignClients注解,并且指定要通过feign方式访问的服务 如@EnableFeignClients(clients = {UserFeignService.class})