SpringCloud分布式微服务b2b2c电子商务(四)学习注解式HTTP请求Feign -b2b2c小程序电子商务

Feign
注解式的 Feign 使得 Java HTTP 客户端编写更方便。Feign 灵感来源于安卓网络编程框架 Retrofit、JAXRS-2.0 和 WebSocket,支持可插拔编码器和解码器,降低 HTTP API 的复杂度,通过最少的资源和代码来实现和 HTTP API 的连接。通过可定制的解码器和错误处理,可以编写任意的HTTP API。Spring Cloud Feign 封装了 Ribbon 这一组件,所以在使用 Feign 同时还能提供负载均衡的功能,这一切只需要一个 @FeignClient 即可完成。

Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:三五三六二四七二五九

早期版本的 Feign 被 Spring Cloud 团队集成在 spring-cloud-netflix 子项目下,但如今 Spring Cloud 团队将 Spring Cloud Feign 独立成一个单独的 spring-cloud-openfeign 项目

Try
准备三个工程,分别是 eureka-server、order-server、product-server
依赖

对比上一章,此处多了一个 spring-cloud-starter-openfeign 的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>

配置文件

在 src/main/resources 目录下创建一个 bootstrap.yml 的文件,写上 eureka 相关配置信息

server:
  port: 7072
spring:
  application:
    name: order-server
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
  client:
    service-url:
      defaultZone: http://localhost:7071/eureka/

roductClient 接口

创建一个 ProductClient ,是不是感觉和 XxxxService 看起来类似(用法都类似),都是接口文件只不过在这个文件的上方多了一个 @FeignClient 注解,多种写法,总有一款适合你

name:指定 FeignClient 的名称,该属性会作为微服务的名称,用于服务发现
value:同 name 字段互通
serviceId:指定服务ID,每个注册到注册中心上的客户端都会有对应的 serviceId 一般是 spring.application.name,与 name 和 value 互通
url: 一般用于调试,可以指定一个详细地址(http://localhost:8080/products)
path: 请求统一路径,可以看成 @RequestMapping("/products")
decode404:404 错误时,调用 decoder 进行解码,否则抛出 FeignException
fallback:发生错误时,回调 hystrix 类/方法(后面会详细介绍)

package com.battcn.api;

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

/**
 * @author Levin
 * @since 2018/9/26 0026
 */
@FeignClient(name = "product-server/products", decode404 = true)
//@FeignClient(name = "products", url = "http://localhost:7073/products")
//@FeignClient(value = "product", serviceId = "product-server", path = "/products", decode404 = true)
public interface ProductClient {

    /**
     * 根据产品ID查询产品信息
     *
     * @param productId ID
     * @return 查询结果
     */
    @GetMapping("/{product_id}")
    String selectProductById(@PathVariable("product_id") Long productId);
}

OrderController

直接使用 @Autowired 注入进去即可,然后调用就好了,对比较 Ribbon 这里我们看不到 RestTemplate 的代码了,也无需自己做解码映射,Spring Cloud Feign 默认都替我们实现好了,我们只需要遵循既定的标准即可

package com.battcn.controller;

import com.battcn.api.ProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Levin
 * @since 2018/9/26 0026
 */
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private ProductClient productClient;


    @GetMapping
    public String query() {
        return this.productClient.selectProductById(10L);
    }
}

主函数

通过 @EnableFeignClients 注解开启对 Feign 的支持,用习惯 Dubbo 的朋友喜欢将 API 打包成独立的 JAR ,这个时候需要指定 basePackage 属性。

package com.battcn;

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

/**
 * @author Levin
 */
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {

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

}
1- 安装lombok插件 ## mysql - 下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads - 下载后按提示进行安装 - 导入document/sql下的mall.sql文件 ## redis - 下载地址:https://github.com/MicrosoftArchive/redis/releases - 下载后按提示进行安装 ## OSS - 该项目文件上传采用OSS,需要自行注册OSS账号并配置 - 首先将mall-admin\src\main\resources\application.properties文件中以aliyun.oss.开头的配置改为你自己的配置 - OSS上传文件需要配置跨域资源共享(CORS)规则,参考文档:https://help.aliyun.com/document_detail/31928.html - 上传方采用服务端签名后直传的形,参考文档:https://help.aliyun.com/document_detail/31926.html ## mall-admin - 启动项目:直接运行com.macro.mall.MallAdminApplication的main方法即可 - 接口文档地址:http://localhost:8080/swagger-ui.html ## mall-search - 启动项目:直接运行com.macro.mall.search.MallSearchApplication的main方法即可 - 接口文档地址:http://localhost:8081/swagger-ui.html - 使用前需要先调用接口导入数据;http://localhost:8081/esProduct/importAll - 如出现无法启动的问题,可以先删除elasticsearch里面的数据再启动 ## mall-portal - 启动mall-portal项目:直接运行com.macro.mall.portal.MallPortalApplication的main方法即可 - 接口文档地址:http://localhost:8085/swagger-ui.html 1. 本地安装开发环境中的所有工具并启动 2. 克隆源代码到本地,使用IDEA或Eclipse打开,并完成编译; 3. 安装 redis mysql 4. 在mysql中新建mall数据库,导入document/sql下的mall.sql文件; 5. 启动mall-admin项目:直接运行com.macro.mall.MallAdminApplication的main方法即可, 接口文档地址:http://localhost:8080/swagger-ui.html;  6. 启动mall-portal项目:直接运行com.macro.mall.portal.MallPortalApplication的main方法即可, 接口文档地址:http://localhost:8085/swagger-ui.html;  7. 克隆mall-admin-web项目,并导入到IDEA中并完成编译传送门,需要安装node环境,然后到项目下; 8. 运行命令:npm install 然后执行 npm run dev,访问地址:http://localhost:8090 即可打开后台管理系统页面; 9. 克隆Mall-Vue-master项目,并导入到IDEA中并完成编译传送门; 10. 运行命令:npm run dev,访问地址: 即可打开pc商城页面; 11. 克隆vue-jd-master项目,并导入到IDEA中并完成编译传送门; 12. 运行命令:npm run dev,访问地址: 即可打开h5商城页面; 13. 下载小程序 用微信开发工具打卡就可以访问   https://gitee.com/zscat-platform/mall   功能预览   http://www.yjlive.cn:8090/#/home   https://gitee.com/zscat-platform/mall/wikis/pages 645
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值