zoukankan      html  css  js  c++  java
  • Spring Cloud进阶之路 | 四:服务消费者(feign)

    转载请注明作者及出处:

    作者:银河架构师

    原文链接:https://www.cnblogs.com/luas/p/12111916.html

    ​feign简介

    github说明

    Feign is a Java to Http client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to Http APIs regardless of ReSTfulness.

    Feign是一种声明式、模板化的HTTP客户端。能让编写服务客户端更加简单,同时也支持JAX-RS标准的注解和WebSocket,也支持可拔插式的编码器和解码器。

    而Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverter。Feign集成了ribbon,故而可以借助注册中心,轮询实现客户端负载均衡。定义客户端只需使用@FeignClient注解声明式绑定接口,进而优雅而简单的实现IOC注入并调用。

    创建服务提供者

    使用前一篇文章的商品工程xmall-product作为服务提供者,启动双实例,供负载均衡使用。如何启动双实例,请参见另外一篇文章:Spring Boot工程如何在IDEA中启动多实例

    先启动8080端口的xmall-product工程,修改端口号为8081,并启动第二个实例。查看nacos,已有2个实例注册:

    创建服务消费者

    创建服务消费者工程xmall-product-clients-feign,作为服务消费者,调用商品服务。

    pom

    其父工程正是上一篇文章中创建的父工程java-boot-parent-2.1

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.luas.cloud</groupId>
            <artifactId>java-boot-parent-2.1</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../../java-boot-parent-2.1</relativePath>
        </parent>
        <groupId>com.luas.xmall</groupId>
        <artifactId>xmall-product-clients-feign</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>xmall-product-clients-feign</name>
        <description>product service clients by ribbon</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- nacos cloud -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

    yml

    依然注册到nacos,端口为8083。

    server:
      port: 8083
    spring:
      application:
        name: xmall-product-clients-feign
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848

    创建客户端

    创建SkuService,service id为商品服务服务名xmall-product。关于FeignClient注解中的关键参数、常见注意事项、常见问题等后续会出专门文章来说明,本文不再赘述。

    package com.luas.xmall.product.clients.clients;
    ​
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    ​
    @FeignClient(name = "xmall-product")
    public interface SkuService {
    ​
        @RequestMapping(value = "/sku/{skuId}", method = RequestMethod.GET)
        Object info(@PathVariable("skuId") String skuId);
    }
     

    创建测试入口

    创建SkuController,注入上一步创建的SkuService客户端,来测试是否生效。

    package com.luas.xmall.product.clients.controller;
    ​
    import com.luas.xmall.product.clients.clients.SkuService;
    ​
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    ​
    @RestController
    @RequestMapping("/sku")
    public class SkuController {
    ​
        @Autowired
        private SkuService skuService;
        
        @RequestMapping("/{skuId}")
        public Object info(@PathVariable String skuId) {
            return this.skuService.info(skuId);
        }
    }
     

    浏览器访问http://localhost:8082/sku/1122,多次刷新页面,可发现负载均衡生效,商品信息来自于不同端口的实例。


    源码

    github

    https://github.com/liuminglei/SpringCloudLearning/tree/master/04/

    gitee

    https://gitee.com/xbd521/SpringCloudLearning/tree/master/04/

    正文完!

    微信搜索【银河架构师】,发现更多精彩内容。

    技术资料领取方法:关注公众号,回复微服务,领取微服务相关电子书;回复MK精讲,领取MK精讲系列电子书;回复JAVA 进阶,领取JAVA进阶知识相关电子书;回复JAVA面试,领取JAVA面试相关电子书,回复JAVA WEB领取JAVA WEB相关电子书。

  • 相关阅读:
    13.ng-value
    Android 下使用 JSON 实现 HTTP 请求,外加几个示例!
    PHP完整的AES加解密算法使用及例子(256位)
    常用对称加密算法(DES/AES)类(PHP)
    随机字符串生成算法
    JAVA实现AES加密
    Base64的好处
    什么是VC、PE、LP、GP?
    mysql update操作
    iOS开发:用DES对字符串加解密
  • 原文地址:https://www.cnblogs.com/luas/p/12111916.html
Copyright © 2011-2022 走看看