springcloud框架的簡單搭建(消費服務基於feign)

上一篇文章主要介紹瞭如何搭建一個簡單的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

至此我們這個服務消費的框架就搭建完畢了。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章