Spring-Cloud Euraka是Spring Cloud集合中一个组件,它是对Euraka的集成,用于服务注册和发现。Eureka是Netflix中的一个开源框架。它和 zookeeper、Consul一样,都是用于服务注册管理的,同样,Spring-Cloud 还集成了Zookeeper和Consul。
在项目中使用Spring Cloud Euraka的原因是它可以利用Spring Cloud Netfilix中其他的组件,如zull等,因为Euraka是属于Netfilix的。
2、Euraka介绍Eureka由多个instance(服务实例)组成,这些服务实例可以分为两种:Eureka Server和Eureka Client。为了便于理解,我们将Eureka client再分为Service Provider和Service Consumer。
Eureka Server 提供服务注册和发现
Service Provider 服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到
Service Consumer服务消费方,从Eureka获取注册服务列表,从而能够消费服务
- P:Partition tolerance,网络分区容错。类似多机房部署,保证服务稳定性。
- A: Availability,可用性。
- C:Consistency ,一致性。
CAP定理:CAP三个属性对于分布式系统不同同时做到。如AP/CP/AC。再来看Zookeepr区别:
(1)Zookeeper是CP,分布式协同服务,突出一致性。对ZooKeeper的的每次请求都能得到一致的数据结果,但是无法保证每次访问服务可用性。如请求到来时,zookeer正在做leader选举,此时不能提供服务,即不满足A可用性。
(2)Eureka是AP,高可用与可伸缩的Service发现服务,突出可用性。相对于Zookeeper而言,可能返回数据没有一致性,但是保证能够返回数据,服务是可用的。
4. 项目搭建
spring_cloud:为总项目(父工程)
eureka-server:注册中心的服务(启动注册中心)
user-consumer:消费者(通过注册中心去访问提供者的API接口)
user-provider:提供者(提供访问接口到注册中心)
- spring_cloud(父工程)搭建
- pom.xml引入
org.springframework.boot spring-boot-starter-parent 2.6.2 org.springframework.cloud spring-cloud-dependencies 2021.0.0 pom import
- eureka-server(注册中心)搭建
- pom.xml引入
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
- application.yml
server:
port: 7001
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:7001/eureka
# server:
# enable-self-preservation: false
# eviction-interval-timer-in-ms: 5000
- 启动类配置
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
- user-provider(提供者)搭建
- pom.xml引入
com.baomidou mybatis-plus-boot-starter 3.1.1 org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.projectlombok lombok
- application.yml配置
server:
port: 18081
spring:
datasource:
password: 123456
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
application:
name: user-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka
instance:
ip-address: 127.0.0.1
prefer-ip-address: true
#若超过90s,eureka服务器还没收到provider发送的http请求,则认为服务停止运行。若关闭了eureka的自我保护机制,则eurekaServer会将无效的服务移除列表
lease-expiration-duration-in-seconds: 90
#服务正常情况下,每个30s,发送一个http请求给eurekaServer,证明自己可以正常运行
lease-renewal-interval-in-seconds: 30
- 启动类配置
@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = "com.guigu.dao")
public class UserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(UserProviderApplication.class,args);
}
}
- user-consumer(消费者)搭建
- pom.xml引入
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client
- application.yml配置
server:
port: 18082
spring:
application:
name: user-consumer
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka
- 启动类配置
@SpringBootApplication
//@EnableDiscoveryClient适用于多种注册中心 @EnableEurekaClient
@EnableDiscoveryClient
public class UserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(UserConsumerApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
- controller配置
@RestController
@RequestMapping("/consumer")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/{id}")
public Object findById(@PathVariable Integer id){
System.out.println("id = " + id);
List serviceInstances = discoveryClient.getInstances("user-provider");
ServiceInstance serviceInstance = serviceInstances.get(0);
String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/find/"+id;
System.out.println(url);
//"http://localhost:18081/user/find/"+id
String result = restTemplate.getForObject(url,String.class);
System.out.println(result);
return result;
}
}
入门搭建项目参考



