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/

正文完!

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

?

原文地址:https://www.cnblogs.com/luas/p/12111916.html

时间: 2024-08-01 00:19:36

Spring Cloud进阶之路 | 四:服务消费者(feign)的相关文章

从 Spring Cloud 开始,聊聊微服务架构实践之路

[编者的话]随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. 系统架构的演进过程 单一应用架构(第一代架构) 这是平台最开始的情况,当时流量小,为了节约成本,并将所有应用都打包放到一个应用里面,采用的架构为 .NET SQL Server: 表示层:位于最外层(最上层),最接近用户.用于显示数据和接收用户输入的数 据,为用户提供一种交互式操作的界面,平台所使用

spring cloud 入门系列八:使用spring cloud sleuth整合zipkin进行服务链路追踪

好久没有写博客了,主要是最近有些忙,今天忙里偷闲来一篇. =======我是华丽的分割线========== 微服务架构是一种分布式架构,微服务系统按照业务划分服务单元,一个微服务往往会有很多个服务单元,一个请求往往会有很多个单元参与,一旦请求出现异常,想要去定位问题点真心不容易,因此需要有个东西去跟踪请求链路,记录一个请求都调用了哪些服务单元,调用顺序是怎么样的以及在各个服务单元处理的时间长短.常见的服务链路追踪组件有google的dapper.twitter的zipkin.阿里的鹰眼等,它们

【译文】用Spring Cloud和Docker搭建微服务平台

by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-microservices.html This blog series will introduce you to some of the foundational concepts of building a microservice-based platform using Spring Cloud

使用Spring Cloud和Docker构建微服务

什么是Spring Cloud?Spring Cloud 是Pivotal提 供的用于简化分布式系统构建的工具集.Spring Cloud引入了云平台连接器(Cloud Connector)和服务连接器(Service Connector)的概念.云平台连接器是一个接口,需要由云平台提供者进行实现,以便库中的其他模块可以与该云平台协同工作.(更多介绍,可以阅读 InfoQ的这篇文章:http://www.infoq.com/cn/news/2014/06/spring-cloud-platfor

Spring Cloud(十一):服务网关 Zuul(过滤器)【Finchley 版】

Spring Cloud(十一):服务网关 Zuul(过滤器)[Finchley 版] 发表于 2018-04-23 |  更新于 2018-05-07 | 在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router).本文我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter). Filter 的作用 我们已经能够实现请求的路由功能,所以我们的微服务应用提供的接口就可以通过统一的 API 网关入口被客户端访问到了.但

spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 eureka-server eureka-client 1 Ribbon工程搭建 1.1 创建spring boot工程:eureka-ribbon 1.2 pom.xml所需要依赖的jar包 <dependency> <groupId>org.springframework.clo

Spring Cloud官方文档中文版-服务发现:Eureka服务端

官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server 文中例子我做了一些测试在:http://git.oschina.net/dreamingodd/spring-cloud-preparation Service Discovery: Eureka Server 服务发现:Eureka服务端 How to Include Eureka Server 如何创建Eurek

使用 Spring Cloud 和 Docker 构建微服务架构

如何使用Spring Boot.Spring Cloud.Docker和Netflix的一些开源工具来构建一个微服务架构. 本文通过使用Spring Boot.Spring Cloud和Docker构建的概念型应用示例,提供了了解常见的微服务架构模式的起点. 该代码可以在Github上获得,并且在Docker Hub上提供了镜像.您只需要一个命令即可启动整个系统. 我选择了一个老项目作为这个系统的基础,它的后端以前是单一应用.此应用提供了处理个人财务.整理收入开销.管理储蓄.分析统计和创建简单预

【spring cloud】导入一个新的spring boot项目作为spring cloud的一个子模块微服务,怎么做/或者 每次导入一个新的spring boot项目,IDEA不识别子module,启动类无法启动/右下角没有蓝色图标

如题:导入一个新的spring boot项目作为spring cloud的一个子模块微服务,怎么做 或者说每次导入一个新的spring boot项目,IDEA不识别,启动类无法启动,怎么解决 下面一起来走一遍这个流程: 1.将一个spring boot服务导入spring cloud中作为一个子模块 如图:这里有一个现成的spring cloud微服务集群,[如何创建一个spring cloud微服务:https://www.cnblogs.com/sxdcgaq8080/p/9035724.h