SpringCloud(H版&alibaba)框架开发教程,使用eureka,zookeeper,consul,nacos做注册中心——附源码(1)

源码地址:https://gitee.com/jackXUYY/springboot-example

创建订单服务,支付服务,公共api服务(共用的实体),eureka服务

1.cloud-consumer-order80

2.cloud-provider-payment8001

3.cloud-api-commons

4.cloud-eureka-server7001

5.cloud-eureka-server7002

如图所示

修改本地host映射

修改数据库地址为自己的地址,并初始化表结构和数据

sql 复制代码
/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50562
 Source Host           : localhost:3306
 Source Schema         : db2019

 Target Server Type    : MySQL
 Target Server Version : 50562
 File Encoding         : 65001

 Date: 28/12/2023 15:24:40
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for payment
-- ----------------------------
DROP TABLE IF EXISTS `payment`;
CREATE TABLE `payment`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `serial` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of payment
-- ----------------------------
INSERT INTO `payment` VALUES (1, '尚硅谷');
INSERT INTO `payment` VALUES (2, 'alibaba');
INSERT INTO `payment` VALUES (3, '京东');
INSERT INTO `payment` VALUES (4, '头条');

SET FOREIGN_KEY_CHECKS = 1;

启动服务

1.cloud-consumer-order80

2.cloud-provider-payment8001

3.cloud-eureka-server7001

4.cloud-eureka-server7002

然后在浏览器输入http://localhost:7001/http://localhost:7002/

看服务是否注册

这里eureka的服务注册与发现测试完成

多个支付服务注册进eureka,订单服务调用支付--负载均衡

创建另一个支付服务

1.cloud-provider-payment8002

启动服务,注册到eureka,可以看到支付服务有2个注册到eureka,接下来,使用restTemplate在订单服务中调支付服务,并且负载均衡,使用自定义的策略

现如今我们已经启动了5个微服务,如果电脑不行,可以使用单机版本,并且服务启动的时候指定好jvm参数,把内存限制的小一点

order80的自定义配置,为了演示负载均衡

访问localhost/consumer/payment/get/1


接下来我们使用zk当注册中心

使用到的服务

1.cloud-consumerzk-order80

2.cloud-provider-payment8004

准备zk环境,pom的zk版本要和zk的一样

idea安装zk插件

https://www.jianshu.com/p/5133ae3135be

启动服务后可以看到zk创建了2个服务节点

注册中心使用consul (省略)

链接:https://pan.baidu.com/s/1Hlkqq307hTYCWwuSM_tBHw?pwd=4t33

提取码:4t33

--来自百度网盘超级会员V7的分享

使用nacos

nacos环境,当注册中心,还能当配置中心

链接:https://pan.baidu.com/s/1JmzQvqAFoYL1PaFWQTsNKA?pwd=t31r

提取码:t31r

--来自百度网盘超级会员V7的分享

nacos使用教程

https://blog.csdn.net/m0_72673485/article/details/128622922

https://blog.csdn.net/weixin_60257072/article/details/128690024

我只启动了order的两个服务,看下效果


相关推荐
iiYcyk8 天前
Eureka与Nacos的区别-服务注册+配置管理
springcloud
iiYcyk9 天前
Hystrix与Sentinel-熔断限流
hystrix·sentinel·springcloud
iiYcyk10 天前
Ribbon和LoadBalance-负载均衡
springcloud
hqxstudying10 天前
Kafka 深入研究:从架构革新到性能优化的全面解析
java·开发语言·微服务·kafka·springcloud
瑞瑞绮绮11 天前
分布式事务的Java实践
java·分布式·springcloud
麦兜*17 天前
MongoDB 聚合管道(Aggregation)高级用法:数据统计与分析
java·数据库·后端·mongodb·springboot·springcloud
麦兜*19 天前
Spring Boot 集成 Docker 构建与发版完整指南
java·spring boot·后端·spring·docker·系统架构·springcloud
毛小茛1 个月前
Spring Cloud Gateway 实现登录校验:构建统一认证入口
springcloud
●VON1 个月前
重生之我在暑假学习微服务第七天《微服务之服务治理篇》
java·学习·微服务·云原生·nacos·架构·springcloud
Rancemy2 个月前
springcloud03-Nacos配置中心
java·intellij-idea·springcloud