在A中pom.xml引入jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
启动类加上注解@EnableFeignClients
package com.hlvy.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
接着写个接口通过接口调用
package com.hlvy.order.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* ProductClient
*
* @author heng
**/
@FeignClient(name = "hlvyclient")//要调用B服务应用名称
public interface ProductClients {
@RequestMapping("/httpclien/httpstr") //这里的/httpclien/httpstr对应的是B的controller访问路径
String productMsg();
}
最后
package com.hlvy.order.controller;
import com.hlvy.order.client.ProductClients;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* HttpClient
*
* @author heng
**/
@RestController
@RequestMapping("client")
@Slf4j
public class HttpClient {
@Autowired
private ProductClients productClients;
@RequestMapping("clitest")
public String hclient(){ //跨服务调用
String msg = productClients.productMsg();
log.info(msg+"------------------------------------------------");
return msg;
}
}
带参数传递使用@RequestBody
A服务是调用B服务方法
B服务Controller方法定义
/**
* 获取商品列表
* @param stringList
* @return
*/
@PostMapping("productInfoList")
public List<ProductInfo> productInfoList(@RequestBody List<String> stringList ){
return productService.findList(stringList);
}
A服务方法
package com.hlvy.order.client;
import com.hlvy.order.entity.ProductInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* ProductClient
*
* @author heng
**/
@FeignClient(name = "product")
public interface ProductInfoc {
@PostMapping("/product/productInfoList")
List<ProductInfo> productMsg(@RequestBody List<String> list);
}
A服务调用页面
package com.hlvy.order.controller;
import com.hlvy.order.client.ProductClients;
import com.hlvy.order.client.ProductInfoc;
import com.hlvy.order.entity.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* HttpClient
*
* @author heng
**/
@RestController
@RequestMapping("client")
@Slf4j
public class HttpClient {
@Autowired
private ProductInfoc productInfoc;
@RequestMapping("prostr")
public String prostr(){ //跨服务调用
List<ProductInfo> list = productInfoc.productMsg(Arrays.asList("157875196366160022","157875227953464068"));
System.err.println(list.size());
log.info(list+"------------------------------------------------");
System.out.println(list.stream().map(ProductInfo::getProductName).collect(Collectors.toList()));
return list.stream().map(ProductInfo::getProductName).collect(Collectors.toList()).toString();
}
}
注意如果使用了多模块化需要加上
@EnableFeignClients(basePackages = "com.hlvy.product.client")在启动类上basePackages =client存放路径