SpringCloud学习(二):Feign 接口调用服务

菜鸟学渣接触spring cloud 系列...

公司也上微服务了,再不学习下就凉了,所以来踩坑吧...

版本:

  spring-boot:  2.0

  spring-cloud: Finchley.SR1

已有项目:

  [eureka-server]              # 注册中心

  [eureka-client-one]       #  微服务1

能上图绝不BB

  

微服务之间的接口相互调用,使用的是Feign,比HTTPClient好用多了...

一、Feign端   [eureka-client-two]

  这里新建一个微服务[eureka-client-two]调用[eureka-client-one]中的helloworld接口

  主要引入依赖  spring-cloud-starter-openfeign

<?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.renzku</groupId>
    <artifactId>eureka-client-two</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client-two</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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.SR1</spring-cloud.version>
    </properties>

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

        <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>
        </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>
pom.xml

  配置文件  application.yml

server:
  port: 8502

spring:
  application:
    name: eureka-client-two

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

  启动类  EurekaClientTwoApplication.java

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient  // 开启eureka客户端
@EnableFeignClients(basePackages = {"com.renzku.eurekaClientTwo.feign"})  // 开启feign
public class EurekaClientTwoApplication {

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

  Feign类  EurekaClientOneFeign.java

/**
 *  value为eureka-client-one注册时的 spring.application.name
 */
@FeignClient(value = "eureka-client-one")
public interface EurekaClientOneFeign {
    /**
     * eureka-client-one的helloworld访问mapping
     */
    @RequestMapping("/")
    String home();
}

  Rest服务  FeignTest.java   :逻辑是调用rest接口,接口中通过feign调用eureka-client-one的接口,返回接受数据

@RestController
public class FeignTest {

    @Autowired
    EurekaClientOneFeign eurekaClientOneFeign;  // 如果提示“No Beans Named***" 其实没问题,恶...

    @RequestMapping("/test/home")
    public String testHome(){
        // 调用feign,feign调用 eureka-client-one的api
        return eurekaClientOneFeign.home();
    }

    @RequestMapping("/")
    public String hello(){
        return "hello world -- client two";
    }
}

  目录结构

  

  依次启动项目 [eureka-server]、[eureka-client-one]、[eureka-client-two]

  访问注册中心,2个服务都添加到了实例列表

  

 

二、结果

  访问 http://192.168.1.100:8502/test/home ,可以看到返回的数据和访问http://localhost:8501/ 是一样的

  

  当feign中RequestMapping下的接口带了参数,会发现用起来确实比手搓HttpClient舒服...

 

posted on 2018-09-09 22:16  江湖吹风  阅读(21433)  评论(1编辑  收藏  举报

renzku