当前位置: 首页 > 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…...

后进先出(LIFO)详解

LIFO 是 Last In, First Out 的缩写&#xff0c;中文译为后进先出。这是一种数据结构的工作原则&#xff0c;类似于一摞盘子或一叠书本&#xff1a; 最后放进去的元素最先出来 -想象往筒状容器里放盘子&#xff1a; &#xff08;1&#xff09;你放进的最后一个盘子&#xff08…...

【网络】每天掌握一个Linux命令 - iftop

在Linux系统中&#xff0c;iftop是网络管理的得力助手&#xff0c;能实时监控网络流量、连接情况等&#xff0c;帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...

理解 MCP 工作流:使用 Ollama 和 LangChain 构建本地 MCP 客户端

&#x1f31f; 什么是 MCP&#xff1f; 模型控制协议 (MCP) 是一种创新的协议&#xff0c;旨在无缝连接 AI 模型与应用程序。 MCP 是一个开源协议&#xff0c;它标准化了我们的 LLM 应用程序连接所需工具和数据源并与之协作的方式。 可以把它想象成你的 AI 模型 和想要使用它…...

UDP(Echoserver)

网络命令 Ping 命令 检测网络是否连通 使用方法: ping -c 次数 网址ping -c 3 www.baidu.comnetstat 命令 netstat 是一个用来查看网络状态的重要工具. 语法&#xff1a;netstat [选项] 功能&#xff1a;查看网络状态 常用选项&#xff1a; n 拒绝显示别名&#…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

有限自动机到正规文法转换器v1.0

1 项目简介 这是一个功能强大的有限自动机&#xff08;Finite Automaton, FA&#xff09;到正规文法&#xff08;Regular Grammar&#xff09;转换器&#xff0c;它配备了一个直观且完整的图形用户界面&#xff0c;使用户能够轻松地进行操作和观察。该程序基于编译原理中的经典…...

初探Service服务发现机制

1.Service简介 Service是将运行在一组Pod上的应用程序发布为网络服务的抽象方法。 主要功能&#xff1a;服务发现和负载均衡。 Service类型的包括ClusterIP类型、NodePort类型、LoadBalancer类型、ExternalName类型 2.Endpoints简介 Endpoints是一种Kubernetes资源&#xf…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...

给网站添加live2d看板娘

给网站添加live2d看板娘 参考文献&#xff1a; stevenjoezhang/live2d-widget: 把萌萌哒的看板娘抱回家 (ノ≧∇≦)ノ | Live2D widget for web platformEikanya/Live2d-model: Live2d model collectionzenghongtu/live2d-model-assets 前言 网站环境如下&#xff0c;文章也主…...