SpringCloud 学习(8):SpringCloud中使用Feign

这篇文章基于SpringCloud 学习(3):eureka集群的基础上进行扩展。


1.首先修改服务提供者:

这里写图片描述

  • 添加police实体

public class Police {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public Police setId(Integer id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Police{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • 修改PoliceController

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
public class PoliceController {
    @RequestMapping("call")
    public String call(HttpServletRequest request) {
        return "call:" + request.getRequestURL().toString();
    }

    @RequestMapping("getPolice")
    public Police getPolice(@RequestParam("id") Integer id) {
        Police police = new Police()
                .setId(id)
                .setName("一号警察")
                .setAge(24);
        return police;
    }

    @RequestMapping("addPolice")
    public Police addPolice(@RequestBody Police police) {
        return police;
    }
}

2.修改服务调用者

这里写图片描述

  • 引入fiegn依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
  • 开启FeignClient,修改启动类如下:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients  //*******开启FeignClient
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}
  • 编写Feign服务接口FeignService

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("police")  //指定服务ID
public interface FeignService {
    @RequestMapping(method = RequestMethod.GET,value = "/call")
    public String call();

    @RequestMapping(method = RequestMethod.GET,value = "/getPolice")
    public Police getPolice(@RequestParam("id") Integer id);

    @RequestMapping(method = RequestMethod.POST, value = "/addPolice")
    public Police addPolice(@RequestBody Police police);
}
  • 服务调用,修改PeopleController
import com.warrior.feign.FeignService;
import com.warrior.feign.Police;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class PeopleController {

    @Autowired
    private FeignService feignService;

    @RequestMapping(method = RequestMethod.GET,value = "call")
    public String call(){
      return feignService.call();
    }

    @RequestMapping(method = RequestMethod.GET,value = "getPolice")
    public Police getPolice(Integer id){
        return feignService.getPolice(id);
    }


    @RequestMapping(method = RequestMethod.GET,value = "addPolice")
    public Police addPolice(){
        Police police = new Police()
                .setId(1)
                .setAge(24)
                .setName("二号警察");
        return feignService.addPolice(police);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37754981/article/details/80927258