是什么
Eureka是一个REST (Representational State Transfer)服务,用于定位服务,以实现中间层服务器的负载平衡和故障转移,我们称此服务为Eureka
服务器。Eureka
还有一个基于java
的客户端组件,Eureka客户端,这使得与服务的交互更加容易,同时客户端也有一个内置的负载平衡器,它执行基本的循环负载均衡。
Feign 是一种声明式服务调用组件,它在 RestTemplate
的基础上做了进一步的封装。通过 Feign,我们只需要声明一个接口并通过注解进行简单的配置(类似于 Dao
接口上面的 Mapper
注解一样)即可实现对 HTTP
接口的绑定。
通过 Feign
,我们可以像调用本地方法一样来调用远程服务,而完全感觉不到这是在进行远程调用。
为什么
eureka
解决了什么问题?为什么要用eureka
?
eureka
为分布式环境引入了服务注册与发现的能力,如果没有服务注册与发现,我们便需要手动去维护一大堆服务的地址信息,而当服务的状态变化发生变化,比如有新的服务加入或某些现有服务突然不可用时,现存的服务也无法感知到并及时切换到可用的服务上去。
相对其他提供服务注册与发现的组件(比如说zk
),Eureka
更偏向于AP。如果分布式系统对可用性的要求更高,那么,Eureka
会是一个不错的选择。
另外,feign
屏蔽了请求构建、发送、重试、负载均衡等相关细节,让我们能够从重复的工作中解放出来,更专注于业务本身。
架构图
案例
eureka server
引入依赖
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置文件
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
service consumer
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class HelloClientApplication {
@Autowired
HelloClient client;
@RequestMapping("/")
public String hello() {
return client.hello();
}
public static void main(String[] args) {
SpringApplication.run(HelloClientApplication.class, args);
}
@FeignClient("HelloServer")
interface HelloClient {
@RequestMapping(value = "/", method = GET)
String hello();
}
}
配置文件
spring:
application:
name: HelloClient
server:
port: 7211
eureka:
password: password
client:
serviceUrl:
defaultZone: http://user:${
eureka.password}@localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
metadataMap:
instanceId: ${
vcap.application.instance_id:${
spring.application.name}:${
spring.application.instance_id:${
server.port}}}
endpoints:
restart:
enabled: true
service provider
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloServerApplication {
@Autowired
DiscoveryClient client;
@RequestMapping("/")
public String hello() {
List<ServiceInstance> instances = client.getInstances("HelloServer");
ServiceInstance selectedInstance = instances
.get(new Random().nextInt(instances.size()));
return "Hello World: " + selectedInstance.getServiceId() + ":" + selectedInstance
.getHost() + ":" + selectedInstance.getPort();
}
public static void main(String[] args) {
SpringApplication.run(HelloServerApplication