当前位置: 首页 > news >正文

(一)服务发现组件 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 微服务套件中的一部分&#xff0c; 它基于Netflix Eureka 做了二次封装&#xff0c; 主要负责完成微服务架构中的服务治理功能。我们只需通过简单引入依赖和注解配置就能让Spring Boot 构建的微服务应用轻松地与Eureka 服务治理…...

学会笔记本电脑录屏快捷键,轻松实现录屏!

案例&#xff1a;笔记本电脑录屏有快捷键吗&#xff1f; 【我每次打开笔记本电脑录屏都要耗费比较长的时间&#xff0c;这样会影响到我录屏的效率。在这里想问一下&#xff0c;有没有快速打开电脑录屏的方法&#xff1f;】 在日常的工作、学习、娱乐中&#xff0c;我们经常需…...

( “树” 之 Trie) 208. 实现 Trie (前缀树) ——【Leetcode每日一题】

知识点回顾 &#xff1a; Trie&#xff0c;又称前缀树或字典树&#xff0c;用于判断字符串是否存在或者是否具有某种字符串前缀。 ❓208. 实现 Trie (前缀树) 难度&#xff1a;中等 Trie&#xff08;发音类似 “try”&#xff09;或者说 前缀树 是一种树形数据结构&#xff…...

算法训练Day40:343. 整数拆分 96.不同的二叉搜索树

文章目录 整数拆分题解&#xff08;动态规划&#xff09;贪心 不同的二叉搜索树题解 整数拆分 CategoryDifficultyLikesDislikesContestSlugProblemIndexScorealgorithmsMedium (62.22%)11660--0 Tags 数学 | 动态规划 Companies 给定一个正整数 n &#xff0c;将其拆分为…...

设计模式及代码

1、工厂方法模式&#xff08;Factory Method Pattern&#xff09;&#xff1a; 定义一个用于创建对象的接口&#xff0c;让子类决定实例化哪一个类。应用场景&#xff1a;当一个类不知道它所必须创建的对象的类时&#xff1b;一个类希望由它的子类来指定它所创建的对象时。 抽…...

9.java程序员必知必会类库之加密库

前言 密码学在计算机领域源远流长&#xff0c;应用广泛。当前每时每刻&#xff0c;每一个连接到互联网的终端&#xff0c;手机&#xff0c;电脑&#xff0c;iPad都会和互联网有无数次的数据交互&#xff0c;如果这些数据都是明文传输那将是难以想象的。为了保护用户隐私&#…...

C技能树:for循环:九九乘法表

使用for循环&#xff0c;打印九九乘法表。下列四个选项中有一项无法实现该功能&#xff0c;请找出该错误选项。 #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老是蓝屏收集错误信息重启无效怎么办&#xff1f;有用户遇到了电脑开机蓝屏的情况&#xff0c;收集错误信息重启电脑之后&#xff0c;依然无法解决问题。那么这个问题要怎么去进行解决呢&#xff1f;接下来我们来看看以下具体的处理方法教学吧。 准备工作&#xff1a; 1、…...

Redis入门学习笔记【五】Redis在分布式环境下常见的应用场景

一、分布式锁 当多个进程不在同一个系统中&#xff0c;用分布式锁控制多个进程对资源的操作或者访问。 与之对应有线 程锁&#xff0c;进程锁。 分布式锁可以避免不同进程重复相同的工作&#xff0c;减少资源浪费。 同时分布式锁可以避免破坏数据正 确性的发生&#xff0c; …...

Python ZIpFile 解惑:GBK 编码与乱码现象

文章目录 参考描述铺垫乱码现象编码与解码编码解码 字符集Unicode 字符集UTF-8CP437Zip 文件与 CP437 编码 GB2312GB2012GBK 单字节编码与多字节编码 溯源通用标志位与语言编码标志ZipFile 所支持的两种编码方式GBK 编码与 Zip 应用乱码现象产生的原因 解决 参考 项目描述维基…...

【LeetCode】213. 打家劫舍 II

213. 打家劫舍 II&#xff08;中等&#xff09; 思路 这道题是 198.打家劫舍 的拓展版&#xff0c;区别在于&#xff1a;本题的房间是环形排列&#xff0c;而198.题中的房间是单排排列。 将房间环形排列&#xff0c;意味着第一间房间和最后一间房间不能同时盗窃&#xff0c;因…...

从初识RabbitMQ到安装了解

一、同步和异步通讯 微服务间通讯有同步和异步两种方式&#xff1a; 同步通讯&#xff1a;就像打电话&#xff0c;需要实时响应。 异步通讯&#xff1a;就像发邮件&#xff0c;不需要马上回复。 两种方式各有优劣&#xff0c;打电话可以立即得到响应&#xff0c;但是你却不…...

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 搭建要求 真实的集群是需要部署在不同的服务器上的&#xff0c;但是在我们测试时同时启动很多个虚拟机内存会吃不消&#xff0c;所以我们通常会搭建伪集群&#xff0c;也就是把所有的服务都搭建在一台虚拟机上&#xff0c;用端口进行区分。 我们这里要…...

【计算机视觉 | 目标检测】OVD:Open-Vocabulary Object Detection 论文工作总结(共八篇)

文章目录 一、2D open-vocabulary object detection的发展和研究现状二、基于大规模外部图像数据集2.1 OVR-CNN&#xff1a;Open-Vocabulary Object Detection Using Captions&#xff0c;CVPR 20212.2 Open Vocabulary Object Detection with Pseudo Bounding-Box Labels&…...

C++入门基础知识[博客园长期更新......]

0.博客园链接 博客的最新内容都在博客园当中&#xff0c;所有内容均为原创(博客园、CSDN同步更新)。 C知识点集合 1.命名空间 在往后的C编程中&#xff0c;将会存在大量的变量和函数&#xff0c;因为有大量的变量和函数&#xff0c;所以C的库会非常多。那么在C语言编程中&a…...

( “树” 之 BST) 501. 二叉搜索树中的众数 ——【Leetcode每日一题】

二叉查找树&#xff08;BST&#xff09;&#xff1a;根节点大于等于左子树所有节点&#xff0c;小于等于右子树所有节点。 二叉查找树中序遍历有序。 ❓501. 二叉搜索树中的众数 难度&#xff1a;简单 给你一个含重复值的二叉搜索树&#xff08;BST&#xff09;的根节点 root…...

openharmony内核中不一样的双向链表

不一样的双向链表 链表初识别遍历双向链表参考链接 链表初识别 最近看openharmony的内核源码时看到一个有意思的双向链表&#xff0c;结构如下 typedef struct LOS_DL_LIST{struct LOS_DL_LIST *pstPrev; //前驱节点struct LOS_DL_LIST *pstNext; //后继节点 }LOS_DL_LIST;不…...

大文件删除不在回收站里怎么找回

在日常办公中&#xff0c;总会有一些新的文件产生&#xff0c;和用完后的文件清理掉。有时候不小心误删文件也是常有的事。但如果大文件删除不在回收站里怎么找回呢?遇到的小伙伴们请不要别急&#xff0c;只要按照下面的方法做就行了。 正常情况下删除会进入到回收站中&#x…...

Ubuntu22.04部署Pytorch2.0深度学习环境

文章目录 安装Anaconda创建新环境安装Pytorch2.0安装VS CodeUbuntu下实时查看GPU状态的方法小实验&#xff1a;Ubuntu、Windows10下GPU训练速度对比 Ubuntu安装完显卡驱动、CUDA和cudnn后&#xff0c;下面部署深度学习环境。 &#xff08;安装Ubuntu系统、显卡驱动、CUDA和cudn…...

接口测试中缓存处理策略

在接口测试中&#xff0c;缓存处理策略是一个关键环节&#xff0c;直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性&#xff0c;避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明&#xff1a; 一、缓存处理的核…...

Java 语言特性(面试系列1)

一、面向对象编程 1. 封装&#xff08;Encapsulation&#xff09; 定义&#xff1a;将数据&#xff08;属性&#xff09;和操作数据的方法绑定在一起&#xff0c;通过访问控制符&#xff08;private、protected、public&#xff09;隐藏内部实现细节。示例&#xff1a; public …...

日语学习-日语知识点小记-构建基础-JLPT-N4阶段(33):にする

日语学习-日语知识点小记-构建基础-JLPT-N4阶段(33):にする 1、前言(1)情况说明(2)工程师的信仰2、知识点(1) にする1,接续:名词+にする2,接续:疑问词+にする3,(A)は(B)にする。(2)復習:(1)复习句子(2)ために & ように(3)そう(4)にする3、…...

《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析 (一)

CSI-2 协议详细解析 (一&#xff09; 1. CSI-2层定义&#xff08;CSI-2 Layer Definitions&#xff09; 分层结构 &#xff1a;CSI-2协议分为6层&#xff1a; 物理层&#xff08;PHY Layer&#xff09; &#xff1a; 定义电气特性、时钟机制和传输介质&#xff08;导线&#…...

Opencv中的addweighted函数

一.addweighted函数作用 addweighted&#xff08;&#xff09;是OpenCV库中用于图像处理的函数&#xff0c;主要功能是将两个输入图像&#xff08;尺寸和类型相同&#xff09;按照指定的权重进行加权叠加&#xff08;图像融合&#xff09;&#xff0c;并添加一个标量值&#x…...

linux 错误码总结

1,错误码的概念与作用 在Linux系统中,错误码是系统调用或库函数在执行失败时返回的特定数值,用于指示具体的错误类型。这些错误码通过全局变量errno来存储和传递,errno由操作系统维护,保存最近一次发生的错误信息。值得注意的是,errno的值在每次系统调用或函数调用失败时…...

【Go】3、Go语言进阶与依赖管理

前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课&#xff0c;做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程&#xff0c;它的核心机制是 Goroutine 协程、Channel 通道&#xff0c;并基于CSP&#xff08;Communicating Sequential Processes&#xff0…...

ios苹果系统,js 滑动屏幕、锚定无效

现象&#xff1a;window.addEventListener监听touch无效&#xff0c;划不动屏幕&#xff0c;但是代码逻辑都有执行到。 scrollIntoView也无效。 原因&#xff1a;这是因为 iOS 的触摸事件处理机制和 touch-action: none 的设置有关。ios有太多得交互动作&#xff0c;从而会影响…...

2023赣州旅游投资集团

单选题 1.“不登高山&#xff0c;不知天之高也&#xff1b;不临深溪&#xff0c;不知地之厚也。”这句话说明_____。 A、人的意识具有创造性 B、人的认识是独立于实践之外的 C、实践在认识过程中具有决定作用 D、人的一切知识都是从直接经验中获得的 参考答案: C 本题解…...

RSS 2025|从说明书学习复杂机器人操作任务:NUS邵林团队提出全新机器人装配技能学习框架Manual2Skill

视觉语言模型&#xff08;Vision-Language Models, VLMs&#xff09;&#xff0c;为真实环境中的机器人操作任务提供了极具潜力的解决方案。 尽管 VLMs 取得了显著进展&#xff0c;机器人仍难以胜任复杂的长时程任务&#xff08;如家具装配&#xff09;&#xff0c;主要受限于人…...