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的两个服务,看下效果


相关推荐
Mr.wangh7 天前
SpringCloudConfig(配置中心)
大数据·elasticsearch·搜索引擎·springcloud·config
阿拉斯攀登12 天前
Spring Cloud Alibaba 生态中 RocketMQ 最佳实践
分布式·微服务·rocketmq·springcloud·cloudalibaba
阿拉斯攀登18 天前
SpringCloudAlibaba之Nacos
微服务·服务发现·springcloud
阿拉斯攀登18 天前
深入微服务配置中心:Nacos注册中心的实操细节
java·微服务·云原生·springcloud
小坏讲微服务19 天前
Spring Boot 4.0 与 MyBatis Plus 整合完整指南
java·spring boot·后端·mybatis·springcloud·mybatis plus·java开发
咖啡不甜不好喝20 天前
sentinel踩坑记录
sentinel·springcloud
她说..20 天前
Spring AOP 操作日志框架(CV可用)
java·jvm·spring·springboot·springcloud
better_liang22 天前
每日Java面试场景题知识点之-分布式事务处理
java·微服务·面试·springcloud·分布式事务
ruleslol1 个月前
Spring Cloud Gateway 断言完全讲解
springcloud
whltaoin1 个月前
【 Java微服务 】Spring Cloud Alibaba :Nacos 注册中心与配置中心全攻略(含服务发现、负载均衡与动态配置)
java·微服务·nacos·springcloud·注册中心·配置中心