Feign主要為了面向接口編程
feign是web service客戶端,是接口實(shí)現(xiàn)的,而ribbon是通過微服務(wù)名字訪問通過RestTemplate調(diào)用的,如下:
在Feign的實(shí)現(xiàn)下,我們只需要創(chuàng)建一個(gè)接口并使用注解的方式來配置它(類似于以前Dao接口上標(biāo)注Mapper注解,現(xiàn)在是一個(gè)微服務(wù)接口上面標(biāo)注一個(gè)Feign注解即可)
在總的springcloud-api中,編寫服務(wù)接口
都導(dǎo)入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
這里將接口寫好之后使用@Component注解注入spring容器,然后新建模塊springcloud-consumer-dept-feign
這里其實(shí)就是springcloud-consumer-dept-80模塊復(fù)制過去,不一樣的是
這里使用接口方式注入,也就是用了spring容器的思想不使用RestTemplate來獲取
然后使用@EnableFeignClients注解和@ComponentScan注解去掃描包路徑文章來源:http://www.zghlxwxcb.cn/news/detail-698194.html
即可成功訪問獲得數(shù)據(jù)文章來源地址http://www.zghlxwxcb.cn/news/detail-698194.html
到了這里,關(guān)于Feign負(fù)載均衡寫法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!