Java教程:SpringCloud以http方式实现feign远程调用

第一步,导入pom

<!--fegin启用-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--hystrix启用-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

第二步,yml配置增加如下

# 熔断器
feign:
  hystrix:
    enabled: true

#指定熔断超时时间 毫秒
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 21000

第三步,启动类增加@EnableCircuitBreaker,@EnableFeignClients

@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients
public class Application {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        logger.info("-----------------------");
        logger.info("application is success!");
        logger.info("-----------------------");
    }
}

第四步,增加service 接口与实现类

import java.util.List;
import java.util.Map;

/**
 * @author wfeil211@foxmail.com
 * @Date 2019/7/18
 */
@FeignClient(value = "service", fallback = ServiceFail.class)
public interface Service {

    /**
     * 获取
     *
     * @param cardNo
     * @return
     */
    @RequestMapping(value = "url/getBalance", method = RequestMethod.GET)
    JsonObject<String> getBalance(@RequestParam("cardNo") String cardNo);
}

import java.util.List;
import java.util.Map;

/**
 * @author wfeil211@foxmail.com
 * @version 1.0 2018-07-10  10:24:05
 */
@Component
public class ServiceFail implements Service {

    /**
     * 获取
     *
     * @param cardNo
     * @return
     */
    @Override
    public JsonObject<String> getBalance(String cardNo) {
        return null;
    }

}

注意:此方式适用于直接调用其他服务controller层接口,属于主动,另一张rpc方式下次出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Feign是一个基于注解的声明式HTTP客户端,它可以简化服务之间的远程调用。使用Feign可以像调用本地方法一样调用远程服务。 要使用Feign进行远程调用,需要进行以下几个步骤: 1. 添加依赖:在项目的pom.xml文件中添加Feign的依赖。例如,对于Spring Boot项目,可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 创建Feign客户端接口:定义一个Java接口,使用`@FeignClient`注解指定要调用的远程服务的名称。在接口中定义需要调用的方法,并使用`@RequestMapping`等注解指定远程服务的URL和参数。 ```java @FeignClient(name = "remote-service") public interface RemoteServiceClient { @RequestMapping(method = RequestMethod.GET, value = "/api/resource") String getResource(); } ``` 3. 配置Feign客户端:在Spring Boot应用程序的配置文件中,可以配置Feign客户端的一些属性,如远程服务的URL、连接超时时间等。例如: ```yaml spring: application: name: my-application cloud: feign: client: config: remote-service: connect-timeout: 5000 read-timeout: 5000 ``` 4. 使用Feign客户端:在需要调用远程服务的地方,通过依赖注入的方式使用Feign客户端接口,并调用定义的方法即可。 ```java @Autowired private RemoteServiceClient remoteServiceClient; public void doRemoteCall() { String resource = remoteServiceClient.getResource(); // 处理返回的资源数据 } ``` 通过以上步骤,就可以使用Feign进行远程调用了。Feign会根据接口定义自动生成对应的HTTP请求,并将结果转换为指定类型返回。同时,Feign还提供了负载均衡、熔断等功能,可以更方便地实现微服务架构中的服务调用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值