Spring Cloud之注册中心之Eureka

打印 上一主题 下一主题

主题 1014|帖子 1014|积分 3042


目录

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依赖

  
  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.springframework.cloud</groupId>
  4.         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5.     </dependency>
  6. </dependencies>
复制代码
项目构建插件

  
  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>org.springframework.boot</groupId>
  5.             <artifactId>spring-boot-maven-plugin</artifactId>
  6.         </plugin>
  7.     </plugins>
  8. </build>
复制代码
完善启动类

  1. package eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @EnableEurekaServer
  6. @SpringBootApplication
  7. public class EurekaServerApplication {
  8.     public static void main(String[] args) {
  9.         SpringApplication.run(EurekaServerApplication.class, args);
  10.     }
  11. }
复制代码
编写设置文件

  1. server:
  2.   port: 10010
  3. spring:
  4.   application:
  5.     name: eureka-server
  6. eureka:
  7.   instance:
  8.     hostname: localhost
  9.   client:
  10.     fetch-registry: false # 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
  11.     register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
  12.     service-url:
  13.       # 设置Eureka Server的地址,查询服务和注册服务都需要依赖这个地址
  14.       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  15. logging:
  16.   pattern:
  17.     console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
复制代码
启动服务并访问


可以看到eureka-server已经启动乐成了。 
服务注册

接下来我们把product-service 注册到eureka-server中。
引入eureka-client依赖

  
  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
复制代码
完善设置文件

添加如下设置
  
  1. spring:
  2.   application:
  3.   name: product-service
  4. eureka:
  5.   client:
  6.   service-url:
  7.   defaultZone: http://127.0.0.1:10010/eureka
复制代码
完整的设置文件
  
  1. server:
  2.   port: 9090
  3. spring:
  4.   application:
  5.     name: product-service
  6.   datasource:
  7.     url: jdbc:mysql://127.0.0.1:3306/cloud_product?characterEncoding=utf8&useSSL=false
  8.     username: root
  9.     password: #密码
  10.     driver-class-name: com.mysql.cj.jdbc.Driver
  11. # 设置 Mybatis 的 xml 保存路径
  12. mybatis:
  13.   configuration: # 配置打印 MyBatis 执行的 SQL
  14.     #    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  15.     map-underscore-to-camel-case: true  #自动驼峰转换
  16. eureka:
  17.   client:
  18.     service-url:
  19.       defaultZone: http://127.0.0.1:10010/eureka/
  20. logging:
  21.   pattern:
  22.     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依赖。
  
  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
复制代码
完善设置文件

添加如下设置
  
  1. spring:
  2.   application:
  3.   name: order-service
  4. eureka:
  5.   client:
  6.   service-url:
  7.   defaultZone: http://127.0.0.1:10010/eureka
复制代码
完整的设置文件
  
  1. server:
  2.   port: 8080
  3. spring:
  4.   application:
  5.     name: order-service
  6.   datasource:
  7.     url: jdbc:mysql://127.0.0.1:3306/cloud_order?characterEncoding=utf8&useSSL=false
  8.     username: root
  9.     password: #密码
  10.     driver-class-name: com.mysql.cj.jdbc.Driver
  11. # 设置 Mybatis 的 xml 保存路径
  12. mybatis:
  13.   configuration: # 配置打印 MyBatis 执行的 SQL
  14. #    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  15.     map-underscore-to-camel-case: true  #自动驼峰转换
  16. eureka:
  17.   client:
  18.     service-url:
  19.       defaultZone: http://127.0.0.1:10010/eureka/
  20. logging:
  21.   pattern:
  22.     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企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表