微服务笔记之Spring Cloud 中使用Feign调用接口服务(Finchley)

本文介绍如何使用Spring Boot和Spring Cloud Finchley版本通过Feign实现微服务之间的调用,包括项目搭建、依赖配置、代码示例及应用配置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此篇和上篇只是spring-boot和spring-cloud的版本不同,所带来的配置及代码变化,项目配置及代码参考如下。

创建maven项目servicefeign_f;

项目目录结构:

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springcloud.lcl.fetgn</groupId>
    <artifactId>servicefeign_f</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>servicefeign_f</name>
    <url>http://www.myorganization.org</url>

    <build>
        <finalName>servicefeign_f</finalName>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

</project>

 2. 代码示例  

 项目主启动类:

package com.springcloud.lcl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author lcl
 * @version 0.1
 * @date 2018/11/8
 **/
@SpringBootApplication
@EnableFeignClients
public class ServiceFeignFinchleyApplication {

    public static void main(String[] args){

        SpringApplication.run(ServiceFeignFinchleyApplication.class,args);
    }

}
服务接口 IFeignService代码:
package com.springcloud.lcl.feign.service;

import com.springcloud.lcl.feign.service.impl.FeignServiceImpl;
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(value = "service-hi",fallback = FeignServiceImpl.class)
public interface IFeignService {

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String getHiService(@RequestParam(value = "name") String name);
}

服务接口实现类FeignServiceImpl:

package com.springcloud.lcl.feign.service.impl;

import com.springcloud.lcl.feign.service.IFeignService;
import org.springframework.stereotype.Component;

/**
 * @author lcl
 * @version 0.1
 * @date 2018/11/8
 **/
@Component
public class FeignServiceImpl implements IFeignService {
    @Override
    public String getHiService(String name) {
        return "Hii "+name+": Sorry,Error...";
    }
}

api接口定义类:FeignController

package com.springcloud.lcl.feign;

import com.springcloud.lcl.feign.service.IFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lcl
 * @version 0.1
 * @date 2018/11/8
 **/
@RestController
public class FeignController {

    @Autowired
    private IFeignService feignService;

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String hiService(@RequestParam String name){
        return feignService.getHiService(name);
    }

}

3.  application.properties 配置: 

spring.application.name=service-feign-fin

server.port=8790

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

4. 启动及验证同上篇。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值