资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Springboot2XConsul中怎么利用Feign调用服务

Springboot2XConsul中怎么利用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=8010spring.application.name=providerspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.health-check-path=/actuator/healthspring.cloud.consul.discovery.service-name=service-providerspring.cloud.consul.discovery.heartbeat.enabled=truemanagement.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;@RestControllerpublic 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;@RestControllerpublic 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=8015spring.application.name=xyz-comsumerspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.register=falsespring.cloud.consul.discovery.health-check-url=/actuator/healthspring.cloud.consul.discovery.heartbeat.enabled=truemanagement.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@SpringBootApplicationpublic 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;@RestControllerpublic class FeignController {  @Autowired  private ProviderService providerService;  @RequestMapping("/call")  public String call() {    return providerService.hello();  }}

启动customer

访问http://localhost:8015/call

交替返回结果

hello,provider 或 hello,another provider

关于Springboot2XConsul中怎么利用Feign调用服务问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


当前题目:Springboot2XConsul中怎么利用Feign调用服务
当前网址:http://cdkjz.cn/article/gdodic.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220