(一)服务发现组件 Eureka
1、Eureka 简介
Eureka 是Spring Cloud Netflix 微服务套件中的一部分, 它基于Netflix Eureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。我们只需通过简单引入依赖和注解配置就能让Spring Boot 构建的微服务应用轻松地与Eureka 服务治理体系进行整合。
Eureka包含两个组件:Eureka Server(注册中心)和Eureka Client (微服务)。
Eureka Server提供服务注册服务。
Eureka Client是一个java客户端,用来简化与Eureka Server的交互、客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

2、搭建 SpringCloud 父工程

按照下图配置即可
这里不选择任何依赖。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>EurekaServer01</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.8.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.helloparent</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
删除如下:这些文件夹没有用,直接删除即可,必须保留 pom.xml。

3、 创建 Maven 工程(注册中心 Eureka)

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>demo</artifactId><groupId>com.helloparent</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>EurekaServer01</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>
在新建的maven项目下面 src-->main-->resources建立一个application.yml文件
#内置的tomcat服务启动监听端口号
server:port: 8888
#应用名称
spring:application:name: EurekaServer01
#EurekaServer配置
eureka:instance:hostname: localhostserver:#关闭自我保护模式(缺省为打开)enable-self-preservation: false#扫描失效服务的间隔时间(缺省为60*1000ms)eviction-interval-timer-in-ms: 1000client:register-with-eureka: false #此EurekaServer不在注册到其他的注册中心fetch-registry: false #不在从其他中心中心拉取服务器信息service-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #注册中心访问地址
在main下面创建文件夹及文件,注意main函数文件名。
package com.eureka.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServer01Start {public static void main(String[] args) {SpringApplication.run(EurekaServer01Start.class, args);}
}
运行查看结果。

主界面中
system status 为系统信息
General Info 为一般信息
Instances currently registered with Eureka 为注册到注册中心的所有微服务列表
4、创建第二个Maven 项目(接口)
创建文件夹及Service,如下图。

编写接口
package com.HelloInterface01.demo.service;public interface HelloService {public String sayHello();}
注释掉 父工程里面的相关代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>EurekaServer01</module><module>HelloInterface01</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.8.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.helloparent</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>--></project>
5、服务提供者创建
<?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>demo</artifactId><groupId>com.helloparent</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>HelloProvide01</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>com.helloparent</groupId><artifactId>HelloInterface01</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>
在resources文件夹下面添加一个yml。
spring:application:name: HelloProvider01
server:port: 9001
eureka:client:service-url:defaultZone: http://localhost:8888/eureka
创建如下文件夹及实现类

package com.HelloProvide.demo.service.Impl;import com.HelloInterface01.demo.service.HelloService;
import org.springframework.stereotype.Service;@Service
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello() {return "hello Eureka!";}
}
在创建一个controller 文件夹及文件
package com.HelloProvide.demo.controller;import com.HelloInterface01.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@Autowiredprivate HelloService helloService;@GetMapping("/hello")public String sayHello(){return helloService.sayHello();}
}

编写启动类。
package com.HelloProvide.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class HelloProvider01Start {public static void main(String[] args) {SpringApplication.run(HelloProvider01Start.class,args);}
}
服务已经注册上去了。

6、服务调用者创建

修改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>demo</artifactId><groupId>com.helloparent</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>HelloConsumer01</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>com.helloparent</groupId><artifactId>HelloInterface01</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>
编写yml配置文件
spring:application:name: helloConsumer01
server:port: 9002eureka:client:service-url:defaultZone: http://localhost:8888/eureka
编写启动类

package com.HelloConsumer.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableDiscoveryClientpublic class HelloConsumer01Start {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(HelloConsumer01Start.class,args);}
}
实现类代码:注意导包别错误。
package com.HelloConsumer.demo.Service.impl;import com.HelloInterface01.demo.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import java.util.List;@Service
public class HelloServiceImpl implements HelloService {@Autowiredprivate DiscoveryClient discoveryClient; //是一个服务查询工具,可以连接到EurekaServer,根据服务名称去查询服务信息,注意别导错包了@Autowiredprivate RestTemplate restTemplate; //调用rest风格接口工具类//从EurekaServer获取对应服务的地址和端口public String getServerInfo() {List<ServiceInstance> instanceList = discoveryClient.getInstances("HELLOPROVIDER01");if (instanceList != null && instanceList.size() > 0) {ServiceInstance serviceInstance = instanceList.get(0);//获取对应服务的主机地址String host = serviceInstance.getHost();//获取对应服务端口号int port = serviceInstance.getPort();return "http://" + host + ":" + port;}return null;}@Overridepublic String sayHello() {ResponseEntity<String> responseEntity = restTemplate.getForEntity(getServerInfo() + "/hello", String.class);String body = responseEntity.getBody();System.out.println("调用远程服务返回值:" + body);return body;}
}
编写controller
package com.HelloConsumer.demo.controller;import com.HelloInterface01.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloControllerConsumer {@Autowiredprivate HelloService helloService;@RequestMapping("/testHello")public String sayHello(){return helloService.sayHello();}
}
启动服务,注册中心成功。
7、进行测试
http://localhost:9002/testHello


相关文章:
(一)服务发现组件 Eureka
1、Eureka 简介 Eureka 是Spring Cloud Netflix 微服务套件中的一部分, 它基于Netflix Eureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。我们只需通过简单引入依赖和注解配置就能让Spring Boot 构建的微服务应用轻松地与Eureka 服务治理…...
学会笔记本电脑录屏快捷键,轻松实现录屏!
案例:笔记本电脑录屏有快捷键吗? 【我每次打开笔记本电脑录屏都要耗费比较长的时间,这样会影响到我录屏的效率。在这里想问一下,有没有快速打开电脑录屏的方法?】 在日常的工作、学习、娱乐中,我们经常需…...
( “树” 之 Trie) 208. 实现 Trie (前缀树) ——【Leetcode每日一题】
知识点回顾 : Trie,又称前缀树或字典树,用于判断字符串是否存在或者是否具有某种字符串前缀。 ❓208. 实现 Trie (前缀树) 难度:中等 Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构ÿ…...
算法训练Day40:343. 整数拆分 96.不同的二叉搜索树
文章目录 整数拆分题解(动态规划)贪心 不同的二叉搜索树题解 整数拆分 CategoryDifficultyLikesDislikesContestSlugProblemIndexScorealgorithmsMedium (62.22%)11660--0 Tags 数学 | 动态规划 Companies 给定一个正整数 n ,将其拆分为…...
设计模式及代码
1、工厂方法模式(Factory Method Pattern): 定义一个用于创建对象的接口,让子类决定实例化哪一个类。应用场景:当一个类不知道它所必须创建的对象的类时;一个类希望由它的子类来指定它所创建的对象时。 抽…...
9.java程序员必知必会类库之加密库
前言 密码学在计算机领域源远流长,应用广泛。当前每时每刻,每一个连接到互联网的终端,手机,电脑,iPad都会和互联网有无数次的数据交互,如果这些数据都是明文传输那将是难以想象的。为了保护用户隐私&#…...
C技能树:for循环:九九乘法表
使用for循环,打印九九乘法表。下列四个选项中有一项无法实现该功能,请找出该错误选项。 #include <stdio.h> int main(int argc, char** argv) {int i 0;int j 0;(_____1_____)return 0; } int row 0; int col 0; for(i 0; i < 8…...
Win10老是蓝屏收集错误信息重启无效怎么办?
Win10老是蓝屏收集错误信息重启无效怎么办?有用户遇到了电脑开机蓝屏的情况,收集错误信息重启电脑之后,依然无法解决问题。那么这个问题要怎么去进行解决呢?接下来我们来看看以下具体的处理方法教学吧。 准备工作: 1、…...
Redis入门学习笔记【五】Redis在分布式环境下常见的应用场景
一、分布式锁 当多个进程不在同一个系统中,用分布式锁控制多个进程对资源的操作或者访问。 与之对应有线 程锁,进程锁。 分布式锁可以避免不同进程重复相同的工作,减少资源浪费。 同时分布式锁可以避免破坏数据正 确性的发生, …...
Python ZIpFile 解惑:GBK 编码与乱码现象
文章目录 参考描述铺垫乱码现象编码与解码编码解码 字符集Unicode 字符集UTF-8CP437Zip 文件与 CP437 编码 GB2312GB2012GBK 单字节编码与多字节编码 溯源通用标志位与语言编码标志ZipFile 所支持的两种编码方式GBK 编码与 Zip 应用乱码现象产生的原因 解决 参考 项目描述维基…...
【LeetCode】213. 打家劫舍 II
213. 打家劫舍 II(中等) 思路 这道题是 198.打家劫舍 的拓展版,区别在于:本题的房间是环形排列,而198.题中的房间是单排排列。 将房间环形排列,意味着第一间房间和最后一间房间不能同时盗窃,因…...
从初识RabbitMQ到安装了解
一、同步和异步通讯 微服务间通讯有同步和异步两种方式: 同步通讯:就像打电话,需要实时响应。 异步通讯:就像发邮件,不需要马上回复。 两种方式各有优劣,打电话可以立即得到响应,但是你却不…...
MySQL(六)-字符串函数的使用解析
字符串函数的使用解析 1 计算字符串字符数的函数和字符串长的函数2 合并字符串函数 CONCAT(s1,s2,...)、CONCAT_WS(xs1,s2,...)3 替换字符串函数INSERT(s1,x,len,s2)4 字母大小写转换函数5 获取指定长度的字符串的函数LEFT(s,n)和RIGHT(s,n)6 填充字符串的函数 LPAD(s1,len,s2)…...
Zookeeper集群搭建
搭建Zookeeper集群 1.1 搭建要求 真实的集群是需要部署在不同的服务器上的,但是在我们测试时同时启动很多个虚拟机内存会吃不消,所以我们通常会搭建伪集群,也就是把所有的服务都搭建在一台虚拟机上,用端口进行区分。 我们这里要…...
【计算机视觉 | 目标检测】OVD:Open-Vocabulary Object Detection 论文工作总结(共八篇)
文章目录 一、2D open-vocabulary object detection的发展和研究现状二、基于大规模外部图像数据集2.1 OVR-CNN:Open-Vocabulary Object Detection Using Captions,CVPR 20212.2 Open Vocabulary Object Detection with Pseudo Bounding-Box Labels&…...
C++入门基础知识[博客园长期更新......]
0.博客园链接 博客的最新内容都在博客园当中,所有内容均为原创(博客园、CSDN同步更新)。 C知识点集合 1.命名空间 在往后的C编程中,将会存在大量的变量和函数,因为有大量的变量和函数,所以C的库会非常多。那么在C语言编程中&a…...
( “树” 之 BST) 501. 二叉搜索树中的众数 ——【Leetcode每日一题】
二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。 二叉查找树中序遍历有序。 ❓501. 二叉搜索树中的众数 难度:简单 给你一个含重复值的二叉搜索树(BST)的根节点 root…...
openharmony内核中不一样的双向链表
不一样的双向链表 链表初识别遍历双向链表参考链接 链表初识别 最近看openharmony的内核源码时看到一个有意思的双向链表,结构如下 typedef struct LOS_DL_LIST{struct LOS_DL_LIST *pstPrev; //前驱节点struct LOS_DL_LIST *pstNext; //后继节点 }LOS_DL_LIST;不…...
大文件删除不在回收站里怎么找回
在日常办公中,总会有一些新的文件产生,和用完后的文件清理掉。有时候不小心误删文件也是常有的事。但如果大文件删除不在回收站里怎么找回呢?遇到的小伙伴们请不要别急,只要按照下面的方法做就行了。 正常情况下删除会进入到回收站中&#x…...
Ubuntu22.04部署Pytorch2.0深度学习环境
文章目录 安装Anaconda创建新环境安装Pytorch2.0安装VS CodeUbuntu下实时查看GPU状态的方法小实验:Ubuntu、Windows10下GPU训练速度对比 Ubuntu安装完显卡驱动、CUDA和cudnn后,下面部署深度学习环境。 (安装Ubuntu系统、显卡驱动、CUDA和cudn…...
浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)
✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义(Task Definition&…...
Opencv中的addweighted函数
一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...
Cinnamon修改面板小工具图标
Cinnamon开始菜单-CSDN博客 设置模块都是做好的,比GNOME简单得多! 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...
大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计
随着大语言模型(LLM)参数规模的增长,推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长,而KV缓存的内存消耗可能高达数十GB(例如Llama2-7B处理100K token时需50GB内存&a…...
回溯算法学习
一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...
Windows安装Miniconda
一、下载 https://www.anaconda.com/download/success 二、安装 三、配置镜像源 Anaconda/Miniconda pip 配置清华镜像源_anaconda配置清华源-CSDN博客 四、常用操作命令 Anaconda/Miniconda 基本操作命令_miniconda创建环境命令-CSDN博客...
Bean 作用域有哪些?如何答出技术深度?
导语: Spring 面试绕不开 Bean 的作用域问题,这是面试官考察候选人对 Spring 框架理解深度的常见方式。本文将围绕“Spring 中的 Bean 作用域”展开,结合典型面试题及实战场景,帮你厘清重点,打破模板式回答,…...
给网站添加live2d看板娘
给网站添加live2d看板娘 参考文献: stevenjoezhang/live2d-widget: 把萌萌哒的看板娘抱回家 (ノ≧∇≦)ノ | Live2D widget for web platformEikanya/Live2d-model: Live2d model collectionzenghongtu/live2d-model-assets 前言 网站环境如下,文章也主…...
【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信 BLE Mesh协议的拓扑结构 定向转发机制
目录 节点的功能承载层(GATT/Adv)局限性: 拓扑关系定向转发机制定向转发意义 CG 节点的功能 节点的功能由节点支持的特性和功能决定。所有节点都能够发送和接收网格消息。节点还可以选择支持一个或多个附加功能,如 Configuration …...
企业大模型服务合规指南:深度解析备案与登记制度
伴随AI技术的爆炸式发展,尤其是大模型(LLM)在各行各业的深度应用和整合,企业利用AI技术提升效率、创新服务的步伐不断加快。无论是像DeepSeek这样的前沿技术提供者,还是积极拥抱AI转型的传统企业,在面向公众…...

