美文网首页
【四】Spring Cloud Feign

【四】Spring Cloud Feign

作者: lemonMT | 来源:发表于2019-03-08 17:05 被阅读0次

基于上个节点的demo-springcloud-service-user进行试验操作。
因为Feign依赖中已经包含了Ribbon,所以在本次中,将Ribbon依赖摘除。

1. 增加本次所需的依赖,备注cloud版本不一致,依赖包也不一样

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

2. 修改启动类代码

//发现服务注册中心,将服务进行注册
@EnableDiscoveryClient
//开启feign能力
@EnableFeignClients
@SpringBootApplication
public class DemoSpringcloudServiceUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoSpringcloudServiceUserApplication.class, args);
    }
}

3. 增加接口配置类

//所有客户端接口集合,需要谁的服务,就配置谁的applicationName
@FeignClient("demo-springcloud-service-user")
public interface FeignUserService {

    //对方服务的请求名称
    @GetMapping("/findUser")
    String findUserById();
    
}

4. 修改调用类

@RestController
public class UserController {

    @Resource
    private FeignUserService   userService ;

    //调用feign进行调用微服务
    @GetMapping("testFeign")
    public Object  testFeign(){

        return   userService.findUserById();
    }
}

5. 预览体验

调用:http://127.0.0.1:8899/testFeign

image.png

相关文章

网友评论

      本文标题:【四】Spring Cloud Feign

      本文链接:https://www.haomeiwen.com/subject/tbarpqtx.html