本篇文章給大家分享的是有關(guān)怎么在SpringCloud中使用Feign調(diào)用服務(wù),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),橋東企業(yè)網(wǎng)站建設(shè),橋東品牌網(wǎng)站建設(shè),網(wǎng)站定制,橋東網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,橋東網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
使用Spring Cloud Feign
創(chuàng)建一個(gè)SpringBoot工程,作為服務(wù)調(diào)用方
1.pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
spring-cloud-starter-feign加入了feign的依賴
2.啟動(dòng)類
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
@EnableFeignClients:開啟Spring Cloud Feign的支持功能
3.服務(wù)層
@FeignClient("hello-service") public interface HelloService { @RequestMapping(value = "/hello", method = RequestMethod.GET) String hello(); }
@FeignClient(“hello-service”):制定服務(wù)名來(lái)綁定服務(wù)
注:服務(wù)名不區(qū)分大小寫
@RequestMapping:指定調(diào)用服務(wù)中的具體方法
4.Controller層
@Controller public class ConsumerController { @Autowired private HelloService helloService; @RequestMapping(value = "/feign-consumer", method = RequestMethod.GET) @ResponseBody public String helloConsumer() { return helloService.hello(); } }
在方法中調(diào)用這個(gè)綁定了hello-service服務(wù)接口的客戶端向該服務(wù)發(fā)起/hello接口的調(diào)用
5.配置類
server.port=9001 spring.application.name=feign-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啟動(dòng)程序以后,在瀏覽器中輸入http://localhost:9001/feign-consumer出現(xiàn)下圖:
- 提升使用,帶參數(shù)的請(qǐng)求
在具體業(yè)務(wù)中的接口會(huì)比之前程序的復(fù)雜得多,這里介紹一下Feign對(duì)集中不同形式參數(shù)的綁定方法。有@RequestParam、@RequestHeader、@RequestBody
1.改造服務(wù)提供類的Service層,添加幾個(gè)方法
@RequestMapping(value = "/hello1", method = RequestMethod.GET) @ResponseBody public String hello(@RequestParam String name) { return "hello " + name; } @RequestMapping(value = "/hello2", method = RequestMethod.GET) @ResponseBody public User hello(@RequestHeader String name, @RequestHeader Integer age) { return new User(name, age); } @RequestMapping(value = "/hello3", method = RequestMethod.POST) @ResponseBody public String hello(@RequestBody User user) { return "Hello " + user.getName() + ", " + user.getAge(); }
2.添加一個(gè)javabean
public class User { private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
3.修改服務(wù)調(diào)用類的接口
@RequestMapping(value = "/hello1", method = RequestMethod.GET) String hello(@RequestParam("name") String name); @RequestMapping(value = "/hello2", method = RequestMethod.GET) User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age); @RequestMapping(value = "/hello3", method = RequestMethod.POST) String hello(@RequestBody User user);
這里在定義各參數(shù)綁定時(shí)@RequestParam、@RequestHeader等可以指定參數(shù)名稱的注解,但它們的value不能少,否則會(huì)報(bào)錯(cuò),這和SpringMVC中有一點(diǎn)不同
4.在服務(wù)調(diào)用類Controller層添加一個(gè)測(cè)試的接口
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET) @ResponseBody public String helloConsumer2() { String s2 = helloService.hello("dayday"); return s2; }
啟動(dòng)以后訪問(wèn)瀏覽器:
以上就是怎么在SpringCloud中使用Feign調(diào)用服務(wù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:怎么在SpringCloud中使用Feign調(diào)用服務(wù)
網(wǎng)站網(wǎng)址:http://newbst.com/article46/jhdhhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、定制網(wǎng)站、App開發(fā)、網(wǎng)站策劃、網(wǎng)站排名、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)