• spring cloud 快速上手系列 -> 01-注册中心 Eureka -> 012-Eureka客户端2


    spring cloud 快速上手系列

    系列说明:快速上手,一切从简,搭建一个简单的微服务框架,让新手可以在这个基础框架上做各种学习、研究。

    01-注册中心 Eureka

    012-Eureka客户端2

    1,工程结构

    在这里插入图片描述

    我们这次建立Client02,调用上一章Client01提供的接口。远程调用,我们使用feign。

    2,Client02
    1) 代码目录

    在这里插入图片描述

    2) 代码内容
    • pom.xml
    
    <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">
        <modelVersion>4.0.0modelVersion>
    
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.5.0version>
            <relativePath/>
        parent>
    
        <groupId>com.hui.study.cloudgroupId>
        <artifactId>StudyEurekaClient02artifactId>
        <version>1.0.0-SNAPSHOTversion>
    
        <properties>
            <java.version>1.8java.version>
            <spring-cloud.version>2020.0.5spring-cloud.version>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring-cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-openfeignartifactId>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • application.yml
    server:
      port: 8002  #Client02的端口号
    spring:
      application:
        name: Client02
    eureka:
      client:
        #表示是否将自己注册进EurekaServer
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。
        fetchRegistry: true
        service-url:
          #服务中心地址
          defaultZone: http://localhost:7001/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • CloudEurekaClient02Application.java
    package com.hui.study.cloud.eureka;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    /**
     * 注册中心客户端
     * 微服务02,服务调用方,调用端
     */
    public class CloudEurekaClient02Application {
        public static void main(String[] args) {
            SpringApplication.run(CloudEurekaClient02Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • Demo01Rpc.java
      这个interface就是远程调用的核心。FeignClient注解是关键。
    package com.hui.study.cloud.eureka.rpc;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.cloud.openfeign.FeignClientsConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(value = "Client01", configuration = FeignClientsConfiguration.class)
    public interface Demo01Rpc {
        @RequestMapping("/demo/d01")
        String demo01(@RequestParam("param") String param);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • DemoController.java
    package com.hui.study.cloud.eureka.controller;
    
    import com.hui.study.cloud.eureka.rpc.Demo01Rpc;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * 调用的demo
     */
    @RestController
    @RequestMapping("/demo")
    public class DemoController {
        @Autowired
        private Demo01Rpc demo01Rpc;
    
        @RequestMapping("/d02")
        public String demo02(String param) {
            return "远程调用:" + demo01Rpc.demo01(param);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    3) 启动

    执行 CloudEurekaClient02Application.java
    注册中心保持启动状态下,访问 http://localhost:7001
    在这里插入图片描述

    client02也已经注册上来了

    4)调用

    用postman或者apifox等工具,访问:http://localhost:8002/demo/d02
    在这里插入图片描述

    5)代码及作业

    01-注册中心 Eureka这一章的代码如下
    链接:https://pan.baidu.com/s/1wPgPc0k-RaV0jLEzswvuGw?pwd=dlhs
    提取码:dlhs

    链接:https://pan.baidu.com/s/1VLhsOCnVSvVJsg3nBBI2ow?pwd=8whb
    提取码:8whb

    链接:https://pan.baidu.com/s/1WAXp0o-2leR6R1NJJaFZ0A?pwd=j56k
    提取码:j56k

    作业:
    大家可以自行在client02里面增加一个被调用的函数,在client01里面去调用。
    这样,两个 eureka客户端实现了互相调用。
    当然,实际大工程里面,是比较忌讳这种调用的,互相调用、循环调用,都会增加系统的复杂度。这里这么做的目标只是学习。

  • 相关阅读:
    代码规范HelloWorld
    Nebula Graph + Plato调研总结
    pytorch TORCH.NN 到底是什么?
    中小学教师资格考试介绍
    少儿编程 电子学会图形化编程等级考试Scratch三级真题解析(选择题)2022年6月
    J2EE项目部署与发布(Linux版本)->jdk&tomcat安装,MySQL安装,后端接口部署,linux单体项目前端部署
    【Acwing166】数独(dfs+剪枝+位运算)超级详细题解!
    Graph Representation Learning学习笔记-chapter5
    Kotlin进阶指南 - Parcelable序列化
    基本算法——二分查找
  • 原文地址:https://blog.csdn.net/yihui823/article/details/126803517