SpringCloud 服務(wù)消費者(rest+ribbon/Feign)

在上一篇文章,講了服務(wù)的注冊和發(fā)現(xiàn)。在微服務(wù)架構(gòu)中,業(yè)務(wù)都會被拆分成一個獨立的服務(wù),服務(wù)與服務(wù)的通訊是基于http restful的。Spring cloud有兩種服務(wù)調(diào)用方式,一種是ribbon+restTemplate,另一種是feign。

一、ribbon簡介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
—–摘自官網(wǎng)

eureka是一個客戶端發(fā)現(xiàn)的注冊中心,所以需要客戶端具備負(fù)載均衡的能力,而ribbon就是一個負(fù)載均衡客戶端,可以很好的控制htt和tcp的一些行為。Feign默認(rèn)集成了ribbon。

ribbon 已經(jīng)默認(rèn)實現(xiàn)了這些配置bean:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl
  • IRule ribbonRule: ZoneAvoidanceRule
  • IPing ribbonPing: NoOpPing
  • ServerList ribbonServerList: ConfigurationBasedServerList
  • ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、建一個服務(wù)消費者端

2.1 這一篇文章基于上一篇文章的工程,啟動eureka-server 工程;啟動service-hi工程,它的端口為8081;將service-hi的配置文件的端口改為8082,并啟動,這時你會發(fā)現(xiàn):service-hi在eureka-server注冊了2個實例,這就相當(dāng)于一個小的集群。

idea多實例.png

服務(wù)注冊多實例.png

2.2 重新新建一個spring-boot工程,取名為:eureka-consumer;
在它的pom.xml和上一個差不多,多引一個spring-cloud-starter-netflix-ribbon庫即可

2.3同樣的在啟動類添加@EnableDiscoveryClient注解表明自己是個eureka客戶端。 并且向程序的ioc注入一個bean: restTemplate;并通過@LoadBalanced注解表明這個restRemplate開啟負(fù)載均衡的功能。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

配置文件如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
server:
  port: 8083
spring:
  application:
    name: service-ribbon

2.4 寫一個服務(wù)類HelloService,通過之前注入ioc容器的restTemplate來消費service-hi服務(wù)的“/hi”接口,在這里我們直接用的程序名替代了具體的url地址,在ribbon中它會根據(jù)服務(wù)名來選擇具體的服務(wù)實例,根據(jù)服務(wù)實例在請求的時候會用具體的url替換掉服務(wù)名,代碼如下:

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    public String hiService(String name){
        return restTemplate.getForObject("http://service-hi/hi?name="+name,String.class);
    }

}

2.5 最后寫一個controller測試,在controller中用調(diào)用HelloService 的方法,代碼如下:

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hi_ribbon")
    public String hi_ribbon(String name){
        return helloService.hiService(name);
    }
}

在瀏覽器上多次訪問http://localhost: 8083/hi_ribbon?name= springcloud,瀏覽器交替顯示:

hi springcloud,i am from port:8081
hi springcloud,i am from port:8082

這說明當(dāng)我們通過調(diào)用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法時,已經(jīng)做了負(fù)載均衡,訪問了不同的端口的服務(wù)實例。

三、此時架構(gòu)

image.png

四、Feign簡介

Feign是一個聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創(chuàng)建一個接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的編碼器和解碼器。Feign默認(rèn)集成了Ribbon,并和Eureka結(jié)合,默認(rèn)實現(xiàn)了負(fù)載均衡的效果。

簡而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有負(fù)載均衡的能力
  • 整合了Hystrix,具有熔斷的能力
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容