(一)服务发现组件 Eureka

news2024/10/6 1:40:41

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: localhost
  server:
    #关闭自我保护模式(缺省为打开)
    enable-self-preservation: false
    #扫描失效服务的间隔时间(缺省为60*1000ms)
    eviction-interval-timer-in-ms: 1000
  client:
    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 {
    @Override
    public 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 {
    @Autowired
    private 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: 9002

eureka:
  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
@EnableDiscoveryClient


public class HelloConsumer01Start {
    @Bean
    public 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 {

    @Autowired
    private DiscoveryClient discoveryClient; //是一个服务查询工具,可以连接到EurekaServer,根据服务名称去查询服务信息,注意别导错包了


    @Autowired
    private 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;
    }

    @Override
    public 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;

    @RestController
    public class HelloControllerConsumer {
        @Autowired
        private HelloService helloService;

        @RequestMapping("/testHello")
        public String sayHello(){
            return helloService.sayHello();
        }
}

启动服务,注册中心成功。

7、进行测试

http://localhost:9002/testHello

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/456063.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

两百左右哪款蓝牙耳机好?两百左右高性价比蓝牙耳机推荐

现如今&#xff0c;蓝牙耳机无疑已经成为学生党外出的标配了。比起一些动辄上千的蓝牙耳机&#xff0c;两百左右的似乎更符合学生党的选择。接下来&#xff0c;我来给大家推荐几款两百左右高性价比的蓝牙耳机&#xff0c;一起来看看吧。 一、南卡小音舱Lite2蓝牙耳机 参考价&…

Spring 循环依赖处理之三级缓存设计

一、思考 1、Spring是如何解决循环依赖问题的? 2、为什么要使用三级缓存?二级缓存能否解决问题? 3、提前暴露对象暴露的是什么? 4、主要源码 二、循环依赖 1、介绍 如上图&#xff0c;创建A之前需要先创建B,创建B之前需要先创建A,造成循环依赖。 由于A没创建完成&am…

C++:IO流

CIO流 C系统实现了一个庞大的类库&#xff0c;其中ios为基类&#xff0c;其他类都是直接或间接派生自ios类 注意&#xff1a; 1. cin为缓冲流。键盘输入的数据保存在缓冲区中&#xff0c;当要提取时&#xff0c;是从缓冲区中拿。如果一次输 入过多&#xff0c;会留在那儿慢慢…

今年SMETA审核费用即将涨价

【今年SMETA审核费用即将涨价】 SMETA全称&#xff08; Sedex Members Ethical Trade Audit &#xff09;&#xff0c;即Sedex会员社会道德贸易审核&#xff0c;它是Sedex发起的一种负责任的供应链审计方法/项目。 Sedex是一个全球性的责任商业平台&#xff0c;SMETA是审核方法…

大白话chatGPT GPT的发展区别

大白话chatGPT & GPT的发展区别 从GPT名字理解chatGPTchatGPT三步曲GPT-1到GPT-4GPT-1~GPT-4的相同点GPT-1~GPT-4的区别GPT-1——pre-training fine-tune&#xff0c;自监督学习无监督预训练有监督微调GPT-2——zero-shot&#xff0c;无监督学习&#xff0c;多任务学习GPT…

机器学习快速入门1(机器学习概念与数据预处理)

文章目录 1.机器学习介绍2 机器学习7大步骤3.数据预处理八大策略4 缺省值、异常值、重复值处理**rand、randn、randint区别**normal与randn缺失值处理判断缺失值删除缺失行填充缺失值 重复值处理异常值处理 5 抽样VS全量数据为什么需要抽样简单随机抽样等距抽样分层抽样整群抽样…

【PR 基础】轨道遮罩键、交叉溶解的简单使用

在上篇博客&#xff08;【PR 基础】裁剪工具的简单使用&#xff09;介绍了裁剪效果的使用&#xff0c;本篇博客在上篇的基础上继续添加 轨道遮罩键、交叉溶解的效果。 效果 步骤 1.可以先将恢复裁剪区域的关键帧删除 2. 接下来添加字幕&#xff0c;点击 新建-》旧版标题 点击…

Ceph入门到精通-Ceph 编排器简介

第 1 章 Ceph 编排器简介 作为存储管理员&#xff0c;您可以将 Ceph 编排器与 Cephadm 实用程序搭配使用&#xff0c;能够发现设备并在 Red Hat Ceph Storage 集群中创建服务。 1.1. 使用 Ceph Orchestrator Red Hat Ceph Storage Orchestrators 是经理模块&#xff0c;主要…

深入JVM了解Java对象实例化过程

文章目录 一、对象创建方式二、对象产生步骤1、判断对象是否已经加载、链接、初始化2、为对象分配内存空间3、处理并发问题3.1 TLAB 4、初始化零值5、完善对象内存布局的信息6、调用对象的实例化方法 <init>7、总结 三、对象的内存布局1、对象头1.1 运行时元数据&#xf…

大学生创业项目-校园外卖的创业优势在哪里?

在当今的外卖行业中&#xff0c;校园外卖已成为外卖行业的垂直分类领域。 与我们通常使用的美团、饿了么平台不同&#xff0c;校园外卖平台需要招聘校园学生和校园内外人员兼职作为校园骑手&#xff0c;完成“最后一公里”的外卖送餐方式。 对于平台运营商来说&#xff0c;配…

生成树协议三姐妹:STP、RSTP 和 MSTP,附思科和华为双厂商命令示例

在计算机网络中&#xff0c;为了保证网络拓扑结构的稳定性和可靠性&#xff0c;需要采用一些协议进行网络的管理和控制。其中&#xff0c;STP、RSTP 和 MSTP 是三种常用的网络管理协议。本文将分别介绍这三种协议&#xff0c;并且使用华为、思科两家厂商作为案例给出相应的命令…

( “树” 之 Trie) 677. 键值映射 ——【Leetcode每日一题】

知识点回顾 &#xff1a; Trie&#xff0c;又称前缀树或字典树&#xff0c;用于判断字符串是否存在或者是否具有某种字符串前缀。 ❓677. 键值映射 难度&#xff1a;中等 设计一个 map &#xff0c;满足以下几点: 字符串表示键&#xff0c;整数表示值返回具有前缀等于给定字…

Scrapy框架 -- 深度爬取并持久化保存图片

一、新建一个Scrapy项目daimg scrapy startproject daimg 二、进入该项目并创建爬虫文件daimgpc cd daimg scrapy genspider daimgpc www.xxx.com 三、修改配置文件settings.py ROBOTSTXT_OBEY False LOG_LEVEL ERROR USER_AGENT "Mozilla/5.0 (Windows NT 10.0; …

Git快速入门

Git快速入门 版本控制什么是版本控制常见的版本控制工具版本控制分类Git与SVN的主要区别 聊聊Git的历史Git环境配置软件下载启动GitGit配置 Git基本理论&#xff08;重要&#xff09;三个区域工作流程 Git项目搭建创建工作目录与常用指令本地仓库搭建克隆远程仓库 Git文件操作文…

Springsecurity课程笔记06-13章基于数据库的方法授权

动力节点Springsecurity视频课程 6 密码处理 6.1 为什么要加密&#xff1f; csdn 密码泄露事件 泄露事件经过&#xff1a;https://www.williamlong.info/archives/2933.html 泄露数据分析&#xff1a;https://blog.csdn.net/crazyhacking/article/details/10443849 6.2加密…

平均薪资28K,测试开发的涨薪史,给我看哭了...

金三银四的涨薪季要来了&#xff0c;看着身边的同事有的晋升&#xff0c;有的收获30%的涨薪&#xff0c;他们都拥有哪些影响涨薪的硬核技能呢&#xff1f;互联网行业的高薪是众所周知的&#xff0c;而测试作为互联网公司越来越重视的技术开发模块&#xff0c;薪资收入同样一路走…

Tomcat部署与优化

前言 Tomcat是一款免费、开放源代码的Web应用服务器&#xff0c;是Apache软件基金会的一个核心开源项目&#xff0c;属于轻量级应用服务器&#xff0c;通常意义上的 Web 服务器接受请求后&#xff0c;只是单纯地响应静态资源&#xff0c;如 HTML 文件&#xff0c;图片文件等&a…

深入探究C++中的仿函数和迭代器——提升你的STL技能

&#x1f4d6;作者介绍&#xff1a;22级树莓人&#xff08;计算机专业&#xff09;&#xff0c;热爱编程&#xff1c;目前在c&#xff0b;&#xff0b;阶段>——目标Windows&#xff0c;MySQL&#xff0c;Qt&#xff0c;数据结构与算法&#xff0c;Linux&#xff0c;多线程&…

若依/RuoYi-Vue,若依管理系统-启动步骤

若依RuoYi-Vue前后端项目启动流程_若依前端怎么启动_primary taste_mm的博客-CSDN博客若依官网&#xff1a;RuoYi 若依官方网站 |后台管理系统|权限管理系统|快速开发框架|企业管理系统|开源框架|微服务框架|前后端分离框架|开源后台系统|RuoYi|RuoYi-Vue|RuoYi-Cloud|RuoYi框架…

进驻Lidl利多超市利器—— EDI

Lidl利多超市是源自德国的跨国零售企业&#xff0c;成立于1973年&#xff0c;发展迅速&#xff0c;目前在欧洲拥有10,800多家门店&#xff0c;覆盖29个国家。Lidl的业务范围包括食品、饮料、家庭用品、家具、电器等多个品类。Lidl一直致力于提供高性价比的商品&#xff0c;以满…