Spring boot2X Consul如何使用Feign实现服务调用
这篇文章主要介绍了springboot2XConsul如何使用Feign实现服务调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
服务调用有两种方式:
A.使用RestTemplate进行服务调用
B.使用Feign进行声明式服务调用
上一次写了使用RestTemplate的方式,这次使用Feign的方式实现
服务注册发现中心使用Consul
启动Consul
consulagent-dev
springboot版本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)测试方法
packagecom.xyz.provider.controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassdemoController{ @RequestMapping("/hello") publicStringHello(){ return"hello,provider"; } }
provider1
修改端口为8011
修改测试方法
packagecom.xyz.provider1.controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassdemoController{ @RequestMapping("/hello") publicStringHello(){ return"hello,anotherprovider"; } }
启动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,开启扫描SpringCloudFeign客户端的功能
packagecom.xyz.comsumer; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.cloud.client.loadbalancer.LoadBalanced; importorg.springframework.context.annotation.Bean; importorg.springframework.web.client.RestTemplate; @EnableFeignClients @SpringBootApplication publicclassComsumerApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(ComsumerApplication.class,args); } }
(4)添加Feign接口
添加注解@FeignClient(name="provider")
provider是要调用的服务名
说明:
添加跟调用目标方法一样的方法声明,必须跟目标方法的定义一致
packagecom.xyz.consumer.controller; importorg.springframework.cloud.openfeign.FeignClient; importorg.springframework.web.bind.annotation.RequestMapping; @FeignClient(name="provider") publicinterfaceProviderService{ @RequestMapping("/hello") publicStringhello(); }
(4)服务调用
注入刚才声明的ProviderService,就可以像本地方法一样进行调用了
packagecom.xyz.consumer.controller; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassFeignController{ @Autowired privateProviderServiceproviderService; @RequestMapping("/call") publicStringcall(){ returnproviderService.hello(); } }
启动customer
访问http://localhost:8015/call
交替返回结果
hello,provider或hello,anotherprovider
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。