SpringCloud服務消費者:restTemplate和feignClient

1、概述

在springCloud微服務架構下,各個業務會被拆分為獨立的微服務。那么我們如何解決服務間調用的問題,springCloud默認提供了兩種方式:restTemplate和feignClient

2、兩者的區別

restTemplate:使用起來較為麻煩,需要自己指定ribbon的負載均衡,但參數較靈活,請求的路徑可以使用程序靈活控制。

feignClient:使用簡單,默認集成了ribbon負載均衡,無需自己配置,但參數不靈活,適用于api固定的接口。

3、使用示例

3.1、restTemplate

添加依賴

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

向容器中注入一個restTemplate實例,使用@LoadBalanced開啟負載均衡

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class DemoApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

在要使用的地方引入

    @Autowired
    private RestTemplate restTemplate;

get請求:

// 不帶參數
public String getDemo() {
    ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://demo-service/hello?name={name}", String.class);
    String body = responseEntity.getBody();
    System.out.println(body);
}

// 帶參數
public String getDemo() {
    Map<String, String> params = new HashMap<>(16);
    params.put("name", "Tony");
    ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://demo-service/hello?name={name}", String.class, params);
    String body = responseEntity.getBody();
    System.out.println(body);
}

post請求

// 帶參數
public String postDemo() {
    Map<String, String> params = new HashMap<>(16);
    params.put("name", "Tony");
    ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://demo-service/hello", params, String.class);
    String body = responseEntity.getBody();
    System.out.println(body);
}

put請求

public String putDemo() {
    restTemplate.put("http://demo-service/hello/{1}", "Tony");
}

delete請求

// 帶參數
public String deleteDemo() {
    restTemplate.delete("http://demo-service/hello/{1}", "Tony");
}

3.2、feignClient

1、pom文件添加依賴

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

2、在springBoot啟動類上添加注解@EnableFeignClients開啟feign功能

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {

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

3、使用時,定義feign的接口

// value指定服務名   fallback指定失敗回調類
@FeignClient(value = "demo-service", fallback = DemoServiceFallback.class)
public interface DemoService {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    // 需要注意的是,@RequestParam注解的value是必須的
    String sayHello(@RequestParam(value = "name") String name);
}


// fallback
@Component
public class DemoServiceFallback implements DemoService {
    @Override
    public String sayHello(String name){
        return "feign failed!";
    }
}

feign的請求方式

feign消費服務時,以GET方式請求的條件:
如果想讓服務消費者采用GET方式調用服務提供者,那么需要:
1.服務消費者這邊feign調用時,在所有參數前加上@RequestParam注解。
2.服務消費者這邊feign調用時,指明為GET方式(注:如果不指明method,那么在條件1滿足的情況下,采用的是默認的GET方式)。
注:這里條件1和條件2,是“且”的關系(都滿足時,才為GET)。

feign消費服務時,以POST方式請求的條件:
如果想讓服務消費者采用POST方式調用服務提供者,那么只需要:
1.服務消費者這邊feign調用時,在所有參數前加上@RequestParam注解,并指明feign消費服務的方式為POST。
2.服務消費者這邊feign調用時,有且只有一個參數前為@RequestBody或什么也沒有(如果有多個參數,那么其余參數前必須有@RequestParam)。
注:這里條件1和條件2,是“或”的關系(當至少一個滿足時,即為POST)。
注:在服務消費者中,使用feign消費服務時,如果參數前什么也不寫,那么默認是由@RequestBody指明的。
即:只要不滿足GET方式請求,那么POST方式請求是一定支持的。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容