1. 程式人生 > >SpringCloud零基礎上手(四)——服務發現以及Feign(宣告式RESTful服務呼叫)

SpringCloud零基礎上手(四)——服務發現以及Feign(宣告式RESTful服務呼叫)

接著上篇的內容,我們再建立一個專案充當服務消費者,命名為pf-font,如果pf-login視為後端,那麼pf-font 就是前端,達到前後端分離的目的。

一、pf-front目錄結構
這裡寫圖片描述

pf-front同樣是一個多模組專案,儘管它目前只有一個模組——pf-web,方便後面擴充套件其他模組。

二、結構組成

1、pf-font的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> <groupId>cn.pw</groupId> <artifactId>pf-front</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging
>
<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>Dalston.SR4</spring-cloud.version
>
<fastjson-version>1.2.15</fastjson-version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson-version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</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> <modules> <module>pf-web</module> </modules> </project>

2、pf-web的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>cn.pw</groupId>
        <artifactId>pf-front</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>pf-web</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>  

三、pf-web

1、啟動類

package cn.pw.platform.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class WebApplication {

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

}

這裡我使用@EnableDiscoveryClient而沒有使用@EnableEurekaClient,因為後者只能為Eureka作用,而前者則依賴於classpath中的實現,也就是說@EnableDiscoveryClient有多種實現,如eureka, consul, zookeeper,我們需要在application.yml中指定具體的實現。

@EnableFeignClients開啟了對Feign的支援 。

當我們在classpath中配置了eureka,@EnableDiscoveryClient@EnableEurekaClient作用是一樣的。

2、配置檔案application.yml

server:
  port: 8680

spring:
  thymeleaf:
    mode: HTML5
    encoding: utf-8
    content-type: text/html
    cache: false
  application:
    name: pf-web

eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
    instance:
      instance-id: ${spring.cloud.client.ipAddress}:{server.port}
      prefer-ip-address: true

這個配置不難看懂,Eureka的配置項中,我們將pf-web自身也註冊到了註冊中心。
現在啟動Eureka Server 和 pf-web ,我們在註冊中心的頁面可以得到驗證 。

四、使用Feign 呼叫 pf-login 的介面

1、在pf-web中定義Feign的呼叫介面

package cn.pw.platform.web.feign;

import cn.pw.platform.web.vo.UserVo;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name="pf-login-server")
public interface LoginClient {
    @GetMapping("/selectUserByName/{userName}")
    UserVo selectUserByName(@PathVariable("userName") String userName);

    @PostMapping("/user/add")
    UserVo addUser(@RequestBody UserVo userVo);
}

@FeignClient(name="pf-login-server")指明瞭要呼叫的服務名,介面中定義的方法要符合被調服務的介面要求。

2、寫一個測試類Controller

package cn.pw.platform.web.controller;

import cn.pw.platform.web.feign.LoginClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

@Controller
public class LoginController {
    @Autowired
    private LoginClient loginClient;

    @GetMapping("/searchByName/{name}")
    @ResponseBody
    public UserVo searchByName(@PathVariable("name") String name){
        return loginClient.selectUserByName(name);
    }
}

3、補充 一個引數對映類UserVo用於封裝從服務端請求來的引數。

public class UserVo {
    private Long id;
    private String name;
    private String nickName;
    private String password;
    private Integer age;
    private Integer sex;
    //...省略getter setter方法
}

4、測試

逐個啟動所需專案,Eureka Server、pf-login、pf-web。

結果:

{
“id”: 10,
“name”: “libin”,
“nickName”: null,
“password”: “123456”,
“age”: 18,
“sex”: null
}

和上一篇我們直接呼叫server,所獲取的結果完全一致。