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

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

关联知识:

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

内容提要:

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

服务调用实现

原生方式调用

服务注册中心使用nacos。

项目示例步骤:

  1. 建Module:微服务起名为openfeign-consumer
  2. 改pom:引入以下依赖
    <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文件,并做以下配置
server:
  port: 8003

spring:
  application:
    # 服务注册时使用的别名
    name: openfeign-consumer
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
  1. 主启动:在src下创建如下主启动类
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类
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();
    }

}
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:引入以下依赖
    <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文件,并做以下配置
server:
  port: 8004

spring:
  application:
    # 服务注册时使用的别名
    name: openfeign-consumer1
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
  1. 主启动:在src下创建如下主启动类
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微服务,创建如下接口类
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类
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 访问微服务接口,能正确返回信息
  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以为您提供OpenFeign远程调用示例。首先,请确保您已经在项目中引入了OpenFeign的依赖。 接下来,您需要定义一个Feign客户端接口,用于声明远程调用的方法。这里以调用一个名为"example-service"的远程服务为例,示例代码如下: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/api/example") String getExampleData(); } ``` 在上述示例中,我们使用`@FeignClient`注解指定了远程服务的名称,这里是"example-service"。然后通过`@GetMapping`注解指定了远程调用的URL路径。 接下来,您需要在启动类上添加`@EnableFeignClients`注解,以启用Feign客户端的自动配置。 完成以上准备工作后,您就可以在需要调用远程服务的地方注入`ExampleServiceClient`接口,并使用其中定义的方法进行远程调用了。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ExampleController { @Autowired private ExampleServiceClient exampleServiceClient; @GetMapping("/example") public String getExampleData() { return exampleServiceClient.getExampleData(); } } ``` 在上述示例中,我们通过`@Autowired`注解将`ExampleServiceClient`接口注入到`ExampleController`中,并在`getExampleData()`方法中调用该接口的方法。 这就是一个简单的OpenFeign远程调用示例。您可以根据实际需要,按照类似的方式进行远程调用。希望对您有帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值