一、Eureka服务注册与发现
1.Eureka基础知识
- 服务治理:用于管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
- Eureka包含两个组件:Eureka Server和Eureka Client
Eureka Server:提供服务注册服务,存储所有可用服务节点的信息
Eureka Client:EurekaClient通过注册中心进行访问
2.单机Eureka构建步骤
(1)pom文件:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--boot web actuator-->
<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>
(2)配置文件
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
(3)启动类
package com.zj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 概述:
* 作者:zhujie
* 创建时间:2021/12/15 10:32
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerAppication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerAppication.class,args);
}
}
(4)测试成功的页面
3.集群Eureka构建步骤
(1)按照单机Eureka构建步骤建立两个节点
(2)修改两个节点配置文件分别为(区别为defaultZone配置项,单节点构建时,为节点本身地址,集群构建时,为集群中除节点本身外其它节点的地址)
节点一:
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://eureka7002.com:7002/eureka/
节点二:
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://eureka7001.com:7001/eureka/
4.actuator微服务信息完善
(1)引入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2)主机名称修改与显示ip地址:
eureka:
instance:
instance-id: payment1
prefer-ip-address: true
二、zookeeper服务注册与发现
(1)引入jar包
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
(2)配置文件
spring:
application:
name: springcloud-order
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
(3)启动类
package com.zj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 概述:
* 作者:zhujie
* 创建时间:2021/12/15 9:24
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderAppication {
public static void main(String[] args) {
SpringApplication.run(OrderAppication.class,args);
}
}
三、Consul服务注册与发现
(1)引入jar包
<!-- SpringBoot整合Consul客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
(2)配置文件
spring:
application:
name: springcloud-order
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}
(3)启动类
package com.zj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 概述:
* 作者:zhujie
* 创建时间:2021/12/15 9:24
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderAppication {
public static void main(String[] args) {
SpringApplication.run(OrderAppication.class,args);
}
}
四、三者区别
组件名 | 编写语言 | CAP | 服务健康检查 | 对外暴露的接口 | springcloud集成 |
---|---|---|---|---|---|
eureka | Java | AP | 可配支持 | HTTP | 已集成 |
consul | Go | CP | 支持 | HTTP/DNS | 已集成 |
zookeeper | Java | CP | 支持 | 客户端 | 已集成 |
注:CAP定理: 指的是在一个分布式系统中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可同时获得。