春城无处不飞花,寒食东风御柳斜,今天是2020年4月4日,清明节,
又是举国悼念在covid-19新冠肺炎疫情中,为了国家和同胞抵御病毒而牺牲烈士的一天。
博主在这里向医生哥哥和姐姐们还有护士们,致敬,白衣天使,救死扶伤,无比伟大。
小时候一直想,长大要做一个对社会有用的人,为建设和谐美好的社会贡献一份小小的力量。
不知道多久才能让朋友们感受到自己的微薄之力。
下面进入正题,springcloud中feign组件的使用
1、首先有一个spring-cloud-euraka server的注册中心(本身是一个springcloud服务)
2、紧接着而来的是一个spring-cloud-euraka-client 的springboot程序(伪装商品模块也是一个springboot服务)这个服务有两个
3、最后是spring-cloud-client-feign组件写的订单模块(也是springboot服务),要去调用springcloud-client-ribbon(商品模块服务)
调用时通过feign组件来进行调用哦,
下面就简单讲解一下springcloud-feign组件的使用
一、首先要引入几个关键的maven依赖
spring-boot-starter-web :提供对spring的web依赖组件
spring-cloud-starter-netflix-eureka-client :提供对spring的eureka 服务器交互通信的功能
spring-cloud-starter-openfeign :在eureka-client包的基础上进行了封装,主要也是和eureka服务器端进行交互
spring-boot-starter :引入了springboot的相关包 (包括自动装配等功能)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
二、在主类上面启动对springcloud-feign组件的支持
@EnableFeignClients(basePackages = "pafc.cloud.feignclient.service")
package pafc.cloud.feignclient;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringCloudApplication
@EnableFeignClients(basePackages = "pafc.cloud.feignclient.service")
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}
三、编写接口,构造feignclient,并将feignclient提交给spring进行管理
package pafc.cloud.feignclient.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value ="EUREKA-CLIENT-GOODS")
public interface ProductClient {
@GetMapping("queryGoods/allGoods")
public Object getAllGoods();
}
四、在相关service的实现类中注入并使用feign-client
下面的productClient就是spring容器注入进来的
@Autowired
public ProductClient productClient;
package pafc.cloud.feignclient.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pafc.cloud.feignclient.domain.Order;
import pafc.cloud.feignclient.service.OrderService;
import pafc.cloud.feignclient.service.ProductClient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.alibaba.fastjson.JSON.parseObject;
@Component
public class OrderServiceImpl implements OrderService {
@Autowired
public ProductClient productClient;
@Override
public List<Object> getOrderById(String orderId) {
List<Object> orderList = new ArrayList<Object>();
HashMap<String, Object> allGoods = (HashMap<String, Object>) productClient.getAllGoods();
for (Map.Entry<String, Object> item : allGoods.entrySet()) {
Object orderItem = item.getValue();
orderList.add(orderItem);
}
return orderList;
}
}
六、配置文件进行相应的支持,将当前服务注册到eureka注册中心,并且起个名字
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8181/eureka
spring:
application:
name: eureka-client-orders-feign
server:
port: 8188
ribbon:
eureka:
enabled: true
# 修改调用超时时间
feign:
client:
config:
default:
connectTimeout: 500
readTimeout: 500
七、进行相应的测试