项目示例 - 3.服务调用 - 1.Openfeign

项目示例 - 3.服务调用 - 1.Openfeign

关联知识:

  • 分布式微服务 - 3.服务调用 - 2.Openfeign

内容提要:

  • 服务调用实现:原生方式、openfeign

服务调用实现

原生方式调用

服务注册中心使用nacos。

项目示例步骤:

  1. 建Module:微服务起名为openfeign-consumer
  2. 改pom:引入以下依赖
xml 复制代码
    <dependencies>
        <!--Nacos服务注册-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 写yml:在resources目录下创建application.yml文件,并做以下配置
yml 复制代码
server:
  port: 8003

spring:
  application:
    # 服务注册时使用的别名
    name: openfeign-consumer
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
  1. 主启动:在src下创建如下主启动类
java 复制代码
package learn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OpenfeignConsumer {

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

}
  1. 业务构建:创建如下配置类和controller类
java 复制代码
package learn.demo.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig {

    // 在配置类中注入bean使用,可以为所有的http请求统一进行配置
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

}
java 复制代码
package learn.demo.controller;

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;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/openfeign/consumer/")
public class ConsumerController {
    // 提供服务提供者注册时使用的别名
    private static final String URL_PAYMENT_PRE_ALIAS = "http://nacos-provider/nacos/provider/";
    // 服务提供者提供的接口
    private static final String PAYMENT_API = "test";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("test/alias")
    public String testAlias() {
        return restTemplate.getForObject(URL_PAYMENT_PRE_ALIAS+PAYMENT_API, String.class);
    }

}
  1. 测试:
    1. 启动nacos,浏览器中能正常打开首页
    2. 启动nacos-provider微服务,浏览器中输入localhost:8001/nacos/provider/test 访问微服务接口,能正确返回信息
    3. 启动本微服务,浏览器中输入localhost:8003/openfeign/consumer/test/alias 访问微服务接口,能正确返回信息

使用Openfeign调用

服务注册中心使用nacos。

项目示例步骤:

  1. 建Module:微服务起名为openfeign-consumer1
  2. 改pom:引入以下依赖
xml 复制代码
    <dependencies>
        <!--Openfeign服务调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--Nacos依赖中不包括loadbalancer依赖,而openfeign需要loadbalancer依赖,因此需要单独引入-->
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <!--Nacos服务注册-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 写yml:在resources目录下创建application.yml文件,并做以下配置
yml 复制代码
server:
  port: 8004

spring:
  application:
    # 服务注册时使用的别名
    name: openfeign-consumer1
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
  1. 主启动:在src下创建如下主启动类
java 复制代码
package learn.demo;

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

@SpringBootApplication
@EnableFeignClients
public class OpenfeignConsumer1 {

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

}
  1. 创建服务调用接口:依照服务提供者nacos-provider微服务,创建如下接口类
java 复制代码
package learn.demo.inter;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

@Component
@FeignClient(value = "nacos-provider", path = "/nacos/provider/")
public interface ProviderInter {

    @GetMapping("test")
    String test();

}
  1. 业务构建:创建如下controller类
java 复制代码
package learn.demo.controller;

import jakarta.annotation.Resource;
import learn.demo.inter.ProviderInter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/openfeign/consumer1/")
public class ConsumerController {
    @Resource
    private ProviderInter providerInter;

    @GetMapping("test")
    public String test() {
        return providerInter.test();
    }

}
  1. 测试:
    1. 启动nacos,浏览器中能正常打开首页
    2. 启动nacos-provider微服务,浏览器中输入localhost:8001/nacos/provider/test 访问微服务接口,能正确返回信息
    3. 启动本微服务,浏览器中输入localhost:8004/openfeign/consumer1/test 访问微服务接口,能正确返回信息
相关推荐
计算机毕设VX:Fegn08955 小时前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
没差c6 小时前
springboot集成flyway
java·spring boot·后端
三水不滴6 小时前
Redis 过期删除与内存淘汰机制
数据库·经验分享·redis·笔记·后端·缓存
笨蛋不要掉眼泪7 小时前
Spring Boot集成LangChain4j:与大模型对话的极速入门
java·人工智能·后端·spring·langchain
像少年啦飞驰点、8 小时前
零基础入门 Spring Boot:从“Hello World”到可上线微服务的完整学习指南
java·spring boot·微服务·编程入门·后端开发
Renhao-Wan8 小时前
从零部署Spring Cloud微服务系统(Kiwi-Hub)
spring·spring cloud·微服务
indexsunny10 小时前
互联网大厂Java面试实战:从Spring Boot到微服务架构的技术问答解析
java·spring boot·redis·微服务·kafka·jwt·flyway
sheji341610 小时前
【开题答辩全过程】以 基于SpringBoot的疗养院管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
短剑重铸之日10 小时前
《设计模式》第六篇:装饰器模式
java·后端·设计模式·装饰器模式