【SpringCloud】Eureka的简单使用

打印 上一主题 下一主题

主题 245|帖子 245|积分 735

本文使用的是jdk17,mysql8。
  
  以下用两个服务做演示:
  订单服务:提供订单ID,获取订单详细信息。
  商品服务:提供商品ID,获取商品详细信息。
  对于上篇http://t.csdnimg.cn/vcWpo  订单服务调用商品服务的时间,使用Spring提供的RestTemplate远程调用时,url部门是写死的,这是很不方便我们后续的操纵。针对这个问题,这里使用Eureka来办理。

注册中心

注册中心是一种用于管理和和谐微服务架构中各个服务实例的组件。它充当了服务注册和发现的中心,使得微服务能够相互发现和通讯
服务注册:每个微服务启动时,会向注册中心注册自己的网络地址、服务名称和其他相干信息。注册中心将这些信息保存起来,以便其他服务可以查询。
服务发现:当一个微服务需要与其他服务进行通讯时,它可以向注册中心查询目标服务的信息,如网络地址、可用实例等。这样,服务之间就可以通过注册中心来建立连接,实现相互通讯。
注册中心主要有三种角色:
服务提供者(Server):一次业务中,给其他微服务提供接口使用。
服务消耗者(Client): 一次业务中,调用其他微服务的接口。
服务注册中心(Registry): 用来保存服务提供者的信息,并且当服务提供者发生变化时,它也同步更新。服务与注册中心使用一定的通讯机制,如果服务与注册中心长时间没有通讯,那么注册中心就会注销服务。

常见的注册中心:
ZooKeeper:ZooKeeper是一种开源的分布式和谐服务,在微服务架构中常被用作注册中心。它具有高可用、同等性和可靠性等特点。(CP保证同等性)节点分为Leader、Follower和Observer,当Leader出现故障时,需要选举出Leader,此时服务不可用。
Eureka:Eureka是Netflix开源的注册中心,具有简单易用、高可用、自我保护等特点,常被用于构建基于Spring Cloud的微服务架构。(AP保证高可用)每个节点都是均等的。
Nacos:Nacos是阿里巴巴开源的一款服务发现和配置管理平台,也可以作为微服务架构中的注册中心。它提供了服务注册、服务发现、动态配置管理和服务治理等功能,对于构建和管理云原生应用非常有用。(CP或AP,默认AP)

CAP理论

CAP理论由下面三部门构成:
同等性(Consistency):多个节点访问数据时,得到的数据都是相同的数据。如果无法保证数据是相同的,就不返回任何数据。
可用性(Availability):每个请求都有相应。大概某个节点返回的结果不对,但是也要返回。
分区容错性(Partition Tolerance):网络分区运行,但是依然可以对外提供服务。如果节点之间出现了故障,也能进行服务。
   根据CAP定理,分布式系统只能满足其中两个属性,无法同时满足三个。这是因为在面临网络分区(节点之间无法相互通讯)的情况下,系统必须在同等性和可用性之间进行权衡选择。而且在分布式系统中,分区容错性必须考虑,一旦发生错误,导致整个系统不能使用,这是不符合现实的。
  于是就出现了:


  • CP架构:保证了分布式系统对外提供的数据同等性。如果不同等,就不返回任何数据。
  • AP架构:保证了分布式系统的可用性,返回的结果就算不对也要返回。

Eureka

Eureka是Netflix OSS套件中的服务注册和发现办理方案。Spring Cloud对Eureka进行了集成,并长期以来作为推荐的办理方案。尽管Eureka 2.0已经停止维护,并且在新的微服务架构设计中不再被推荐使用,但目前仍有许多公司的微服务系统在使用Eureka作为其注册中心。

组成

Eureka Server:作为服务中心的服务端,向微服务应用步伐提供服务注册,发现,健康查抄等。
Eureka Client:服务提供者,向服务端注册自己的信息(IP, 端口, 服务信息等),服务端会保存信息。

搭建Eureka Server

   对于Eureka Server,应该创建成一个独立项目方便管理。这里为了方便,将在上一篇http://t.csdnimg.cn/PV6Xw 的底子上,把Eureka Server创建成子模块。
  创建子模块


添加依赖

在刚才的eureka-server模块的pom中添加依赖。
  1.         <dependency>
  2.             <groupId>org.springframework.cloud</groupId>
  3.             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4.         </dependency>
复制代码
完整的pom文件 
  1. <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <parent>        <artifactId>spring-cloud-eureka</artifactId>        <groupId>org.example</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>eureka-server</artifactId>    <dependencies>        <dependency>
  2.             <groupId>org.springframework.cloud</groupId>
  3.             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4.         </dependency>    </dependencies>    <properties>        <maven.compiler.source>17</maven.compiler.source>        <maven.compiler.target>17</maven.compiler.target>    </properties>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
复制代码
配置文件

  1. server:
  2.   port: 8360
  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'
复制代码
启动类

  1. @EnableEurekaServer // 开启 Eureka Server
  2. @SpringBootApplication
  3. public class EurekaServerApplication {
  4.     public static void main(String[] args) {
  5.         SpringApplication.run(EurekaServerApplication.class, args);
  6.     }
  7. }
复制代码
启动 


至此服务中心搭建成功。

服务注册

把product-service注册。
添加依赖

  1.         <dependency>
  2.             <groupId>org.springframework.cloud</groupId>
  3.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4.         </dependency>
复制代码
修改配置

主要是添加了
  1. server:
  2.   port: 8350
  3. spring:
  4.   application:
  5.     # 给product-service起个名字,方便eureka管理
  6.     name: product-service
  7.   datasource:
  8.     url: jdbc:mysql://82.157.124.63:8220/cloud_product?characterEncoding=utf8&useSSL=false
  9.     username: root
  10.     password: pxf1212
  11.     driver-class-name: com.mysql.cj.jdbc.Driver
  12. # 设置 Mybatis 的 xml 保存路径
  13. mybatis:
  14.   configuration: # 配置打印 MyBatis 执行的 SQL
  15.     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  16.     map-underscore-to-camel-case: true  #自动驼峰转换
  17. # 配置打印 MyBatis 执行的 SQL
  18. logging:
  19.   file:
  20.     name: logs/springboot.log
  21.   logback:
  22.     rollingpolicy:
  23.       max-file-size: 1KB
  24.       file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i
  25.   level:
  26.     com:
  27.       example:
  28.         demo: debug
  29. # Eureka Client
  30. eureka:
  31.   client:
  32.     service-url:
  33.       # product-service 使用这个地址 注册到 eureka-server
  34.       defaultZone: http://127.0.0.1:8360/eureka/
复制代码
启动


可以看到已经product-service已经注册到服务中心了。

服务发现

在order-service拉取product-service的服务信息,从而实现服务发现。
添加依赖

  1.         <dependency>
  2.             <groupId>org.springframework.cloud</groupId>
  3.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4.         </dependency>
复制代码
修改配置

  1. server:
  2.   port: 8340
  3. spring:
  4.   application:
  5.     name: order-service
  6.   datasource:
  7.     url: jdbc:mysql://82.157.124.63:8220/cloud_order?characterEncoding=utf8&useSSL=false
  8.     username: root
  9.     password: pxf1212
  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. # 配置打印 MyBatis 执行的 SQL
  17. logging:
  18.   file:
  19.     name: logs/springboot.log
  20.   logback:
  21.     rollingpolicy:
  22.       max-file-size: 1KB
  23.       file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i
  24.   level:
  25.     com:
  26.       example:
  27.         demo: debug
  28. eureka:
  29.   client:
  30.     service-url:
  31.       defaultZone: http://127.0.0.1:8360/eureka
复制代码
远程调用

从eureka-server中获取product-service中的服务列表,并选择其中的一个调用。
  1. package com.demo.order.service;
  2. import com.demo.order.mapper.OrderMapper;
  3. import com.demo.order.model.OrderInfo;
  4. import com.demo.order.model.ProductInfo;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.cloud.client.ServiceInstance;
  8. import org.springframework.cloud.client.discovery.DiscoveryClient;
  9. import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.web.client.RestTemplate;
  12. import java.util.List;
  13. @Slf4j
  14. @Service
  15. public class OrderService {
  16.     @Autowired
  17.     private OrderMapper orderMapper;
  18.     @Autowired
  19.     private RestTemplate restTemplate;
  20.     @Autowired
  21.     private DiscoveryClient discoveryClient;
  22.         public OrderInfo selectOrderById(Integer orderId) {
  23.             OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
  24.             List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
  25.             EurekaServiceInstance instance = (EurekaServiceInstance) instances.get(0);
  26.             log.info(instance.getInstanceId());
  27.             String url = instance.getUri() + "/product/" + orderInfo.getProductId();
  28.             ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
  29.             orderInfo.setProductInfo(productInfo);
  30.             return orderInfo;
  31.         }
  32. }
复制代码
 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

梦应逍遥

高级会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表