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

互联网技术-Alibaba-nacos-远程服务调用-使用技术feign

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

互联网技术-Alibaba-nacos-远程服务调用-使用技术feign

  • RestTemplate和WebClient都是Spring自己封装的工具

  • Feign 是 Spring Cloud 的成员

  • Spring Cloud Alibaba 支持对Feign的调用

  • 1.需要在消费方consumer项目的pom文件中添加对应的坐标

  • 2.需要建立feign对应的接口,在接口中书写provider也就是数据提供方中controller层中对应的方法请求:如下:

    3.编写测试的controller,在里面调用接口中定义的方法即可进行远程调用

    4.最后切记在项目启动类上加上对应的注解

     
最后附上代码:

consumer的pom坐标


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.apache.httpcomponents
            httpclient
            4.5.6
        
        
        
            org.projectlombok
            lombok
        
        
        
            com.alibaba
            fastjson
            1.2.49
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        

        
        
            io.springfox
            springfox-swagger2
        
        
            io.springfox
            springfox-swagger-ui
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

        
        
            org.springframework.boot
            spring-boot-starter-webflux
        


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

接口feign

package com.czxy.feign;

import com.czxy.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//远程服务器名 和短路径
@FeignClient(value = "user-service",path = "/user")
public interface UserControllerFeign {

    @GetMapping("/findAll")
    public List findAll();

    @PostMapping("/add")
    public String save(@RequestBody User user);


    @PutMapping("/update")
    public String update(@RequestBody User user);

    @DeleteMapping("/del/{id}")
    public String delete(@PathVariable("id") Integer id);

    @PostMapping("/findByCondition")
    public List findByCondition(@RequestBody User user);
}

controller:

package com.czxy.controller;

import com.czxy.domain.User;
import com.czxy.feign.UserControllerFeign;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/feign")
public class TestFeignController {

    //feign对象调用
    @Resource
    private UserControllerFeign userControllerFeign;

    @GetMapping("/findAll")
    public List findAll(){
        return userControllerFeign.findAll();
    }

    @PostMapping("/add")
    public String save(@RequestBody User user){
        return userControllerFeign.save(user);
    }

    @PutMapping("/update")
    public String update(@RequestBody User user){
        return userControllerFeign.update(user);
    }


    @DeleteMapping("/del/{id}")
    public String delete(@PathVariable("id") Integer id){
        return userControllerFeign.delete(id);
    }

    @PostMapping("/findByCondition")
    public List findByCondition(@RequestBody User user){
        return userControllerFeign.findByCondition(user);
    }
}

项目启动类:

package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.reactive.config.EnableWebFlux;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//feign远程调用
public class CompanyHttpApplication {
    public static void main(String[] args) {
        SpringApplication.run(CompanyHttpApplication.class,args);
    }
}

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

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

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