springcloud使用使用Feign-Ribbon做負載均衡實現聲明式REST調用


什么是Feign

Feign是一個聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創建一個接口並注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的編碼器和解碼器。Feign默認集成了Ribbon,並和Eureka結合,默認實現了負載均衡的效果。注意:負載均衡是加在客戶端中,而不是服務端中。

簡而言之:

Feign 采用的是基於接口的注解

Feign 整合了ribbon

代碼實現及演示效果

注冊中心(略)

1、在客戶端添加依賴包

1
2
3
4
< dependency >
     < groupId >org.springframework.cloud</ groupId >
     < artifactId >spring-cloud-starter-feign</ artifactId >
</ dependency >

2、在客戶端啟動類中使用@EnableFeignClients開啟feiginClient功能

1
2
3
4
5
6
7
8
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public  class  ServiceAApplication {
     public  static  void  main(String[] args) {
         SpringApplication.run(ServiceAApplication. class , args);
     }
}

3、在客戶端中新建一個ServiceAFeignClient接口調用service-b的服務

1
2
3
4
5
6
7
8
9
10
11
package  com.example.servicea;
import  org.springframework.cloud.netflix.feign.FeignClient;
import  org.springframework.stereotype.Component;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestParam;
@Component
@FeignClient (value =  "service-b" //這里的name對應調用服務的spring.applicatoin.name
public  interface  ServiceAFeignClient {
     @RequestMapping (value =  "/hi" )
     String hi( @RequestParam ( "id" ) String id);
}

4、在客戶端系統中寫一個測試controller

1
2
3
4
5
6
7
8
9
10
@RestController
public  class  TestController {
     @Autowired
     ServiceAFeignClient serviceAFeignClient;
     
     @RequestMapping ( "/hi" )
     public  String hi( @RequestParam  String id){
         return  serviceAFeignClient.hi(id);
     }
}

5、在服務端系統中提供一個hi()方法,供客戶端調用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@EnableEurekaClient
@RestController
@SpringBootApplication
public  class  ServiceBApplication {
     public  static  void  main(String[] args) {
         SpringApplication.run(ServiceBApplication. class , args);
     }
     @Value ( "${spring.application.name}" )
     private  String name;
     @Value ( "${server.port}" )
     private  String port;
     @RequestMapping ( "/hi" )
     public  String hi( @RequestParam  String id){
         return  "hi, "  + id +  ", "  + name +  ":"  + port;
     }
}

6、新建一個服務service-b-2,代碼和service-b完全一致,只需改端口號(服務名也叫service-b,spring.application.name=service-b

在eclipse中可以不新建工程,當啟動了service-b后,改service-b的端口號,再啟動一次,這樣就啟動了兩個service-b服務,區別是端口號不同。

啟動后會在Eureka注冊中心中看到service-b有兩個,一個端口號為8081,一個為8082:

DS Replicas

Instances currently registered with Eureka

Application AMIs Availability Zones Status
SERVICE-A n/a (1) (1) UP (1) - SZVY2AWX5511361.china.huawei.com:service-a:8083
SERVICE-B n/a (2) (2)

UP (2) - SZVY2AWX5511361.china.huawei.com:service-b:8081 ,

 SZVY2AWX5511361.china.huawei.com:service-b:8082

7、查看效果

訪問:http://localhost:8083/hi?id=123

返回:hi, 123, service-b:8082

刷新:hi, 123, service-b:8081

刷新:hi, 123, service-b:8082

刷新:hi, 123, service-b:8081

因為Ribbon負債均衡默認使用的是輪詢機制。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM