欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud整合Feign

程序员文章站 2022-07-12 23:22:52
...

一 新建项目spring-feign-server

启动该项目

二 新建项目spring-feign-provider

1 新建控制器

package org.crazyit.cloud;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PoliceController {

    @RequestMapping(value = "/call/{id}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Police call(@PathVariable Integer id, HttpServletRequest request) {
        Police p = new Police();
        p.setId(id);
        p.setName("angus");
        p.setMessage(request.getRequestURL().toString());
        return p;
    }
    
    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    public String hello(@PathVariable String name) {
        return "Hello, " + name;
    }
    
}

2 新建启动类

package org.crazyit.cloud;

import java.util.Scanner;

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

@SpringBootApplication
@EnableEurekaClient
public class ProviderApp {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String port = scan.nextLine();
        new SpringApplicationBuilder(ProviderApp.class).properties("server.port=" + port).run(args);
    }

}

3 新建实体类

package org.crazyit.cloud;
public class Police {
      private Integer id;
      private String name;
      private String message;
      public Integer getId() {
            return id;
      }
      public void setId(Integer id) {
            this.id = id;
      }
      public String getName() {
            return name;
      }
      public void setName(String name) {
            this.name = name;
      }
      public String getMessage() {
            return message;
      }
      public void setMessage(String message) {
            this.message = message;
      }
      
}

4 启动两个实例

三 新建项目spring-feign-invoker

1 新加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

2 启动类开启Feign开关

package org.crazyit.cloud;

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
public class InvokerApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(InvokerApp.class).web(true).run(args);
    }

}

3  新建接口类

package org.crazyit.cloud;

import org.crazyit.cloud.contract.MyUrl;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("spring-feign-provider")
public interface HelloClient {

    @RequestMapping(method = RequestMethod.GET, value="/hello/{name}")
    String hello(@PathVariable("name") String name);
    
    
    @RequestMapping(method = RequestMethod.GET, value="/call/{id}")
    Police getPolice(@PathVariable("id") Integer id);
    

}

4 编写控制器

package org.crazyit.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    
    @Autowired
    private HelloClient helloClient;

    @RequestMapping(method = RequestMethod.GET, value="/router")
    public String router() {
        String result = helloClient.hello("angus");
        return result;
    }

    @RequestMapping(method = RequestMethod.GET, value="/police",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Police getPolice() {
        Police p = helloClient.getPolice(1);
        return p;
    }
}

5 启动spring-feign-invoker

四 测试结果

1 基本测试

Spring Cloud整合Feign

2 负载均衡测试 

Spring Cloud整合Feign

相关标签: feign