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

Spring boot2X Consul如何使用Feign实现服务调用

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

Spring boot2X Consul如何使用Feign实现服务调用

这篇文章主要介绍了spring boot2X Consul如何使用Feign实现服务调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

服务调用有两种方式:

  A.使用RestTemplate 进行服务调用

  B.使用Feign 进行声明式服务调用

上一次写了使用RestTemplate的方式,这次使用Feign的方式实现

服务注册发现中心使用Consul

启动Consul

consul agent -dev

spring boot 版本 2.2.1.RELEASE

1.服务端

provider

(1)添加依赖


  1.8
  Greenwich.SR3



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

  
    org.springframework.cloud
    spring-cloud-starter-consul-discovery
  



  
    
      org.springframework.cloud
      spring-cloud-dependencies
      ${spring-cloud.version}
      pom
      import
    
  

(2)修改配置

server.port=8010

spring.application.name=provider
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=service-provider
spring.cloud.consul.discovery.heartbeat.enabled=true

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

(3)测试方法

package com.xyz.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoController {
  @RequestMapping("/hello")
  public String Hello(){
    return "hello,provider";
  }

}

provider1

修改端口为8011

修改测试方法

package com.xyz.provider1.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {
  @RequestMapping("/hello")
  public String Hello(){
    return "hello,another provider";
  }
}

启动provider和provider1

2.客户端

customer

(1)添加依赖


   1.8
   Greenwich.SR4


    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.cloud
      spring-cloud-starter-consul-discovery
    
    
      org.springframework.cloud
      spring-cloud-starter-openfeign
    



    
      
 org.springframework.cloud
 spring-cloud-dependencies
 ${spring-cloud.version}
 pom
 import
      
    

(2)配置

server.port=8015
spring.application.name=xyz-comsumer
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.register=false
spring.cloud.consul.discovery.health-check-url=/actuator/health
spring.cloud.consul.discovery.heartbeat.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

(3)修改启动类

添加注解 @EnableFeignClients,开启扫描Spring Cloud Feign客户端的功能

package com.xyz.comsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableFeignClients
@SpringBootApplication
public class ComsumerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ComsumerApplication.class, args);
  }
}

(4)添加Feign接口

添加注解@FeignClient(name = "provider")

provider是要调用的服务名

说明:

  添加跟调用目标方法一样的方法声明,必须跟目标方法的定义一致

package com.xyz.consumer.controller;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name = "provider")
public interface ProviderService {
  @RequestMapping("/hello")
  public String hello();
}

(4)服务调用

注入刚才声明的ProviderService,就可以像本地方法一样进行调用了

package com.xyz.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignController {
  @Autowired
  private ProviderService providerService;
  @RequestMapping("/call")
  public String call() {
    return providerService.hello();
  }
}

启动customer

访问http://localhost:8015/call

交替返回结果

hello,provider 或 hello,another provider

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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