文章目录
- 前言
- 1、Eureka 两大组件
- 2、Eureka 服务注册与发现
- 3、案例
- 3.1、创建主工程
- 3.1.1、主工程pom.xml配置
- 3.2、创建子公共模块common-api
- 3.2.1、添加module
- 3.2.2、pom.xml配置
- 3.2.3、maven非springboot项目,增加main入口
- 3.3、创建Eureka注册中心模块eureka-server
- 3.3.1、配置pom.xml
- 3.3.2、配置application.yml
- 3.3.3、启动eureka-server
- 3.3.3.1、编译eureka-server
- 3.3.3.2、运行EurekaServerApplication.java文件
- 3.4、创建用户服务模块user-service
- 3.4.1、配置pom.xml
- 3.4.2、配置application.yml
- 3.4.3、启动user-service
- 3.4.3.1、编译user-service
- 3.4.3.2、运行UserApplication.java文件
- 3.4.3.3、测试
- 3.5、查看编译后的包
前言
Eureka 是 Netflix 公司开发的一款开源的服务注册与发现组件。
Spring Cloud 使用 Spring Boot 思想为 Eureka 增加了自动化配置,开发人员只需要引入相关依赖和注解,就能将 Spring Boot 构建的微服务轻松地与 Eureka 进行整合。1、Eureka 两大组件
Eureka 采用 CS(Client/Server,客户端/服务器) 架构,它包括以下两大组件:Eureka Server、Eureka Client
组件 介绍 Eureka Server Eureka 服务注册中心,主要用于提供服务注册功能 Eureka Client Eureka 客户端,通常指的是微服务系统中各个微服务 2、Eureka 服务注册与发现
功能 介绍 服务注册中心(Register Service) 它是一个 Eureka Server,用于提供服务注册和发现功能。 服务提供者(Provider Service) 它是一个 Eureka Client,用于提供服务。它将自己提供的服务注册到服务注册中心,以供服务消费者发现。 服务消费者(Consumer Service) 它是一个 Eureka Client,用于消费服务。它可以从服务注册中心获取服务列表,调用所需的服务。 3、案例
3.1、创建主工程
名称:SpringCloud
3.1.1、主工程pom.xml配置
4.0.0 pom org.springframework.boot spring-boot-starter-parent 2.6.13 com.hqyj drp 0.0.1-SNAPSHOT drp-parent Demo project for Spring Boot 8 8 UTF-8 1.8 1.8 4.12 1.2.17 1.16.18 org.springframework.cloud spring-cloud-dependencies 2021.0.5 pom import org.apache.maven.plugins maven-compiler-plugin 3.5.1 ${maven.compiler.source} ${maven.compiler.target} 3.2、创建子公共模块common-api
3.2.1、添加module
3.2.2、pom.xml配置
4.0.0 com.hqyj SpringCloud 0.0.1-SNAPSHOT common-api 8 8 UTF-8 org.projectlombok lombok 3.2.3、maven非springboot项目,增加main入口
添加Main.java,指定main入口,防止Maven package / install打包失败
public class Main { public static void main(String[] args) { System.out.println("common-api"); } }
3.3、创建Eureka注册中心模块eureka-server
3.3.1、配置pom.xml
4.0.0 com.hqyj SpringCloud 0.0.1-SNAPSHOT eureka-server 8 8 UTF-8 true org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.projectlombok lombok true com.github.enesusta spring-devtools 1.0.1 true org.springframework.cloud spring-cloud-starter-netflix-eureka-server 3.3.2、配置application.yml
在resource目录下,新建application.yml文件
编辑application.yml文件,添加eureka配置server: port: 7001 eureka: instance: hostname: localhost #eureka服务端的实例名称, client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机版服务注册中心
3.3.3、启动eureka-server
创建EurekaServerApplication.java启动文件
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class,args); } }
3.3.3.1、编译eureka-server
3.3.3.2、运行EurekaServerApplication.java文件
启动:http://localhost:7001/3.4、创建用户服务模块user-service
3.4.1、配置pom.xml
4.0.0 com.hqyj SpringCloud 0.0.1-SNAPSHOT user-service 8 8 UTF-8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-starter-test test junit junit 4.12 org.springframework springloaded 1.2.8.RELEASE org.springframework.cloud spring-cloud-starter-netflix-eureka-client com.hqyj common-api 0.0.1-SNAPSHOT 3.4.2、配置application.yml
server: port: 8001 spring: application: name: user-service #微服务名称 eureka: client: #将客户端注册到 eureka 服务列表内 service-url: defaultZone: http://localhost:7001/eureka #这个地址是 7001注册中心在 application.yml 中暴露出来额注册地址 (单机版)
3.4.3、启动user-service
创建UserApplication.java启动文件
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class,args); } }
3.4.3.1、编译user-service
3.4.3.2、运行UserApplication.java文件
3.4.3.3、测试
http://localhost:8001/user/userInfoList
3.5、查看编译后的包
猜你喜欢
- 3天前(大理悦云雅阁酒店电话)雅阁酒店集团|端午佳节礼遇,大理悦云雅阁度假酒店
- 3天前(郭富城热舞劲歌演唱会)郭富城年度压轴《新濠尊属系列郭富城梦幻舞林演唱会2023》
- 3天前(三亚太阳湾柏悦度假酒店)三亚太阳湾柏悦酒店携手ROSEONLY诺誓缔造浪漫七夕
- 3天前(2025年“文化和自然遗产日”广东主会场活动举办)2025年“文化和自然遗产日”广东主会场活动举办
- 3天前(当科学邂逅喜剧:科技馆喜剧嘉年华背后的"文旅破壁者")当科学邂逅喜剧:科技馆喜剧嘉年华背后的"文旅破壁者"
- 3天前(安岚度假村及酒店推出"山海之约"目的地婚礼计划)安岚度假村及酒店推出"山海之约"目的地婚礼计划
- 3天前(美诺酒店集团旗下臻选品牌m collection)美诺酒店集团启动盛橡品牌战略焕新 开启全球扩张新篇章
- 3天前(殷建祥简历)全国十大牛商解码:殷建祥如何用178天技术突围打造星空梦星空房
- 3天前(内蒙古冬季旅游攻略)内蒙古冬日奇遇:携程租车带你策马踏雪
- 3天前(我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章