@GetMapping("/RibbonServer/1")
public Map getRibbonServer1() {
// 新建對象
RestTemplate restTemplate = new RestTemplate();
//請求目標地址
String requestMsg = "方式一 GET 請求 RibbonServer";
Map response = restTemplate.getForObject("http://localhost:9999/RibbonServer/RibbonTest?requestMsg=" + requestMsg, Map.class);
log.info("response={}", response);
return response;
}
@PostMapping("/RibbonServer/1")
public Map postRibbonServer1() {
// 新建對象
RestTemplate restTemplate = new RestTemplate();
//請求目標地址
String requestMsg = "方式一 POST 請求 RibbonServer";
Map<String, Object> requestParam = new HashMap<>(16);
requestParam.put("requestMsg", requestMsg);
Map response = restTemplate.postForObject("http://localhost:9999/RibbonServer/RibbonTest", requestParam, Map.class);
log.info("response={}", response);
return response;
}
複製代碼
直接使用 RestTemplate , Url寫死java
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/RibbonServer/2")
public Map getRibbonServer2() {
// 獲取IP地址
ServiceInstance choose = loadBalancerClient.choose("RIBBON-SERVER");
String requestMsg = "方式二 GET 請求 RibbonServer";
String url = String.format("http://%s:%s", choose.getHost(), choose.getPort() + "/RibbonServer/RibbonTest?requestMsg=" + requestMsg);
RestTemplate restTemplate = new RestTemplate();
Map response = restTemplate.getForObject(url, Map.class);
log.info("response={}", response);
return response;
}
@PostMapping("/RibbonServer/2")
public Map postRibbonServer2() {
// 獲取IP地址
ServiceInstance choose = loadBalancerClient.choose("RIBBON-SERVER");
// 組裝URL
String url = String.format("http://%s:%s", choose.getHost(), choose.getPort() + "/RibbonServer/RibbonTest");
RestTemplate restTemplate = new RestTemplate();
String requestMsg = "方式二 POST 請求 RibbonServer";
Map<String, Object> requestParam = new HashMap<>(16);
requestParam.put("requestMsg", requestMsg);
Map response = restTemplate.postForObject(url, requestParam, Map.class);
log.info("response={}", response);
return response;
}
複製代碼
利用 LoadBalancerClient 經過應用名獲取 url,而後再使用 RestTemplate 請求git
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class RestTemplateConfig {
@Bean
@LoadBalanced //添加該註解,能夠直接經過服務名找到對應的IP地址
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
複製代碼
@Autowired
private RestTemplate restTemplate;
@GetMapping("/RibbonServer/3")
public Map getRibbonServer3() {
String requestMsg = "方式三 GET 請求 RibbonServer";
Map response = restTemplate.getForObject("http://RIBBON-SERVER/RibbonServer/RibbonTest?requestMsg=" + requestMsg, Map.class);
log.info("response={}", response);
return response;
}
@PostMapping("/RibbonServer/3")
public Map postRibbonServer3() {
String requestMsg = "方式三 POST 請求 RibbonServer";
Map response = restTemplate.postForObject("http://RIBBON-SERVER/RibbonServer/RibbonTest", getRequestParam(requestMsg), Map.class);
log.info("response={}", response);
return response;
}
複製代碼
利用 @LoadBalanced,可在 RestTemplate 裏面直接使用應用名字github
1.導入 OpenFeign 的包(SpringCloud2之後,Feign更名爲OpenFeign)web
<!--能夠不導入ribbon,由於feign裏面依賴了ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
複製代碼
2.啓動類添加 @EnableFeignClientsspring
@SpringBootApplication
@EnableFeignClients
public class SpringCloudRibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudRibbonClientApplication.class, args);
}
}
複製代碼
3.寫一個接口,用來調用指定的微服務(能夠由接口提供方寫好SDK,咱們直接調用)架構
@FeignClient(name = "ribbon-server") //調用的服務名
public interface RibbonServerClient {
/** * 測試請求 RibbonServer * @Method: GET * @author show * @date 16:26 2019/6/9 * @param requestMsg 接口請求參數 * @return java.util.Map 接口返回值,建議用對象接收 */
@GetMapping("/RibbonServer/RibbonTest")
Map getRibbonServer(@RequestParam("requestMsg") String requestMsg);
/** * 測試請求 RibbonServer * @Method: POST * @author show * @date 16:26 2019/6/9 * @param requestMsg 接口請求參數 * @return java.util.Map 接口返回值,建議用對象接收 */
@PostMapping("/RibbonServer/RibbonTest")
Map PostRibbonServer(@RequestBody Map requestMsg);
}
複製代碼
4.利用Spring的依賴注入,直接調用該接口方法,便可調用到 ribbon-server 服務的接口app
@RestController
@RequestMapping("/Feign")
@Slf4j
public class FeignController {
@Autowired
private RibbonServerClient ribbonServerClient;
/** * Feign 訪問微服務測試 * @author show * @date 16:42 2019/6/9 * @return java.util.Map */
@GetMapping("/RibbonServer")
public Map getRibbonServer() {
String requestMsg = "Feign GET 請求 RibbonServer";
Map response = ribbonServerClient.getRibbonServer(requestMsg);
log.info("response={}", response);
return response;
}
/** * Feign 訪問微服務測試 * @author show * @date 16:42 2019/6/9 * @return java.util.Map */
@PostMapping("/RibbonServer")
public Map postRibbonServer1() {
String requestMsg = "Feign Post 請求 RibbonServer";
Map<String, Object> map = new HashMap<>(16);
map.put("requestMsg", requestMsg);
Map response = ribbonServerClient.PostRibbonServer(map);
log.info("response={}", response);
return response;
}
}
複製代碼