前面我們已經完成了註冊中心和服務提供者兩個基礎組件。本文就介紹使用Spring Cloud Ribbon在客戶端負載均衡的調用服務。
對於大型應用系統負載均衡(LB:Load Balancing)是首要被解決一個問題。在微服務之前LB方案主要是集中式負載均衡方案,在服務消費者和服務提供者之間又一個獨立的LB,LB通常是專門的硬件,如F5,或者是基於軟件的,如VS、HAproxy等。 LB上有所有服務的地址映射表,當服務消費者調用某個目標服務時,它先向LB發起請求,由LB以某種策略(比如:Round-Robin)做負載均衡後將請求轉發到目標服務。
而微服務的出現,則為LB的實現提供了另外一種思路:把LB的功能以庫的方式集成到服務消費方的進程內,而不是由一個集中的設備或服務器提供。這種方案稱為軟負載均衡(Soft Load Balancing)或者客戶端負載均衡。在Spring Cloud中配合Eureka的服務註冊功能,Ribbon子項目則為REST客戶端實現了負載均衡。
使用Spring Cloud Ribbon實現服務消費者
新建spring-cloud-sample-tutorial-consumer項目
在spring-cloud-sample-tutorial下新建spring-cloud-sample-tutorial-consumer子項目
添加ribbon和eureka依賴
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>
配置applicatioin.properties,註冊中心地址
spring.application.name=consumerserver.port=30001eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
編寫UserController,添加@LoadBalanced註解,啟用Ribbon負載均衡
@Controller@RequestMapping("user")public class UserController { @Bean @LoadBalanced RestTemplate initRestTemplate(){ return new RestTemplate(); } @Autowired private RestTemplate restTemplate; @RequestMapping("add") @ResponseBody public String add(String userName, String age){ return restTemplate.getForEntity("http://PRODUCER/user/add",String.class,userName,age).getBody(); }}編寫ConsumerApplication,添加@EnableEurekaClient,啟用服務註冊
@EnableEurekaClient@SpringBootApplicationpublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); }}集群部署Producer
為了模擬集群的Producer,在producer項目中新建application-profile1.properties和application-profile2.properties。
application-profile1.properties
spring.application.name=producerserver.port=20001eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
application-profile2.properties
spring.application.name=producerserver.port=20002eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
為了測試負載的效果,我們把被調用服務的端口打出來
@Controller@RequestMapping("user")public class UserController { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private DiscoveryClient client; @RequestMapping("add") @ResponseBody public String addUser(String userName, String age) { return "Success from " + client.getLocalServiceInstance().getHost() + ":" + client.getLocalServiceInstance().getPort() ; }}啟動測試
啟動註冊中心
分別配置Active Profiles為profile1和profile2,啟動兩次,完成註冊中心集群的服務啟動。
啟動服務提供者
按上面同樣的方法,啟動服務提供者。
啟動服務消費者
服務提供者單機就可以了,正常啟動ConsumerApplication即可。
驗證
從瀏覽器輸入http://localhost:30001/user/add
再次訪問:
可以看到,我們的負載均衡調用服務,已經成功了,默認是按輪訓的方式做負載均衡。
總結
本文介紹並完成了使用Spring Cloud Ribbon進行客戶端負載均衡的調用。
接下來我們繼續介紹怎麼使用Spring Cloud進行服務監控。
源碼下載
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。