OpenFeign的使用

文章目录

cubemall-product

Blog.java

java 复制代码
package com.xd.cubemall.product.entity;



import lombok.Data;

@Data
public class Blog {
    private Long id;
    private String title;
    private String content;
    private String comment;
    private String mobile;
}

SearchFeignTest.java

java 复制代码
package com.xd.cubemall.feign;


import com.xd.cubemall.common.utils.R;
import com.xd.cubemall.product.CubemallProductApplication;
import com.xd.cubemall.product.entity.Blog;
import com.xd.cubemall.product.feign.SearchFeign;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallProductApplication.class)
public class SearchFeignTest {
    @Autowired
    private SearchFeign searchFeign;

    @Test
    public void testSayHello() {
        R r = searchFeign.sayHello("tom");
        System.out.println(r);
    }

    @Test
    public void testSayHello2() {
        R r = searchFeign.sayHello2("张三");
        System.out.println(r);
    }

    @Test
    public void testGetBlog() {
        Blog blog = new Blog();
        blog.setId(1l);
        blog.setTitle("hello");
        blog.setContent("world");
        R r = searchFeign.getBlog(blog);
        System.out.println(r);
    }

    @Test
    public void testGetBlog2() {
        Blog blog = new Blog();
        blog.setId(1l);
        blog.setTitle("hello");
        blog.setContent("world");
        R r = searchFeign.getBlog2(blog,"aaa");
        System.out.println(r);
    }

}

SearchFeign.java

java 复制代码
package com.xd.cubemall.product.feign;

import com.xd.cubemall.common.utils.R;
import com.xd.cubemall.product.entity.Blog;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

@FeignClient("cubemall-search")
public interface SearchFeign {

    @GetMapping("hello/{name}")
    R sayHello(@PathVariable("name") String name);

    @GetMapping("hello")
    R sayHello2(@RequestParam("name") String name);


    @PostMapping("/blog")
    public R getBlog(@RequestBody Blog blog);

    @PostMapping("/blog2")
    public R getBlog2(@RequestBody Blog blog, @RequestParam("name") String name);

    @GetMapping("/spuinfo/putonsale/{spuId}")
    R putOnSale(@PathVariable("spuId") Long spuId);
}

pom.xml

xml 复制代码
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

application.yml

yml 复制代码
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        ip: 127.0.0.1
  application:
    name: cubemall-product

HelloController.java

java 复制代码
package com.xd.cubemall.search.controller;


import com.xd.cubemall.common.utils.R;
import com.xd.cubemall.search.model.Blog;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {
    @GetMapping("hello/{name}")
    public R sayHello(@PathVariable String name) {
        return R.ok("hello" + name);
    }


    //http://localhost:8082/hello?name=lisi
    @GetMapping("hello")
    public R sayHello2(String name) {
        return R.ok("hello" + name);
    }

    @PostMapping("/blog")
    public R getBlog(@RequestBody Blog blog) {
        blog.setComment("接收到blog对象");
        blog.setMobile("111111");
        return R.ok(blog);
    }


    @PostMapping("/blog2")
    public R getBlog2(@RequestBody Blog blog, String name) {
        blog.setComment(name);
        blog.setMobile("222222");
        return R.ok(blog);
    }

}
相关推荐
菜菜-plus9 天前
分布式,微服务,SpringCloudAlibaba,nacos,gateway,openFeign
java·分布式·微服务·nacos·gateway·springcloud·openfeign
sdg_advance2 个月前
Spring Cloud之OpenFeign的具体实践
后端·spring cloud·openfeign
hac13222 个月前
OpenFeign原理
java·http·微服务·openfeign
OceanSky63 个月前
Spring Cloud OpenFeign 4组件
openfeign·feign
威哥爱编程3 个月前
OpenFeign深入学习笔记
java·springcloud·openfeign
ideal-cs4 个月前
索引:SpringCloudAlibaba分布式组件全部框架笔记
nacos·gateway·sentinel·seata·springcloud·openfeign·cloudalibaba
-无-为-4 个月前
科普文:微服务之Spring Cloud 服务调用组件Openfeign
spring cloud·微服务·架构·openfeign·feign·服务调用
Cloud-Future4 个月前
Spring cloud 中使用 OpenFeign:让 http 调用更优雅
java·spring cloud·openfeign·feign
A呆唯5 个月前
OpenFeign --学习笔记
笔记·学习·openfeign