openfeign替换httpclient

时间:2022-10-16 12:56:41

openfein是spring cloud的一个服务间调用组件,这边文档介绍使用openfeign代替HttpClient调用远程服务

1. 创建一个springboot服务RemoteService

application.properties

server.port=9001
spring.application.name=remote-service

TestRest.java

@RestController
@RequestMapping("/test")
public class TestRest {

    @PostMapping("/token")
    public String token(@RequestParam("username")String username, @RequestParam("password")String password) {
        return Base64.getEncoder().encodeToString((username + password).getBytes(StandardCharsets.UTF_8));

    }

}

RemoteApplication.java

@SpringBootApplication
public class RemoteApplication {
    public static void main(String[] args) {
        SpringApplication.run(RemoteApplication.class, args);
    }
}

启动服务访问接口

API:/test/token
Param: username=root,password=123456
返回结果:cm9vdDEyMzQ1Ng==

2. 创建一个服务使用feign调用上面服务

application.properties

spring.application.name=springboot_demo
server.port=8991

User.java

public class User {

    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

==TestFeign.java==

@FeignClient(
        name = "TestFeign",
        url = "baidu.com",
        path = "/test"
)
public interface TestFeign {

    @PostMapping("/token")
    String getToken1(URI uri, @RequestParam("username")String username, @RequestParam("password")String password);

    @PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String getToken2(URI uri, @SpringQueryMap User user);
}

HelloRest.java

@RestController
@RequestMapping("/hello")
public class HelloRest {

    @Autowired
    private TestFeign testFeign;

    @GetMapping("/name")
    public String getName(@RequestParam(value = "name", defaultValue = "boot") String name) {
        return "hello:" + name;
    }

    @PostMapping("/token1")
    public String getToken1(@RequestParam("username")String username, @RequestParam("password")String password) throws Exception{
        return testFeign.getToken1(new URI("localhost:9001"), username, password);
    }

    @PostMapping("/token2")
    public String getToken2(@RequestParam("username")String username, @RequestParam("password")String password) throws Exception{
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return testFeign.getToken2(new URI("localhost:9001"), user);
    }
}

Application.java

@EnableFeignClients
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

记得加 @EnableFeignClients

测试

  1. 接口/hello/token1, /hello/token2 请求参数都是username=root, password=123456 返回结果都是:cm9vdDEyMzQ1Ng==

  2. TestFeign说明

  • URI动态参数 TestFeign中的@FeignClient注解中的url="baidu.com",这里主要说明使用URI作为参数可以使用 ==动态URL==,即你的url可以每次不一样,实际开发中调用的远程服务一般不只一种,可以使用这种方式使用自定义请求地址,如果涉及header加入@RequestHeader处理即可

  • 远程服务多个参数,本地调用使用对象 token2方法中的注解@PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE),对象参数使用了注解@SpringQueryMap,此处如果使用@RequestBody会报错