使用RestTemplate和Feign通訊

2021-09-17 05:26:46 字數 1213 閱讀 4391

resttemplate通訊的三種方式

1、直接呼叫

public string hello()", response);

return "hello world, i'm service-demo";

}缺點:再多台微服務部署的情況下,一般不知道ip和埠

2、使用loadbalancerclient構建出ip和埠

@autowired

private loadbalancerclient loadbalancerclient;

public string hello()", response);

return "hello world, i'm service-demo";

}

3、使用config配置

@component

public class resttemplateconfig

}@autowired

private resttemplate resttemplate;

public string hello()", response);

return "hello world, i'm service-demo";

}

feign通訊步驟

1、引用依賴

org.springframework.cloud

spring-cloud-starter-feign

1.4.5.release

2、新增feign註解

@enablefeignclients

public static void main(string args)

}

3、編寫遠端通訊介面

@feignclient(name = "service-demo")

public inte***ce serviceclient

4、呼叫方法

@autowired

private serviceclient serviceclient;

public string hello()", response);

return "hello world, i'm service-demo";

}

RestTemplate使用總結

resttemplate的介紹就不說了,總的來說用這個物件可以很方便的模擬乙個http請求。talk is cheap,show me the code,使用的是springboot整合的工程所以以下涉及到的物件都是註解式的宣告和注入,一 宣告resttemplate物件 當然,可以利用註解 bea...

RestTemplate使用總結

resttemplate的介紹就不說了,總的來說用這個物件可以很方便的模擬乙個http請求。talk is cheap,show me the code,使用的是springboot整合的工程所以以下涉及到的物件都是註解式的宣告和注入,一 宣告resttemplate物件 當然,可以利用註解 bea...

使用restTemplate隨筆

使用resttemplate訪問其他介面時 採用post方式傳參 發現服務端接受不到引數 坑了半天 記錄一下 服務端是個servlet 以post接參 所以本地使用mvc模擬了一下 方便除錯 如下 客戶端 如下 test public void test1 不能以map或hashmap代替 否則服務...