SpringCloud(五)调用Eureka中的服务之Ribbon
文章内索引
[显示]
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign,feign默认集成了ribbon。
ribbon
ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。
Ribbon工作时分为两步:第一步先选择Eureka Server,它优先选择在同一个Zone且负载较少的Server;第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略。例如轮询round robin、随机Random、根据响应时间加权等。
Simple
Ribbon需要注册至eureka服务中心,通过eureka服务发现获取服务列表,ribbion负载均衡通过rule选择要调用的服务,所以工程需要引入eureka discovery与ribbon。
在http://start.spring.io/中,输入eureka discovery与ribbon快速生成eureka服务调用项目。
添加@EnableDiscoveryClient注解,启动springboot项目(此项目与其他Restful风格的springboot项目一致,只需要添加@EnableDiscoveryClien即可注册至服务中心)
pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?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.hanson</groupId> <artifactId>Eureka-Consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Eureka-Consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.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> <spring-cloud.version>Edgware.SR2</spring-cloud.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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> </project> |
配置
1 2 3 |
spring.application.name=eureka-consumer server.port=8080 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ |
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.hanson.ribbon; import org.springframework.beans.factory.annotation.Autowired; 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.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient @RestController public class RibbonApplication { /** * 注入声明的 */ @Autowired RestTemplate client; public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } /** * 声明一个RestTemplate的bean。 * 通过@LoadBalanced注解,表明这个restRemplate开启负载均衡的功能。 * @return */ @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } @RequestMapping("/") public String helloWorld() { //hanson-service 为服务提供者的配置的spring.application.name String forObject = client.getForObject("http://hanson-service/", String.class); System.out.println(forObject); return forObject; } } |
如图所示,hanson-service服务分别启动在8001,8002,8003端口。
通过访问调用端,会发现此时ribbion通过轮询的方式,分别访问这三个服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
你好,这是你要的XX商品,由8001为您提供服务。 你好,这是你要的XX商品,由8003为您提供服务。 你好,这是你要的XX商品,由8002为您提供服务。 你好,这是你要的XX商品,由8001为您提供服务。 你好,这是你要的XX商品,由8003为您提供服务。 你好,这是你要的XX商品,由8002为您提供服务。 你好,这是你要的XX商品,由8001为您提供服务。 你好,这是你要的XX商品,由8003为您提供服务。 你好,这是你要的XX商品,由8002为您提供服务。 你好,这是你要的XX商品,由8001为您提供服务。 你好,这是你要的XX商品,由8003为您提供服务。 你好,这是你要的XX商品,由8002为您提供服务。 你好,这是你要的XX商品,由8001为您提供服务。 你好,这是你要的XX商品,由8003为您提供服务。 你好,这是你要的XX商品,由8002为您提供服务。 |
此时服务的架构为(盗图..)
©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!
发表评论