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

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

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

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

起名为sc-feign

pom文件内容为:

<?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

 

在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=打篮球

看到结果:

 

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前后端杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值