1.搭建Eureka Server和Eureka provide

参考链接 https://blog.nowcoder.net/n/fa86573da12f492fa6925d29b4c76e48

2.创建服务提供者

在上述创建的服务提供者Eureka provide中创建controller包,并在改包下创建HelloController类。该类定义了sayHello()方法,用于处理路径为/hello的请求,代码如下:

package com.lz.controller;

import com.lz.entity.User;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayHello(){
        return "hello Feign";
    }
}

3.创建Feign
(1)创建Eureka Feign-client模块

如链接中 https://blog.nowcoder.net/n/fa86573da12f492fa6925d29b4c76e48 创建Eureka Server,建立Eureka Feign-client的模块。。

(2)添加Eureka Feign-client依赖

在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
https://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.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-feign-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-feign-client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>
    <dependencies>

        <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>
            <version>2.1.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.7.RELEASE</version>
        </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>
(3)配置全局文件application.yml

在Eureka Feign-client模块下,找到src/main/resources下创建文件application.yml,复制如下代码进行配置。。

spring:
  application:
    name: eureka-feign-client
server:
  port: 8764
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/
(4)启动EurekaFeginClientApplication

在src/main/java下创建启动类EurekaFeginClientApplication的Java文件。

package com.lz;

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 EurekaFeginClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaFeginClientApplication.class,args);
    }
}

(5)实现一个简单的Feign Client

首先在Eureka Feign-client中创建service包,并在该包下创建接口FeignService,通过添加@FeignClient注解指定要调用的服务。。具体代码如下:

package com.lz.service;

import com.lz.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@Service
@FeignClient(name="eureka-provider")
public interface FeignService {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayhello();
}

在Eureka Feign-client中创建controller包,并在该包下创建接口FeignController类,该类定义的hello()方法调用FeignService的sayHello()方法。。具体代码如下:

package com.lz.controller;

import com.lz.entity.User;
import com.lz.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {
    @Autowired
    FeignService feignService;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        return feignService.sayhello();
    }

}
(6)启动服务并进行测试

依次启动Eureka Server,Eureka Provider,Eureka Feign-client。启动成功后,使用浏览器访问 http://localhost:8764/hello ,效果如下所示。。

alt

从上图可以看出,页面调用了Eureke Provider服务输出的信息。与使用Ribbon调用服务相比,使用Feign远程调用服务时,只需要声明式指定要调用的服务名称,即可简单的实现服务调用。。。

4.参数绑定
(1)改造服务提供者Eureka Provider

在Eureka Provider中创建entity包,并在该包中创建实体类User,具体代码如下:

package com.lz.entity;

public class User {
    private String name;
    private Integer age;

    public User() {}

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name=:"+name+"   "+"age:"+age;
    }
}

在HelloController中新增3个不同类型参数的hello()方法。修改后的HelloController代码如下:

package com.lz.controller;

import com.lz.entity.User;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayHello(){
        return "hello Feign";
    }


    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    public String hello(@RequestParam String name){
        return "hello,"+name;
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public User hello(@RequestParam String name, @RequestHeader Integer age){
        return new User(name,age);
    }

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    public String hello(@RequestBody User user){
        return "hello,"+user.getName()+","+user.getAge();
    }
}

(2)改造含有Feign的Eureka Feign-client

在Eureka Feign-client中创建entity包,并在该包中创建实体类User,具体代码如下:

package com.lz.entity;

public class User {
    private String name;
    private Integer age;

    public User() {}

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name=:"+name+"   "+"age:"+age;
    }
}

在Feign中增加接口的绑定声明。修改后的代码如下:

package com.lz.service;

import com.lz.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@Service
@FeignClient(name="eureka-provider")
public interface FeignService {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayhello();

    @RequestMapping("port")
    public String port();

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

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public User hello(@RequestParam("name") String name,@RequestHeader("age") Integer age);

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    public String hello(@RequestBody User user);
}

在Feign中新增HelloUser()方法,处理"/helloUser"的请求,该方法调用了Feign绑定的接口,修改后的代码如下:

package com.lz.controller;

import com.lz.entity.User;
import com.lz.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {
    @Autowired
    FeignService feignService;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        return feignService.sayhello();
    }

    @RequestMapping("/port")
    public String port(){
        return feignService.port();
    }

    @RequestMapping(value = "/helloUser",method = RequestMethod.GET)
    public String helloUser(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(feignService.hello("zhangsan")).append("<br/>");

        stringBuilder.append(feignService.hello("lisi",30)).append("<br/>");

        stringBuilder.append(feignService.hello(new User("wangwu",27))).append("<br/>");

        return stringBuilder.toString();
    }
}
(3)启动服务并进行测试

依次启动Eureka Server,Eureka Provider,Eureka Feign-client。启动成功后,使用浏览器访问 http://localhost:8764/helloUser ,效果如下所示。。

alt