ITPub博客

首页 > 架构设计 > 软件结构 > 四、SpringCloud Alibaba + Spring Boot数据中台+多租户saas+前后端分离之OpenFe

四、SpringCloud Alibaba + Spring Boot数据中台+多租户saas+前后端分离之OpenFe

原创 软件结构 作者:JIAN2 时间:2021-12-03 16:26:52 0 删除 编辑

  1.前言
Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。

Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。使用@EnableFeignClients即可开启Feign。

架构技术选型

  1. 核心框架 Spring Boot
  2. SOA Spring Cloud
  3. 安全框架 Spring Security Oauth2
  4. 注册中心/配置中心 Nacos 集群部署

  5. 网关 Spring Cloud Gateway 部署多套,使用nginx负载
  6. 链路监控 Skywalking
  7. 流量控制、熔断降级 Sentinel
  8. 数据库 Mysql
  9. 分布式事务 Seata
  10. 定时调度 xxl-job
  11. 缓存 Redis
  12. MQ RocketMq
  13. 持久框架 Mybatis Plus
  14. 搜索引擎 ElasticSearch
  15. 需要架构源码的朋友可以看我个人简介联系我

简而言之:

Feign采用的是基于接口的注解
Feign默认整合了ribbon,具有负载均衡的能力
Feign默认整合了Hystrix,具有熔断的能力


OpenFeign属性配置列表文档:

OpenFeign官方教程文档:

2.初探Feign
微服务间的通讯是基于HTTP的,而Feign就是一个伪HTTP请求客户端,通过声明式的Feign客户端可以调用远程微服务,在实现远程微服务调用的过程中,多个实例存在的时候,Ribbon负载均衡器会默认的进行本地负载均衡,默认通过轮询机制进行微服务的调用,Feign默认则是集成了并且支持Ribbon负载均衡。

下面以订单服务(Order)调用商品服务(Product)为例,搭建环境使用Feign远程调用消费服务。

3.商品(Product)工程
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
         xmlns:xsi="
         xsi:schemaLocation="
    <parent>
        <artifactId>springcloud-alibaba-nacos</artifactId>
        <groupId>com.bruce.springcloud-alibaba-nacos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>springcloud-alibaba-provider</artifactId>
    <dependencies>
        <!-- web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除tomcat依赖 -->
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--undertow容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <!--nacos服务注册与发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- lombok插件 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>


application.yml

server:
  port: 8715
spring:
  cloud:
    nacos:
      discovery:
        #服务注册与发现地址
        server-addr: 127.0.0.1:8848
        #开启服务注册与发现功能
        enabled: true
  application:
    name: nacos-product

商品服务接口ProviderController

@Slf4j
@RestController
public class ProductController {
    @Value("${server.port}")
    private String port;
    @GetMapping(value = "/getProductInfo/{productId}")
    public String getProductInfo(@PathVariable("productId") String productId) {
        log.info("请求进来啦");
        return "Hello Nacos Discovery " + productId;
    }
}


启动类ProductProviderApp

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @BelongsProject: springcloud-alibaba-nacos
 * @BelongsPackage: com.bruce
 * @CreateTime: 2021-02-18 13:08
 * @Description: TODO
 */
@SpringBootApplication
public class AppProvider {
    public static void main(String[] args) {
        SpringApplication.run(AppProvider.class);
    }
}


4.订单(Order)工程
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
         xmlns:xsi="
         xsi:schemaLocation="
    <parent>
        <artifactId>springcloud-alibaba-nacos</artifactId>
        <groupId>com.bruce.springcloud-alibaba-nacos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>springcloud-alibaba-consumer</artifactId>
    <dependencies>
        <!-- Alibaba-nacos服务发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>nacos-client</artifactId>
                    <groupId>com.alibaba.nacos</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- nacos-client -->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.2.0</version>
        </dependency>
        <!-- hystrix断路器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!-- openfeign客户端 ,默认集成并开启了ribbon负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除tomcat依赖 -->
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--undertow容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <!-- lombok插件 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


application.yml

server:
  port: 8710
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        enabled: true
  application:
    name: nacos-order


定义Feign客户端OrderFeign

OrderFeign 中引入了Hystrix熔断器组件起到服务的保护降级作用,@FeignClient注解当中使用断路器支持的有两种方式fallback 和fallbackFactory

关于Feign Hystrix Fallbacks:

第一种:Hystrix支持回退的概念:当它们的电路断开或出现错误时,将执行默认代码路径。要为给定@FeignClient集启用后备,该fallback属性应为实现后备的类名称。您还需要将实现声明为Spring bean。

import com.thinkingcao.order.feign.callback.OrderFeignFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "nacos-product",fallback = HystrixClientFallback.class)
public interface OrderFeign {
    @GetMapping(value = "/getProductInfo/{productId}")
    public String getProductInfo(@PathVariable("productId") String productId);
    
    @Component
    @Slf4j
    static class HystrixClientFallback implements OrderFeign {
        @Override
        public String getProductInfo(String productId) {
            return "fallback; reason was:  服务忙,稍后重试" ;
        }
    }
}


第二种: 如果需要访问引起后备触发器的原因,则可以使用fallbackFactory inside的属性@FeignClient。说白了就是如果你想知道服务调用为什么失败了,可以使用下面这种断路器方式。

import com.thinkingcao.order.feign.callback.OrderFeignFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "nacos-product",fallbackFactory = OrderFeignFallbackFactory.class)
public interface OrderFeign {
    @GetMapping(value = "/getProductInfo/{productId}")
    public String getProductInfo(@PathVariable("productId") String productId);
    @Component
    @Slf4j
    static class OrderFeignFallbackFactory implements FallbackFactory<OrderFeign> {
      @Override
      public OrderFeign create(Throwable throwable) {
         log.error("调用异常:"+ throwable.toString());
         return new OrderFeign() {
            @Override
            public String getProductInfo(String string) {
                return "开启断路-fallback; reason was: "+ throwable;
            }
        };
      }
}


服务消费OrderController

package com.bruce.controller;

import com.bruce.client.OrderFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
 * @BelongsProject: springcloud-alibaba-nacos
 * @BelongsPackage: com.bruce.controller
 * @CreateTime: 2021-02-18 15:19
 * @Description: TODO
 */
@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderFeign orderFeign;
    @GetMapping("/getProduct/{productId}")
    public String getProduct(@PathVariable("productId") String id){
        String response = orderFeign.getProductInfo(id);
        log.info("调用服务结束: "+ response);
        return response;
    }
}


启动类OrderConsumerApp

1.@SpringCloudApplication: 组合注解:

@SpringBootApplication(启动SPringBoot程序)、
@EnableDiscoveryClient(开启服务注册与发现)、
@EnableCircuitBreaker(开启断路器)


2.@EnableFeignClients: 开启Feign客户端

package com.bruce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
 * @BelongsProject: springcloud-alibaba-nacos
 * @BelongsPackage: com.bruce
 * @CreateTime: 2021-02-18 13:16
 * @Description: TODO
 */
//@SpringBootApplication
@SpringCloudApplication
@EnableFeignClients
public class AppConsumer {
    public static void main(String[] args) {
        SpringApplication.run(AppConsumer.class);
    }
}


5.测试
地址:


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/70006413/viewspace-2845689/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
华为资深架构师,曾参与华为数据中心迁移解决方案!有需要了解分布式微服务电商源码的朋友可以+我 v ❤: 445909108 , 10余年互联网研发行业经验!博客里面是个人的一点技术总结,希望能够帮助到更多的IT朋友成长!

注册时间:2021-09-06

  • 博文量
    682
  • 访问量
    212501