本文使用的是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中添加依赖。
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
复制代码 完整的pom文件
- <?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>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </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>
复制代码 配置文件
- server:
- port: 8360
- 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'
复制代码 启动类
- @EnableEurekaServer // 开启 Eureka Server
- @SpringBootApplication
- public class EurekaServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaServerApplication.class, args);
- }
- }
复制代码 启动
至此服务中心搭建成功。
服务注册
把product-service注册。
添加依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
复制代码 修改配置
主要是添加了
- server:
- port: 8350
- spring:
- application:
- # 给product-service起个名字,方便eureka管理
- name: product-service
- datasource:
- url: jdbc:mysql://82.157.124.63:8220/cloud_product?characterEncoding=utf8&useSSL=false
- username: root
- password: pxf1212
- 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 #自动驼峰转换
- # 配置打印 MyBatis 执行的 SQL
- logging:
- file:
- name: logs/springboot.log
- logback:
- rollingpolicy:
- max-file-size: 1KB
- file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i
- level:
- com:
- example:
- demo: debug
- # Eureka Client
- eureka:
- client:
- service-url:
- # product-service 使用这个地址 注册到 eureka-server
- defaultZone: http://127.0.0.1:8360/eureka/
复制代码 启动
可以看到已经product-service已经注册到服务中心了。
服务发现
在order-service拉取product-service的服务信息,从而实现服务发现。
添加依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
复制代码 修改配置
- server:
- port: 8340
- spring:
- application:
- name: order-service
- datasource:
- url: jdbc:mysql://82.157.124.63:8220/cloud_order?characterEncoding=utf8&useSSL=false
- username: root
- password: pxf1212
- 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 #自动驼峰转换
- # 配置打印 MyBatis 执行的 SQL
- logging:
- file:
- name: logs/springboot.log
- logback:
- rollingpolicy:
- max-file-size: 1KB
- file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i
- level:
- com:
- example:
- demo: debug
- eureka:
- client:
- service-url:
- defaultZone: http://127.0.0.1:8360/eureka
复制代码 远程调用
从eureka-server中获取product-service中的服务列表,并选择其中的一个调用。
- package com.demo.order.service;
- import com.demo.order.mapper.OrderMapper;
- import com.demo.order.model.OrderInfo;
- import com.demo.order.model.ProductInfo;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestTemplate;
- import java.util.List;
- @Slf4j
- @Service
- public class OrderService {
- @Autowired
- private OrderMapper orderMapper;
- @Autowired
- private RestTemplate restTemplate;
- @Autowired
- private DiscoveryClient discoveryClient;
- public OrderInfo selectOrderById(Integer orderId) {
- OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
- List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
- EurekaServiceInstance instance = (EurekaServiceInstance) instances.get(0);
- log.info(instance.getInstanceId());
- String url = instance.getUri() + "/product/" + orderInfo.getProductId();
- ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
- orderInfo.setProductInfo(productInfo);
- return orderInfo;
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |