首页 > 其他分享 >SpringBoot2 整合 SpringCloud Feign 实例

SpringBoot2 整合 SpringCloud Feign 实例

时间:2023-12-31 13:37:05浏览次数:39  
标签:Feign SpringCloud study boot springframework eureka SpringBoot2 org import



文章目录

  • 1. 简介
  • 2. 工程实例
  • 2.1 注册中心 springcloud-study-eureka-server
  • 2.1.1 依赖 pom.xml
  • 2.1.2 配置文件 application.properties
  • 2.1.3 启动类 EurekaServerApplication.java
  • 2.2 服务提供者 springcloud-study-hello-service
  • 2.2.1 依赖 pom.xml
  • 2.2.2 配置文件 application.yml
  • 2.2.3 控制器 HelloController.java
  • 2.2.4 启动类 HelloServiceApplication.java
  • 2.3 springcloud-study-feign
  • 2.3.1 依赖 pom.xml
  • 2.3.2 配置文件 application.yml
  • 2.3.3 Fegin 接口 HelloFeign
  • 2.3.4 调用 feign 接口的控制器
  • 2.3.5 启动类 FeignApplication
  • 3. 启动并测试
  • 3.1 注册中心的情况
  • 3.2 测试访问
  • 参考文献


1. 简介

Feign 是一个声明式的伪Http客户端,使用时只需要创建一个接口并注解。
Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果。

2. 工程实例

在这篇文章: SpringBoot2 整合 SpringCloud Ribbon 实现负载均衡实例 中我们使用RestTemplate+Ribbon去消费服务,本篇文章主要讲述如何通过Feign去消费服务。

工程目录如下图所示:

SpringBoot2 整合 SpringCloud Feign 实例_xml


root 工程 spring-study,root 工程的 pom.xml 如下所示:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.syrdbt</groupId>
    <artifactId>study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>springcloud-study-eureka-server</module>
        <module>springcloud-study-feign</module>
        <module>springcloud-study-hello-service</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.1 注册中心 springcloud-study-eureka-server

工程目录如下所示:

SpringBoot2 整合 SpringCloud Feign 实例_maven_02

2.1.1 依赖 pom.xml

pom.xml 的依赖如下所示:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>study</artifactId>
        <groupId>com.syrdbt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>eurkea-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>
2.1.2 配置文件 application.properties

配置文件如下所示:

# 服务注册中心端口号
server.port=8761
# 服务注册中心主机名
eureka.instance.hostname=127.0.0.1
# 不向注册中心注册自己
eureka.client.register-with-eureka=false
# 不需要检索服务
eureka.client.fetch-registry=false
# 注册中心地址,会自动填充 主机名 和 端口号
eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.1.3 启动类 EurekaServerApplication.java
package com.syrdbt.study.eureka.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author syrdbt
 * @date 2019-10-12
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

2.2 服务提供者 springcloud-study-hello-service

工程目录如下所示:

SpringBoot2 整合 SpringCloud Feign 实例_xml_03

2.2.1 依赖 pom.xml

pom.xml 如下所示:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>study</artifactId>
        <groupId>com.syrdbt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hello-service</artifactId>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
2.2.2 配置文件 application.yml
eureka:
  client:
    serviceUrl:
      #注册中心的地址
      defaultZone: http://localhost:8761/eureka/
server:
  #当前服务端口号
  port: 8762
spring:
  application:
    #当前应用名称,当前应用的名称
    name: service-hi
2.2.3 控制器 HelloController.java
package com.syrdbt.study.hello.service.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author syrdbt
 * @date 2019-10-12
 */
@RestController
public class HelloController {
    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String index() {
        return "eurekaClient:Hello World " + this.port;
    }
}
2.2.4 启动类 HelloServiceApplication.java
package com.syrdbt.study.hello.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author syrdbt
 * @date 2019-10-12
 */
@SpringBootApplication
@EnableEurekaClient
public class HelloServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloServiceApplication.class, args);
    }
}

2.3 springcloud-study-feign

SpringBoot2 整合 SpringCloud Feign 实例_spring_04

2.3.1 依赖 pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>study</artifactId>
        <groupId>com.syrdbt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>feign</artifactId>

    <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>
</project>
2.3.2 配置文件 application.yml

application.yml:

server:
  port: 8765
spring:
  application:
    # 该服务的名字
    name: server-feign
eureka:
  client:
    serviceUrl:
      # eureka服务的地址
      defaultZone: http://localhost:8761/eureka/
2.3.3 Fegin 接口 HelloFeign

通过@ FeignClient(“服务名”),来指定调用哪个服务。下面的示例中调用了service-hi服务的“/hello”接口,HelloFeign.java 代码如下:

package com.syrdbt.study.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author syrdbt
 * @date 2019-10-12
 */
@FeignClient(name = "service-hi")
public interface HelloFeign {

    @RequestMapping(value="/hello")
    String hello();
}
2.3.4 调用 feign 接口的控制器

HelloController.java,通过上面定义的Feign客户端 HelloFeign 来消费服务。

package com.syrdbt.study.feign.controller;

import com.syrdbt.study.feign.HelloFeign;
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 syrdbt
 * @date 2019-10-12
 */
@RestController
public class HelloController {
    @Autowired
    private HelloFeign helloFeign;

    @GetMapping(value="/hello")
    public String getHi() {
        return helloFeign.hello();
    }
}
2.3.5 启动类 FeignApplication
package com.syrdbt.study.feign;

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

/**
 * @author syrdbt
 * @date 2019-10-12
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class FeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }
}

3. 启动并测试

需要启动4个实例。

⚠️注意:HelloServiceApplication 需要启动两个,两个的端口号分别为 8762 和 8763 。

IDEA 启动多实例教程:。

SpringBoot2 整合 SpringCloud Feign 实例_maven_05

3.1 注册中心的情况

访问 http://localhost:8761/ 。

注册中心注册的 实例如下所示:

SpringBoot2 整合 SpringCloud Feign 实例_xml_06

3.2 测试访问

访问:http://localhost:8765/hello 。

运行截图如下所示:

SpringBoot2 整合 SpringCloud Feign 实例_spring_07


SpringBoot2 整合 SpringCloud Feign 实例_maven_08

参考文献


标签:Feign,SpringCloud,study,boot,springframework,eureka,SpringBoot2,org,import
From: https://blog.51cto.com/xuxiangyang/9047866

相关文章

  • SpringBoot2 整合 SpringCloud 的 Hystrix断路器 实例
    文章目录1.概述2.短路器3.SpringFeign使用Hystrix断路器3.1工程实例3.2修改Fegin模块3.3测试运行参考文献1.概述微服务架构中服务之间互相调用,单个服务通常会集群部署,由于网络等原因,服务不能保证100%可用,如果单个服务出现问题会出现请求的堆积,Servlet容器的线程资源......
  • SpringBoot2 整合 Redis 实例,实现写入和读取的操作
    1.启动RedisServer启动redisserver,如下图所示,端口号6379:2.工程实例2.1工程目录工程目录如下图所示:2.2pom.xml引入依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>......
  • Spring Boot2.x 集成 OpenFeign 实现 Hystrix 熔断降级与 Ribbon 负载均衡配置
    参考https://blog.csdn.net/zhangchaoyang/article/details/123453616https://blog.csdn.net/u010277958/article/details/88744263https://blog.csdn.net/iwlnner/article/details/110090595https://cloud.tencent.com/developer/article/2225692https://blog.csdn.net/z......
  • SpringBoot2 全局捕获异常实例
    1. SpringBoot全局捕获异常首先写一个会抛出异常的 Controller 类 如下所示,i=1/0,0不能作为除数,显然这个 Controller 类 ErrorController.java会抛出异常。@EnableAutoConfiguration@RestControllerpublicclassErrorController{@RequestMapping("/errorTest......
  • SpringBoot2 读取不同环境的配置文件实例
    SpringBoot 可以在application.properties 中配置信息spring.profiles.active来读取不同环境的配置文件。1.  SpringBoot2读取不同环境的配置文件工程运行环境可能有:开发环境、测试环境和生产环境,可以通过修改application.properties 来获取不同环境的的配置信息。首先我......
  • IDEA 中 SpringBoot2 整合 Mybatis 实例实例
    记录在IDEA中 使用SpringBoot2整合Mybatis的实例,环境:Java8+Maven+MySQL8。1. 添加依赖 添加MyBatis依赖,MySQL连接依赖,,数据库用的MySQL8。<!--MyBatis依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-s......
  • RocketMQ系统性学习-SpringCloud Alibaba集成RocketMQ以及消费收发实战
    欢迎关注公众号:【11来了】发送“资料”可以下载Redis、JVM系列文章PDF版本!作者为在读研究生,目前研二,计划在公众号记录学习常用中间件笔记,以及明年更新面试经历!SpringCloudAlibaba集成RocketMQ最佳实践SpringBoot相对于SSM来说已经很大程度上简化了开发,但是使用SpringBo......
  • springcloud动力节点-01Eureka
    SpringCloudEureka1.SpringCloudEureka简介注册发现中心Eureka来源于古希腊词汇,意为“发现了”。在软件领域,Eureka是Netflix在线影片公司开源的一个服务注册与发现的组件,和其他Netflix公司的服务组件(例如负载均衡、熔断器、网关等)一起,被SpringCloud社区整合......
  • springcloud动力节点-05Sleuth
    SpringCloudSleuth1.什么是链路追踪官网:https://spring.io/projects/spring-cloud-sleuth链路追踪就是:追踪微服务的调用路径2.链路追踪的由来在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的服务节点调用来协同产生最后的请求结果,每一个请求都会开成一......
  • springcloud动力节点-04Hystrix
    SpringCloudHystrix1.前言1.1什么是服务雪崩   服务雪崩的本质:线程没有及时回收。不管是调用成功还是失败,只要线程可以及时回收,就可以解决服务雪崩1.2服务雪崩怎么解决1.2.1修改调用的超时时长(不推荐)将服务间的调用超时时长改小,这样就可以让线程及时回收,保证服......