spring cloud alibaba Nacos 整合 OpenFeign

Feign 的简单介绍

Feign 框架进行RPC调用,会按照http协议来封装请求数据,数据格式都是按照http协议里的请求来做的,底层通过tcp连接发送。其基本原理是服务B向注册中心注册自己,服务A通过Ribbon发现服务。然后Feign通过封装好的代码发送http请求给服务B,从而进行两服务之间的通讯。简而言之Feign是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求。
在这里插入图片描述

Nacos 整合 OpenFeign

  • 前提条件准备好nacos 环境
    1、创建一个maven父工程
	<modules>
        <module>common</module>
        <module>provider</module>
        <module>consumer</module>
    </modules>
    <packaging>pom</packaging>

    <properties>
        <jdk-version>11</jdk-version>
        <spring-boot-version>2.6.3</spring-boot-version>
        <spring-cloud-version>2021.0.1</spring-cloud-version>
        <spring-cloud-alibaba-version>2021.0.1.0</spring-cloud-alibaba-version>
        <lombok-version>1.16.10</lombok-version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba-version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring-boot-version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok-version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、创建三个子工程模块
在这里插入图片描述
2.1、创建provider 模块,自己暴露写出一个接口到时提供给consumer模块调用(和写普通接口一样)。
2.1.1、引入依赖

	<dependencies>
        <dependency>
            <groupId>com.xia</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--Spring Cloud alibaba nacoos 服务发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--监听信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2.1.2、修改配置文件

server:
  port: 9001
spring:
  application:
    name: Provider9001
  cloud:
    nacos:
      discovery:
        # nacos 服务地址
        server-addr: 192.168.98.30:8848,192.168.98.31:8848,192.168.98.32:8848
        # 所属组
        group: myDemo

2.1.3、配置启动类

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class Provider9001 {
    public static void main(String[] args) {
        SpringApplication.run(Provider9001.class,args);
        log.info("-------------服务启动成功-------------");
    }
}

3.1、创建consumer 模块
3.1.1、引入依赖

	<dependencies>
		<!--Open Feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--LoadBalancer-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

        <!--实现服务降级-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xia</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--Spring Cloud alibaba nacoos 服务发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--监听信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

3.1.2、修改配置文件

server:
  port: 9002

spring:
  application:
    name: Provider9001
  cloud:
    nacos:
      discovery:
        # nacos 服务地址
        server-addr: 192.168.98.30:8848,192.168.98.31:8848,192.168.98.32:8848
        # 所属组
        group: myDemo
feign:
  # 开启服务降级
  sentinel:
    enabled: true
  client:
    config:
      default:
        # 网络连接超时时间
        connectTimeout: 1000
        # 远程调用响应超时时间
        readTime: 2000

3.1.3、配置启动类

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class Consumer9002 {
    public static void main(String[] args) {
        SpringApplication.run(Consumer9002.class,args);
        log.info("-------------服务启动成功-------------");
    }
}

3.1.4、创建调用provider提供的接口

@FeignClient(value = "Provider9001",fallbackFactory = ApiFailBack.class)
public interface ProviderApi {

    @RequestMapping(value = "/provider/demo", method = RequestMethod.POST)
    public BusinessResult myDemo();
}

创建服务降级方法

@Component
@Slf4j
public class ApiFailBack implements FallbackFactory<ProviderApi> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiFailBack.class);

    @Override
    public ProviderApi create(Throwable cause) {
        return  () -> {
                return BusinessResult.error("服务超时");
        };
    }
}

编写调用服务的controller

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    public ProviderApi providerApi;

    @RequestMapping(value = "/demo", method = RequestMethod.POST)
    public BusinessResult myDemo() {
        return providerApi.myDemo();
    }
}

4、运行结果
4.1 、 正常情况
在这里插入图片描述

4.2、 超时情况
在这里插入图片描述

5、代码地址

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值