Spring Cloud学习笔记:Feign声明试调用

基础配置

开发工具 :IDEA
MAVEN :3.6
JDK : 1.8
Spring boot :1.5.9.RELEASE
Spring cloud : Edgware.RELEASE

pox配置信息

哪个客户端要消费其他客户端,哪个就加上以下配置:

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

前提条件:

在上一篇的基础上写一个服务端和两个客户端.
cloud_eureka,cloud_client1,clud_client2(切记application.properties中应用名不能是下划线)
端口:8761,8081,8082

cloud_client1:

cloud_client1中写一个HelloController:

package com.cloud.client1.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/show")
public class HelloController {
    @GetMapping("/hello")
    public String  toHello(){
        return "show say Hello!";
    }

    @GetMapping("/noHello")
    public String  toNoHello(){
        return "show say no Hello!";
    }


}

cloud_client2:

cloud_client2的pox.xml中添加上面feign配置。
启动文件中添加:

@EnableFeignClients

注意:导入包为:

import org.springframework.cloud.openfeign.EnableFeignClients;

application.properties配置:

spring.application.name=cloud-client2

server.port=8082

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.thymeleaf.cache=false

feign.client.config.feignName.connectTimeout= 5000
feign.client.config.feignName.readTimeout=5000

feign.hystrix.enabled=false

创建一个Feign接口
(切记是value不是name,不然会报错

*“Exception with Eureka client Expected authority at index 7: http://”*或者feign.FeignException: status 404 reading CloudClient1Feign#showNoHel

package com.cloud.client2.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "cloud-client1")
public interface CloudClient1Feign {

    @RequestMapping(value = "/show/hello",method = RequestMethod.GET)
    String  showHello();
    @RequestMapping(value = "/show/noHello",method = RequestMethod.GET)
    String  showNoHello();

}

创建一个IndexController

package com.cloud.client2.controller;
import com.cloud.client2.feign.CloudClient1Feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/to")
public class IndexController {

    @Autowired
    private CloudClient1Feign cloudClient1Feign;

    @GetMapping("/hello")
    public String sayHello(){
        return "to say Hello!";
    }
    @GetMapping("/noHello")
    public String sayNOHello(){
        return "to say no Hello!";
    }

    @GetMapping("/getShow")
    public String  GetShow(){
        return cloudClient1Feign.showHello();
    }

    @GetMapping("/getNoShow")
    public String  GetNoShow(){
        return cloudClient1Feign.showNoHello();
    }


}

启动cloud_eureka,cloud_client1,cloud_client2
访问http://localhost:8761/
在这里插入图片描述访问http://localhost:8081/show/hello
在这里插入图片描述
访问http://localhost:8081/show/hello

cloud_client1上是show,cloud_client2是to
cloud_client2通过feign得到了show

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值