1、在前面我们讲解了Ribbon,我们使用ribbon的方式主要是自己是同RestTemplate 来进行远程请求的调用,代码如下:
@GetMapping("userGetOrderById")
public OrderDto userGetOrderById(@RequestParam("orderId") String orderId){
//使用具备负载均衡功能的RestTemplate进行http服务调用
ResponseEntity<OrderDto> forEntity = restTemplate.getForEntity("http://order-service/getOrderById?orderId=" + orderId, OrderDto.class);
return forEntity.getBody();
}
这种方式在小项目中我们这样写确实也没啥问题,但是如果我们的接口特别多,这种方式就比较鸡肋了,因此spring cloud 为我们提供了OpenFeign来解决这个问题,接下来我们来看看如何使用openFeign?
2、使用OpenFeign
2.1、项目结构:
2.2、order-api 项目信息:
pom.xml文件:
<parent>
<artifactId>spring-cloud-openfeign</artifactId>
<groupId>com.wzy</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-api</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
定义的FeignClient :
@FeignClient("order-service")
@RequestMapping("order")
public interface OrderFeignClient {
@PostMapping("save")
boolean saveOrder(@RequestBody OrderDto orderDto);
@GetMapping("findAll")
Collection <OrderDto> findAll();
}
2.3、order-service 项目信息:
pom.xml文件:
<parent>
<artifactId>spring-cloud-openfeign</artifactId>
<groupId>com.wzy</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.wzy</groupId>
<artifactId>order-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
OrderController提供服务:
@RestController
@RequestMapping("order")
public class OrderController {
public static final Map<String,OrderDto> datas = new ConcurrentHashMap<>();
@PostMapping("save")
public boolean saveOrder(@RequestBody OrderDto orderDto){
datas.put(orderDto.getId(), orderDto);
return true;
}
@GetMapping("findAll")
public Collection<OrderDto> findAll(){
try {
Thread.sleep(800L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return datas.values();
}
}
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class OrderService {
public static void main(String[] args) {
SpringApplication.run(OrderService.class, args);
}
}
配置文件application.properties:
server.port=7070
spring.application.name=order-service
eureka.client.service-url.defaultZone=http://localhost:9090/eureka
eureka.client.registry-fetch-interval-seconds=10
2.4、user-service 项目信息:
pom.xml文件:
<parent>
<artifactId>spring-cloud-openfeign</artifactId>
<groupId>com.wzy</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.wzy</groupId>
<artifactId>order-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
调用order-service 的方式:
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private OrderFeignClient orderFeignClient;
@PostMapping("saverOrder")
public boolean saveOrder(@RequestBody OrderDto orderDto){
return orderFeignClient.saveOrder(orderDto);
}
@GetMapping("getAllOrder")
public Collection <OrderDto> getAllOrder(){
return orderFeignClient.findAll();
}
}
启动类:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(clients = {OrderFeignClient.class})
public class UserService {
public static void main(String[] args) {
SpringApplication.run(UserService.class, args);
}
}
配置文件application.properties:
spring.application.name=user-service
server.port=8080
eureka.client.service-url.defaultZone=http://localhost:9090/eureka
eureka.client.instance-info-replication-interval-seconds=10
#设置feign的请求超时时间为1秒
feign.client.config.default.read-timeout=1000
ribbon.ReadTimeout=1000
以上就是使用OpenFeign的代码,使用还是很简单的,接下来的文章我们将剖析其原理。