欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第四章:spring cloud分布式开发之openfeign

程序员文章站 2022-04-07 12:34:18
...

Spring Cloud中的openfeign整合了ribbon的负载均衡和熔断机制,还实现了微服务之间的相互调用

这章我们主要讲解openfeign实现一个feign客户端调用主客户端的接口

首先我们在原有项目基础上新建一个子项目moudule

起名为sc-feign

第四章:spring cloud分布式开发之openfeignpom文件内容为:

<?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.softA</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.systop</groupId>
    <artifactId>sc-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sc-feign</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <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>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

 

在application.yml文件中配置feign客户端的应用名称及端口

 

spring:
  application:
    name: sc-feign
server:
  port: 9001
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

在启动类ScFeignApplcation上添加注解@EnableDiscoveryClient,@EnableFeignClients

package com;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ScFeignApplication {

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

}

在scfeign路径下新建两个包controller,service

第四章:spring cloud分布式开发之openfeign

 

在service包内新建一个接口类

package com.systop.scfeign.service;

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

/**
 *
 * @FeignClient
 * 注解定义要调用那个微服务的名称(spring.application.name:)
 */
@FeignClient(name="sc-client")
public interface FeignClientService {
    /**
     *
     * 定义要调用服务的哪个接口
     * 使用@RequestMapping()
     *
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addStu(@RequestParam("name") String name, @RequestParam("age") int age, @RequestParam("height") double height, @RequestParam("love") String love);
}

 

在controller包内新建一个类

package com.systop.scfeign.controller;

import com.systop.scfeign.service.FeignClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class FeignController {
    @Autowired
    private FeignClientService feignClientService;

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addStu(@RequestParam("name") String name, @RequestParam("age") int age, @RequestParam("height") double height, @RequestParam("love") String love){
        return feignClientService.addStu(name, age, height, love);
    }
}

欧克!

依次启动注册中心,主客户端,feign客户端

访问

http://localhost:9001/add?name=xiaoma&&age=23&&height=2&&love=打篮球

看到结果:第四章:spring cloud分布式开发之openfeign

 

说明我们的feign客户端成功调用了主客户端的 add接口

查看数据库,数据添加成功

第四章:spring cloud分布式开发之openfeign