首页 > 其他分享 >Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一

Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一

时间:2024-09-18 17:51:21浏览次数:3  
标签:5.4 openfeign 二合一 springframework service2 import org service1 public

截至目前,还剩eureka client和openfeign两个工程没有整合,但这两个工程本来就应该是一个工程

想一想,eureka client作为微服务的客户端,是真正的微服务业务处理模块;而openfeign工程作为服务间调用的例子,本就应该应用在微服务模块上,所以本章的内容不是单纯移植,而是创建两个服务模块service1和service2实现模块间调用。

理论上所有的业务模块应该有大量通用的依赖,所以可以再建立一层子模块关系,如下图所示,在工程下创建microservices模块,在microservices模块下创建service1和service2模块。

Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一_spring cloud

microservices build.gradle

subprojects {
    dependencies {
        // --- Spring boot dependencies ---
        implementation 'org.springframework.boot:spring-boot-starter-web'

        implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
        implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

        // --- Other dependencies ---
        // 要想使用lombok,两个依赖必须同时存在
        annotationProcessor 'org.projectlombok:lombok'
        compileOnly 'org.projectlombok:lombok'

    }
}

service1 build.gradle

version = '0.0.1-SNAPSHOT'

service2 build.gradle

version = '0.0.1-SNAPSHOT'

service1 application.yml

server:
  port: 9101
spring:
  application:
    name: service1
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:123456@localhost:9000/eureka/

service2 application.yml

server:
  port: 9102
spring:
  application:
    name: service2
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:123456@localhost:9000/eureka/

Service1Application

package com.hao1st.service1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class Service1Application {

    public static void main(String[] args) {
       SpringApplication.run(Service1Application.class, args);
    }

}

Service2Application

package com.hao1st.service2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class Service2Application {

    public static void main(String[] args) {
       SpringApplication.run(Service2Application.class, args);
    }

}

service1 IndexDemoController

package com.hao1st.service1.biz.indexdemo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexDemoController {

    /**
     * 浏览器访问
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "您访问的是service1的index";
    }

    /**
     * rpc访问
     * @return
     */
    @PostMapping("/pindex")
    public String postIndex() {
        return "您访问的是service1的postIndex";
    }
}

service2 IndexDemoController

package com.hao1st.service2.biz.indexdemo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexDemoController {

    /**
     * 浏览器访问
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "您访问的是service2的index";
    }

    /**
     * rpc访问
     * @return
     */
    @PostMapping("/pindex")
    public String postIndex() {
        return "您访问的是service2的postIndex";
    }
}

到此,service1和service2的对外api创建完毕。我们可以依次启动eureka、gateway、service1、service2,启动完毕后访问eureka控制面板localhost:9000查看服务注册状态,服务列表中应包含gateway、service1和service2三个服务,如下图:

Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一_spring cloud_02

接下来分别测试service1和service是否畅通,访问localhost:9001/SERVICE1/index,localhost:9001/SERVICE2/index两个地址,可以看到如下内容:

Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一_spring cloud_03

Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一_spring cloud_04

接下来,编写rpc调用代码,注意,下面所有代码都在service1里。

先看文件结构,如下图,只需新建两个文件,一个用于外部访问的controller,一个用于调用service2的接口。

Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一_gradle_05

Service2Rpc

package com.hao1st.service1.biz.rpcdemo.rpc;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

// 指定要调用的模块,value即配置文件中的spring.application.name
@FeignClient(value="service2")
public interface Service2Rpc {

    // api路径
    @PostMapping("/pindex")
    String rpcService2();
}

RpcController

package com.hao1st.service1.biz.rpcdemo.controller;

import com.hao1st.service1.biz.rpcdemo.rpc.Service2Rpc;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rpc")
public class RpcController {

    @Resource
    private Service2Rpc service2Rpc;

    @GetMapping("/invoke")
    public String rpcService2() {
        // 调用接口访问service2
        return service2Rpc.rpcService2();
    }
}

这样就OK了,访问service1的/rpc/invoke就可以调用到service2的内容,如下图:

Spring Cloud 5.4: 将多工程整合成多模块工程-eureka client + openfeign二合一_spring cloud_06

同理,service2调用service1也是同样的逻辑。

标签:5.4,openfeign,二合一,springframework,service2,import,org,service1,public
From: https://blog.51cto.com/u_8549061/12047400

相关文章

  • 【CSS in Depth 2 精译_033】5.4 Grid 网格布局的显示网格与隐式网格(中)
    当前内容所在位置(可进入专栏查看其他译好的章节内容)第一章层叠、优先级与继承(已完结)1.1层叠1.2继承1.3特殊值1.4简写属性1.5CSS渐进式增强技术1.6本章小结第二章相对单位(已完结)2.1相对单位的威力2.2em与rem2.3告别像素思维2.4视口的相对单位2.5......
  • 【CSS in Depth 2 精译_032】5.4 Grid 网格布局的显示网格与隐式网格(上)
    当前内容所在位置(可进入专栏查看其他译好的章节内容)第一章层叠、优先级与继承(已完结)1.1层叠1.2继承1.3特殊值1.4简写属性1.5CSS渐进式增强技术1.6本章小结第二章相对单位(已完结)2.1相对单位的威力2.2em与rem2.3告别像素思维2.4视口的相对单位2.5......
  • 服务治理---注册中心,openfeign
    1.为什么会有注册中心?        在微服务拆分的理念中,我们必须遵循每个服务尽量的完全独立,类独立,接口独立,方法独立,不依赖别的服务,这样就最大程度的耦合性降低。但随之而来的却是一系列问题,像我们一个接口可能就会调用别的服务的接口,这样就会逻辑上的跨越主机。但为了遵......
  • SpringCloud-04 OpenFeign服务调用与负载均衡
    OpenFeign是一个声明式、模板化的HTTP客户端,它简化了在Java应用程序中调用RESTfulAPI的过程。OpenFeign是Netflix开发的一个开源项目,它构建在Feign的基础上,为开发者提供了更加简单、灵活的方式来实现HTTP请求。OpenFeign的特点包括:前面在使用SpringCloudLoadBalancer+Res......
  • 【OpenFeign 】OpenFeign 的常用配置
    1 前言上节我们看了下OpenFeign里的重试,在从源码的角度看它的执行原理的时候,又意外的遇到了一个【OpenFeign】OpenFeign下未开启重试,服务却被调用了两次 的问题的分析,后面我们又看了重试器的入场和执行时机,那么本节我们看看OpenFeign的一些常用配置,以及全局配置和想对某......
  • 【OpenFeign 】OpenFeign 下的重试器的执行过程分析
    1 前言上节我们看了下OpenFeign里的重试,在从源码的角度看它的执行原理的时候,又意外的遇到了一个【OpenFeign】OpenFeign下未开启重试,服务却被调用了两次 的问题的分析,那本节我们就来看看重试器的一个入场以及执行的过程。2 源码分析首先我们要知道在默认的情况下,OpenF......
  • 【工具使用】【OpenFeign 】OpenFeign 下未开启重试,服务却被调用了两次
    1 前言上节我们看了下OpenFeign里的重试,后来我从源码的角度看了下它的原理,发现一个诡异的情况,就是我没开启重试的情况下,我的服务却被调用了两次,这是为什么呢?本节我们来看看这个问题。2 环境准备首先准备一下,我这里有两个服务,一个是demo一个是virtuous,本地启动的Eurek......
  • 【更新日志】AI运动识别插件又双叕发布更新了,v1.5.4版已正式发布。
    Ai运动识别插件可以为您的小程序赋于原生的人体检测、运动识别、姿态识别、运动计时计数AI能力,让您的小程序轻松实现AI健身、线上运动会、学生体测等场景,并拥有大量的用户案例,针对近期开发者的反馈,我们修复了相关问题,并对部分功能进行了优化增强,发布了v1.5.4版。本次版本的详细......
  • 【工具使用】【OpenFeign 】OpenFeign 下的重试机制
    1 前言服务间的调用,大家可能会用到OpenFeign方式。那么当被调用服务可能会因为某种情况导致调用失败(这个失败可能好似下游服务重启了或者超时断开等)的情况下,我们想重试的情况下该怎么做呢?本节我们就来看看。2 环境准备首先准备一下,我这里有两个服务,一个是demo一个是vi......
  • OpenFeign深入学习笔记
    OpenFeign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加容易。OpenFeign是在SpringCloud生态系统中的一个组件,它整合了Ribbon(客户端负载均衡器)和Eureka(服务发现组件),从而简化了微服务之间的调用。在SpringCloud应用中,我们经常会使用OpenFeign,比如......