栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

openfeign和dubbo远程调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

openfeign和dubbo远程调用

openfeign和dubbo远程调用

使用openfeign

消费端:

1.导入依赖

2.启动类加@EncableFeignClients注解(开启openfeign)

3.编写feign接口加上@FeignClient注解(绑定服务提供方)

4调用接口

1.依赖

    
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

2.主启动类添加注解

@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OpenFeignApp
{
    public static void main(String[] args)
    {
        SpringApplication.run(OrderHystrixMain80.class,args);
    }
}

3.feign接口

@Component
@FeignClient("SERVICE-PROVIDER")
public interface ProductFeignService {
    
    //通过控制层url进行帮订。
    @RequestMapping("/provider/product/findAll")
    public List findAll();

    @RequestMapping("/provider/product/findByPid")
    public String findByPid(@RequestParam("pid") Integer pid);
}

4.另外的服务调用接口

@RestController
@Slf4j
public class PaymentController {

@Resource
private ProductFeignService productFeignService; // 注入 service 中 通过 @FeignClient 定义好的接口

@GetMapping("/feign/payment/get/{id}")
public RespResult getPayment(@PathVariable("id") Integer id) {
    log.info("<<<<<<<<<<<  我是通过feign >>>>>>>>>>>>");
    return productFeignService.findByPid(id);
}

}

使用Dubbo远程调用

1.生产者消费者添加dubbo依赖

2.使用org.apache.dubbo.config.annotation.Service注解标记要远程引用的service

3.消费者使用@Refence注解注入Service

1.生产者消费者添加dubbo依赖


        com.alibaba.cloud
        spring-cloud-starter-dubbo
    

2.使用org.apache.dubbo.config.annotation.Service注解标记要远程引用的service

//dubbo里的@Service注解
import org.apache.dubbo.config.annotation.Service;

@Service
public class AppServiceImpl implements IAppService {
    @Autowired
    AppMapper appMapper;


    @Override
    public AppDTO createApp(AppDTO appDTO) throws BizException {
     

        return null;
    }

3.消费者使用@Refence注解注入Service

@Slf4j
@RestController
public class AppController {
//远程调用
@Reference
IAppService appService;

public AppDTO createApp(@RequestBody AppVo appVo){

    return appService.createApp(appDTO);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786191.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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