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

springboot openfeign服务端与客户端调用演示demo

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

springboot openfeign服务端与客户端调用演示demo

文章目录
  • server demo 演示
    • 创建server项目
    • application.properties配置
    • import jar [pom.xml]
    • 创建服务端的restful controller
    • 验证
  • openfeign client demo
    • 创建client项目
    • import jar【pom.xml】
      • 注意 springboot与springcloud版本的一个对照 参考
    • application.properties配置
    • 调用服务端的client接口编码
    • 客户端restful controller编码
    • 客户端调用验证
  • 源码
  • 写在最后

server demo 演示 创建server项目 application.properties配置
spring.application.name=springboot-sample-openfeign-server
server.port=8086
import jar [pom.xml]


    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    

    com.gaoxinfu.springboot.sample.openfeign
    springboot-sample-openfeign-server
    1.0.0
    springboot-sample-openfeign-server
    springboot-sample-openfeign-server
    pom

    
        1.8
    

    
        
            maven-public
            maven-public
            http://localhost:8081/repository/maven-public/
            
                true
            
            
                true
                always
                warn
            
        
    

    
    
        
            releases
            Nexus Release Repository
            http://localhost:8081/repository/releases/
        
        
            snapshots
            Nexus Snapshot Repository
            http://localhost:8081/repository/snapshots/
        
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                ${basedir}/src/main/webapp
                META-INF/resources
                
                    ****
                
            
        
    

创建服务端的restful controller

@RestController
@Controller
public class IndexController {


    @PostMapping("/index")
    public IndexResDto index(@RequestBody IndexReqDto indexReqDto) {
        System.out.println("indexReqDto = "+indexReqDto);
        return new IndexResDto(1,"gaoxinfu","man",18);
    }
}
验证 openfeign client demo 创建client项目 import jar【pom.xml】


    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    

    com.gaoxinfu.springboot.sample.openfeign
    springboot-sample-openfeign-client
    1.0.0
    springboot-sample-openfeign-client
    springboot-sample-openfeign-client
    pom

    
        1.8
    

    
        
            maven-public
            maven-public
            http://localhost:8081/repository/maven-public/
            
                true
            
            
                true
                always
                warn
            
        
    

    
    
        
            releases
            Nexus Release Repository
            http://localhost:8081/repository/releases/
        
        
            snapshots
            Nexus Snapshot Repository
            http://localhost:8081/repository/snapshots/
        
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                
                Greenwich.SR6
                pom
                import
            
        
    

    
        
            
                ${basedir}/src/main/webapp
                META-INF/resources
                
                    ****
                
            
        
    

注意 springboot与springcloud版本的一个对照 参考

https://blog.csdn.net/u014636209/article/details/125863935?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125863935%22%2C%22source%22%3A%22u014636209%22%7D&ctrtid=dXJ80

application.properties配置
spring.application.name=springboot-sample-openfeign-client
server.port=8085
调用服务端的client接口编码
@FeignClient(name = "demo",url = "http://localhost:8086")
public interface ServerFeignClient {

    @GetMapping("/index")
    IndexResDto index(@RequestBody IndexReqDto indexReqDto);
}

客户端restful controller编码
@RestController
@Controller
public class DemoController {

    @Resource
    private ServerFeignClient serverFeignClient;

    @GetMapping("/client/index")
    public IndexResDto index(@RequestParam int id) {
        return serverFeignClient.index(new IndexReqDto(id));
    }
}
客户端调用验证 源码

https://github.com/gaoxinfu/springboot-sample/tree/master/springboot-sample-openfeign

写在最后

欢迎点赞,收藏,转发,关注,一起学习,一起成长
https://github.com/gaoxinfu/springboot-sample/tree/master/springboot-sample-openfeignd现在
https://github.com/gaoxinfu/springboot-sample/tree/master/springboot-sample-openfeign

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

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

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