0


注册中心技术Eureka、Nacos

说明:在微服务框架中,各个服务之间都是独立的。理论上来说,各个服务之间是可以直接通信的,但实际上因为服务之间通信需要管理和规划,如请求怎么负载均衡、请求怎么降级处理等等,所以就需要使用一个技术,对这些服务做到统一管理,称为注册中心,Eureka、Nacos就是这类技术。

在这里插入图片描述

环境搭建:

在center_module模块下,创建两个子模块:订单模块(端口8081)、用户模块(端口8082),两个模块之间没有联系,现在需要查询订单,根据订单中的用户ID,查询该订单对应的用户信息。详细参考(http://t.csdn.cn/gwnXI)

订单服务配置文件(application.properties)

mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_spring_cloud
spring.datasource.username=root
spring.datasource.password=123456

# 设置端口号
server.port=8081

# 设置微服务名称
spring.application.name=orderservice

用户服务配置文件(application.properties)

mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_spring_cloud
spring.datasource.username=root
spring.datasource.password=123456

# 设置端口号
server.port=8082

# 设置微服务名称
spring.application.name=userservice

# 关联eureka的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8083/eureka/

Eureka

在微服务之上创建一个Eureka模块,里面只有启动类、pom.xm文件、application配置文件,不写业务代码;

(启动类,需加@EnableEurekaServer注解)

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublicclassStart{publicstaticvoidmain(String[] args){SpringApplication.run(Start.class, args);}}

(pom.xml文件依赖)

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId><version>2.2.5.RELEASE</version></dependency></dependencies>

(application.yml配置文件)

# 配置端口号
server.port=8083

# 如果当前Eureka服务器只有一台 写自己的地址即可
eureka.client.serviceUrl.defaultZone=http://localhost:8083/eureka/

# 配置Eureka不抓取自己的服务
eureka.client.fetch-registry=false

# 配置Eureka不注册自己
eureka.client.register-with-eureka=false

关联的订单服务、用户服务需要添加对应的依赖

<!--添加eureka服务依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

并在配置文件中关联注册中心

# 关联eureka的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8083/eureka/

启动所有服务,打开注册中心(http://localhost:8083,注意不要加eureka),可以看到两个服务已经上线

在这里插入图片描述

此时,就可以直接用对应的服务名对微服务发送请求
在这里插入图片描述


在这里插入图片描述

负载均衡策略参考(http://t.csdn.cn/neb3n)

Nacos

安装&启动

可在官网(https://nacos.io/zh-cn/)提供的下载链接(https://github.com/alibaba/nacos/releases)中下载,下载后解压到一个**没有中文、没有数字**的路径下。打开目录的bin文件夹,在此目录下打开CMD,输入

startup.cmd -m standalone 

命令,启动Nacos,这种方式为单级模式、非集群模式启动,直接双击startup.exe为集群模式。
在这里插入图片描述
出现下面界面,为启动成功

在这里插入图片描述

此时,可以打开浏览器,输入:http://localhost:8848/nacos/,进入nacos平台,首次登录需要账号密码,都是nacos。

在这里插入图片描述

使用

在订单服务、用户服务的pom.xml文件中,添加nacos注册依赖

<!--nacos配置管理依赖【注册】--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>

订单服务配置(application.yml)

# 配置服务器端口server:port:8081# 1.spring配置spring:# 1.1 配置数据库连接池datasource:driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_spring_cloud
    username: root
    password:123456# 1.2 配置微服务名称application:name: orderservice

  # 1.3 配置Nacos注册中心地址cloud:nacos:discovery:server-add: localhost:8848# 2. feign配置feign:# 微服务保护组件 熔断器hystrix:enabled:true# 3. mybatis配置mybatis:configuration:map-underscore-to-camel-case:true

用户服务配置(application.yml)

# 配置服务器端口server:port:8082# 1.spring配置spring:# 1.1 配置数据库连接池datasource:driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_spring_cloud
    username: root
    password:123456# 1.2 配置微服务名称application:name: userservice

  # 1.3 配置Nacos注册中心地址cloud:nacos:discovery:server-add: localhost:8848# 2. feign配置feign:# 微服务保护组件 熔断器hystrix:enabled:true# 3. mybatis配置mybatis:configuration:map-underscore-to-camel-case:true

启动这两个服务,打开nacos平台,可以看到这两个服务

在这里插入图片描述

此时,使用Feign技术,可以使用服务名,直接访问到其他服务

在这里插入图片描述

在这里插入图片描述

降级处理方案参考(http://t.csdn.cn/BaYEe)

总结

目前市面上,微服务框架开发,注册中心和微服务通信有这两套,Eureka+Ribbon、Nacos+Feign(因为都是阿里巴巴的,称为alibaba springcloud),一般后者使用较多。

而注册中心技术Eureka、Nacos,在使用上的两点区别如下:

(1)服务名;

  • Eureka服务名称区分大小写;
  • Nacos服务名称不区分大小写;

(2)注册中心;

  • Eureka需要另外创建一个模块
  • Nacos不需要另外创建模块,但需要在配置的服务端,另外添加一个依赖

本文转载自: https://blog.csdn.net/qq_42108331/article/details/131673968
版权归原作者 何中应 所有, 如有侵权,请联系我们删除。

“注册中心技术Eureka、Nacos”的评论:

还没有评论