Feign调用

  1. 添加feign的jar包

    org.springframework.cloud
    spring-cloud-starter-openfeign

  1. 编写Feign的接口服务,用于微服务的提供者和消费者
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("XXX")
public interface IFeignService {
    @GetMapping("feign_get")
    String get(@RequestParam("name") String name);

    @PostMapping("feign_post")
    User post(@RequestBody User user);
}
  1. 在微服务的提供者一方,实现这个接口服务
import com.qf.feign.IFeignService;
import com.qf.feign.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController implements IFeignService {
    @Value("${server.port}")
    private String port;

    @Override
    public String get(String name) {
        return String.format("你好, %s, 调用的是GET方法, 端口为:%s", name, port);
    }

    @Override
    public User post(User user) {
        user.setName(user.getName().concat("   POST方法  ").concat(port));
        return user;
    }
}

  1. 在微服务的消费者一方,开启Feign;并实现这个接口服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;

@Service
@FeignClient(name = "EUREKA-CLIENT-PROVIDER")
public interface FeignService extends IFeignService {
}

接下来就直接注入这个FeignService并调用即可:

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    private final FeignUserService feignUserService;

    @GetMapping("get")
    public String feignGet(@RequestParam(value = "name") String name) {
        return feignUserService.feignGet(name);
    }

    @GetMapping("post")
    public String feignPost(@RequestParam(value = "name") String name) {
        return feignUserService.feignPost(name);
    }

    @GetMapping("getEntity")
    public User feignGetEntity(User user) {
        return feignUserService.feignGetEntity(user);
    }

    @GetMapping("postEntity")
    public User feignPostEntity(User user) {
        return feignUserService.feignPostEntity(user);
    }
}

你可能感兴趣的:(SpringCloud,-,教学)