SpringCloud | 服务消费者 ribbon (四)
2017-09-04 00:00
691 查看
简介
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法 , Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。springcloud提供了两种服务消费的方案,一种是ribbon 另外一种是 Feign ( 默认集成了ribbon ),让我们先看看ribbon
实践
pom.xml<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>service-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-ribbon</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon
启动类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /* * @EnableDiscoveryClient向服务中心注册 * 并且向程序的ioc注入一个bean: restTemplate; * 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。 * */ @SpringBootApplication @EnableDiscoveryClient public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
service
package com.example.demo.service; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * Created by zph Date:2017/8/18. */ @Service public class HelloService { @Autowired RestTemplate restTemplate; public String hiService(String name) { return restTemplate.getForObject("http://servicehi/hi?name="+name,String.class); } }
controler
package com.example.demo.controler; import com.example.demo.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * Created by zph Date:2017/8/18. */ @RestController public class HelloControler { @Autowired HelloService helloService; @RequestMapping(value = "/hi") public String hi(@RequestParam String name){ return helloService.hiService(name); } }
启动ServiceRibbonApplication 中main方法,连续访问 http://localhost:8764/hi?name=test
发现 hi test,i am from port:8762 hi test,i am from port:8763 在不断切换,由此可知,rpc负载均衡调用实现。
相关文章推荐
- springcloud实战之5 服务消费者(ribbon)
- Spring Cloud(服务的消费者 ribbon+restTemplate)
- Spring Cloud(三)服务提供者 Eureka + 服务消费者(rest + Ribbon)
- (1-2)SpringCloud:服务的消费者rest+ribbon
- 一起来学SpringCloud之 - 服务消费者(Ribbon)
- SpringCloud教程| 第二篇:服务消费者(rest+ribbon)
- Spring Cloud学习:02服务消费者(Ribbon&Feign)
- 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)
- SpringCloud 笔记 (二)---- 简单搭建一个服务消费者,实现简单的ribbon负载均衡
- SpringCloud入门2:服务消费者(rest+ribbon)
- SpringCloud学习教程二( 服务消费者rest+ribbon )
- SpringCloud教程二:服务消费者(rest+ribbon)
- SpringCloud教程 | 三.服务消费者(Ribbon)
- [bigdata-109] spring-cloud-05 分布式服务的ribbon消费者
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)
- SpringCloud教程第二篇:服务消费者(rest+ribbon)
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)
- 玩转SpringCloud(F版本) 二.服务消费者(1)ribbon+restTemplate
- SpringCloud: 服务消费者(rest+ribbon)
- SpringCloud 学习 | 第三篇: 服务消费者 Ribbon