上一篇文章主要介绍了如何搭建一个简单的springcloud框架。不过,我们搭建好框架就是为了消费它使用它,那么这篇文章就来看看如何去消费使用我们之前搭建起来的服务吧!

首先本文是基于上一篇文章进行的。

一、Feign简介

Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API。

二、启动程序

继续用上一节的工程, 启动eureka-server,端口为8080; 启动eureka-client两次,端口分别为8081 、8082.

三、创建一个feign的服务

我们首先要新建一个spring-boot工程,取名为serice-feign。

clipboard.png

这次我们要选择三个依赖:

clipboard.png

对应的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhouxiaoxi</groupId>
    <artifactId>eureka-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-feign</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</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>
        </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>

接下来我们就需要配置application.yml文件了:

server:
  #端口号
  port: 8083
spring:
  application:
    #端口名称
    name: eureka-feign
eureka:
  client:
    serviceUrl:
      #服务注册地址
      defaultZone: http://localhost:8080/eureka/

在程序的启动类EurekaFeignApplication,加上@EnableFeignClients注解开启Feign的功能:

package com.zhouxiaoxi.eurekafeign;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class EurekaFeignApplication {

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

}

现在我们需要定义一个feign接口,通过@FeignClient(“服务名”)注解来指定调用哪个服务。
比如在下面的代码中调用了eureka-client服务的“/hello”接口,代码如下:

package com.zhouxiaoxi.eurekafeign.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(value = "eureka-client")
public interface SchedualServiceHello {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);

}

在Web层的controller层,对外暴露一个”/hello”的API接口,通过上面定义的Feign客户端SchedualServiceHello 来消费服务。代码如下:

package com.zhouxiaoxi.eurekafeign.controller;

import com.zhouxiaoxi.eurekafeign.service.SchedualServiceHello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    SchedualServiceHello schedualServiceHello;

    @GetMapping(value = "/hello")
    public String sayHi(@RequestParam String name) {
        return schedualServiceHello.sayHiFromClientOne( name );
    }

}

启动程序,多次访问http://localhost:8083/hello?name=feign,浏览器显示内容为:

hello feign ,i am from port:8081
hello feign ,i am from port:8082

至此我们这个服务消费的框架就搭建完毕了。。。


周兆东
107 声望21 粉丝

一个java小白的成长之路。。。