栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

(七)feign-简化服务调用

(七)feign-简化服务调用

前面的章节虽然完成了服务的注册以及相互间的调用,但是调用方发起调用时,还是硬编码的形式,如果服务比较多,代码不好管理。如,用户服务调用订单服务:

    @PostMapping("/findOrderList")
    public JSONArray findOrderList() throws Exception {
        String serviceId = "ORDER";
        String url = "http://" + serviceId + "/order/findOrderList";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity formEntity = new HttpEntity(null, headers);

        JSONArray responseEntity
                = restTemplate.postForObject(url, formEntity, JSONArray.class);
        return responseEntity;
    }

feign很好的封装了这些调用细节,可以使得客户端像调用本地方法一样调用远程服务。

service-api模块

新增service-api模块,统一管理接口,用户服务和订单服务都依赖该模块。

    pom文件配置


    
        eureka-parent
        com.ft
        0.0.1-SNAPSHOT
    
    4.0.0

    service-api


    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-configuration-processor
        
        
            org.springframework.boot
            spring-boot-starter-aop
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.retry
            spring-retry
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
        
            com.alibaba
            fastjson
            1.2.51
        
        
            com.squareup.okhttp3
            okhttp
            4.2.2
        
    

    OrderControllerApi.java
@RequestMapping("order")
@FeignClient(value = "order") // 当前类视为客户端,并指定服务别名
public interface OrderControllerApi {

    
    @PostMapping("/findOrderList")
    public JSONArray findOrderList() throws Exception;

    @GetMapping("/hello")
    public String hello();

}
    UserControllerApi.java
@RequestMapping("user")
public interface UserControllerApi {

    
    @PostMapping("/findOrderList")
    public JSONArray findOrderList() throws Exception;

    @GetMapping("/hello")
    public String hello();

}
订单服务
    订单服务pom.xml依赖service-api


    
        eureka-parent
        com.ft
        0.0.1-SNAPSHOT
    
    4.0.0

    order

    
        
        
            com.ft
            service-api
            0.0.1-SNAPSHOT
        
    


    订单服务OrderController实现service-api里的OrderControllerApi
@RestController
public class OrderController implements OrderControllerApi {

    @Value("${server.port}")
    private String port;

    @Override
    public JSONArray findOrderList() throws Exception {
        JSONArray orderList = new JSONArray();
        JSONObject o1 = new JSONObject();
        o1.put("ordNo", "001");
        o1.put("ordName", "鼠标");
        JSONObject o2 = new JSONObject();
        o2.put("ordNo", "002");
        o2.put("ordName", "键盘");
        JSONObject o3 = new JSONObject();
        o3.put("port", port);
        System.out.println("port=" + port);
        orderList.add(o1);
        orderList.add(o2);
        orderList.add(o3);
        return orderList;
    }


    @Autowired
    public String hello() {
        return "hello order!!!";
    }

}
用户服务
    用户服务pom.xml依赖service-api


    
        eureka-parent
        com.ft
        0.0.1-SNAPSHOT
    
    4.0.0

    user

    
        
        
            com.ft
            service-api
            0.0.1-SNAPSHOT
        
    


    用户服务UserController实现service-api里的UserControllerApi
@RestController
public class UserController implements UserControllerApi {
    
    @Autowired
    private OrderControllerApi orderControllerApi;

    @Override
    public JSONArray findOrderList() throws Exception {
    	// 发起调用订单服务
        JSONArray result = orderControllerApi.findOrderList();
        return result;
    }

    @Override
    public String hello() {
        return "hello user!!!";
    }
}
测试

postman测试结果跟之前的完全一致。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/728799.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号