IT评测·应用市场-qidao123.com
标题:
Spring Cloud之注册中心之Eureka
[打印本页]
作者:
天津储鑫盛钢材现货供应商
时间:
2025-2-19 03:35
标题:
Spring Cloud之注册中心之Eureka
目录
Eureka
搭建Eureka Server
创建Eureka-server子模块
引入eureka-server依赖
项目构建插件
完善启动类
编写设置文件
启动服务并访问
服务注册
引入eureka-client依赖
完善设置文件
启动服务并访问
服务发现
引入依赖
完善设置文件
启动服务并访问
Eureka和Zookeeper的区别
Eureka
Eureka是Netflix OSS套件中关于服务注册和发现的解决⽅案. Spring Cloud对Eureka进⾏了集成, 并作为优先推荐⽅案进⾏宣传, 固然⽬前Eureka 2.0已经停⽌维护, 新的微服务架构设计中, 也不再发起使⽤, 但是⽬前依然有⼤量公司的微服务系统使⽤Eureka作为注册中心。
Eureka重要分为两个部门:
Eureka Server: 作为注册中⼼Server端, 向微服务应⽤步伐提供服务注册, 发现, 康健检查等能⼒.
Eureka Client: 服务提供者, 服务启动时, 会向Eureka Server 注册⾃⼰的信息(IP,端⼝,服务信息等),Eureka Server 会存储这些信息。
关于Eureka的学习, 重要包含以下三个部门:
1. 搭建Eureka Server
2. 将order-service, product-service 都注册到Eureka
3. order-service远程调⽤时, 从Eureka中获取product-service的服务列表, 然后进⾏交互
搭建Eureka Server
Eureka-server 是⼀个独⽴的微服务.
创建Eureka-server子模块
引入eureka-server依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
复制代码
项目构建插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
复制代码
完善启动类
package eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
复制代码
编写设置文件
server:
port: 10010
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
fetch-registry: false # 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
service-url:
# 设置Eureka Server的地址,查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
logging:
pattern:
console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
复制代码
启动服务并访问
可以看到eureka-server已经启动乐成了。
服务注册
接下来我们把product-service 注册到eureka-server中。
引入eureka-client依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
复制代码
完善设置文件
添加如下设置
spring:
application:
name: product-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka
复制代码
完整的设置文件
server:
port: 9090
spring:
application:
name: product-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/cloud_product?characterEncoding=utf8&useSSL=false
username: root
password: #密码
driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:
configuration: # 配置打印 MyBatis 执行的 SQL
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true #自动驼峰转换
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka/
logging:
pattern:
console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
复制代码
启动服务并访问
可以看到product-service已经注册到 eureka上了。
服务发现
接下来我们修改order-service, 在远程调⽤时, 从eureka-server拉取product-service的服务信息, 实现服务发现。
引入依赖
服务注册和服务发现都封装在eureka-client依赖中, 以是服务发现时, 也是引⼊eureka-client依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
复制代码
完善设置文件
添加如下设置
spring:
application:
name: order-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka
复制代码
完整的设置文件
server:
port: 8080
spring:
application:
name: order-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/cloud_order?characterEncoding=utf8&useSSL=false
username: root
password: #密码
driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:
configuration: # 配置打印 MyBatis 执行的 SQL
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true #自动驼峰转换
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka/
logging:
pattern:
console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
复制代码
启动服务并访问
可以看到order-service已经注册到 eureka上了。
可以看到, 远程调⽤也乐成了。
Eureka和Zookeeper的区别
Eureka和Zookeeper都是⽤于服务注册和发现的⼯具,区别如下:
1. Eureka是Netflix开源的项⽬, ⽽Zookeeper是Apache开源的项⽬.
2. Eureka 基于AP原则, 包管⾼可⽤, Zookeeper基于CP原则, 包管数据⼀致性.
3. Eureka 每个节点 都是均等的, Zookeeper的节点区分Leader 和Follower 或 Observer, 也正由于这个原因, 如果Zookeeper的Leader发⽣故障时, 必要重新选举, 选举过程集群会有短暂时间的不可⽤。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4