创建个父工程:wenbei-mall 在父工程中引入:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在父工程下创建两个子工程,用户服务member和订单服务order。用户服务依赖订单服务的接口。
订单服务pom文件引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
订单controller:
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping(value = "/create", method = RequestMethod.POST)
public Result create(@Valid @RequestBody OrderCreateParam orderCreateParam) {
orderService.createOrder(orderCreateParam);
return Result.success("订单创建成功");
}
}
OrderService业务层实现:
@Override
public void createOrder(OrderCreateParam orderCreateParam) {
validOrderCreateParam(orderCreateParam);
OrderDto orderDto = saveOrder(orderCreateParam);
sendMessage(orderDto);
}
这里的重点不是业务层代码,均为空方法。
application.yml配置。基本的配置及nacos的地址
server:
port: 19278
servlet:
context-path: /order
spring:
application:
name: mall-order
cloud:
nacos:
server-addr: 127.0.0.1:8848
至此,Order服务的下单接口已完毕。
用户服务pom文件引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
启动类:
@SpringBootApplication
@EnableFeignClients
public class MemberApplication {
public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}
}
用户服务主配置类:
@Configuration
@EnableDiscoveryClient
public class MainConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
用户服务controller层调用order服务下单接口
@RequestMapping("/createOrder")
public String createOrder() {
OrderCreateParam orderCreateParam = new OrderCreateParam();
orderCreateParam.setUserId(1233L);
orderCreateParam.setShippingAddressId(123L);
List<OrderItem> orderItemList = new ArrayList<>();
orderCreateParam.setOrderItemList(orderItemList);
OrderItem orderItem = new OrderItem();
orderItem.setSkuId(1231234L);
orderItem.setQuantity(2);
orderItemList.add(orderItem);
Result result = orderService.create(JSONObject.toJSONString(orderCreateParam));
log.info("result===》{}", result);
return result.toString();
}
orderService.create方法:
@FeignClient(name = "mall-order", path = "/order")
public interface OrderService {
@RequestMapping(value = "/create", method = RequestMethod.POST)
Result create(OrderCreateParam orderCreateParam);
}
这里的OrderCreateParam和order服务里的OrderCreateParam 是完全不同的类,在各自的工程里,Result也是。不过可以提取公共的打成jar藏到公共仓库。类似于dubbo中的api层。
这里的入参和出参都为对象,实际返回的是字符串,feign会为我们自动转成对象。这里的入参和出参都可以改为String,也可以任意组合,及入参为String,出参为对象,或入参为对象,出参为String。
如下代码所示,入参直接写成String,那么在传入前需要转成JSON字符串。
@RequestMapping(value = "/create",method = RequestMethod.POST,produces = "application/json",headers = "content-type=application/json")
String create(String orderCreateParam);
不过,此时可能需要在headers 加content-type=application/json参数,这取决于服务方接口是如何接收参数的。可以回到刚刚的order服务下单接口那里,可以看到入参加了@RequestBody注解,这意味着请求时content-type要为application/json类型,即,参数以JSON方式传递。如果这里的的入参写的是String,则需要手动指定content-type=application/json,而上述参数为对象格式的无需指定,为什么?我的猜想,因为写String类型,框架并不知道要用哪种格式,默认为text/plain,但是order服务的下单接口入参上加了@RequestBody,表明需要为application/json类型。而为对象是不需要,我想是框架自动识别了吧。
至此,spring cloud alibaba集成feign已经完成。
源码: gitee地址: https://gitee.com/it-wenbei/wenbei-mall.git
|