SpringCloud零基础上手(四)——服务发现以及Feign(声明式RESTful服务调用)

接着上篇的内容,我们再创建一个项目充当服务消费者,命名为pf-font,如果pf-login视为后端,那么pf-font 就是前端,达到前后端分离的目的。

一、pf-front目录结构
SpringCloud零基础上手(四)——服务发现以及Feign(声明式RESTful服务调用)_第1张图片

pf-front同样是一个多模块项目,尽管它目前只有一个模块——pf-web,方便后面扩展其他模块。

二、结构组成

1、pf-font的pom.xml


<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.0modelVersion>

    <groupId>cn.pwgroupId>
    <artifactId>pf-frontartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>pompackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Dalston.SR4spring-cloud.version>
        <fastjson-version>1.2.15fastjson-version>
    properties>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.8.RELEASEversion>
        <relativePath/> 
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>${fastjson-version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <modules>
        <module>pf-webmodule>
    modules>
project>

2、pf-web的pom.xml


<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.0modelVersion>
    <parent>
        <groupId>cn.pwgroupId>
        <artifactId>pf-frontartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>pf-webartifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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。

使用postman ,请求 http://localhost:8680/searchByName/libin

结果:

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

和上一篇我们直接调用server,所获取的结果完全一致。

你可能感兴趣的:(spring,cloud微服务上手篇)