SpringCloud Ribbon是一个基于HTTP与TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。
客户端负载均衡:在客户端负载均衡中,所有的客户端节点都有一份自己要访问的服务端清单,这些清单统统都是从Eureka服务注册中心获取的。在Spring Cloud中我们如果想要使用客户端负载均衡,方法很简单,开启@LoadBalanced注解即可,这样客户端在发起请求的时候会先自行选择一个服务端,向该服务端发起请求,从而实现负载均衡。
使用SpringCloud Eureka创建服务提供者和消费者,使用SpringCloud Ribbon实现客户端负载均衡。
(Eureka沿用上一篇的项目,首先启动两个Eureka注册中心)
首先给出项目结构
1 更新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>yunlingfly</groupId> <artifactId>mavenspringcloudclient</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mavenspringcloudclient</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>Dalston.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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>
2 更新application.yml
eureka: enabled: true client: registerWithEureka: true fetchRegistry: true serviceUrl: # 如果是本机测试将xxx换成localhost即可,服务器则填写IP地址 defaultZone: http://xxx.xxx.xxx.xxx:8761/eureka/,http://xxx.xxx.xxx.xxx:8762/eureka/ # 如果服务提供者不是运行在本机的话需要加上下面的注释,否则注册中心无法通过ip查找服务(从注册中心的实例名称也能看出来) # instance: # preferIpAddress: true # instance-id: ${spring.cloud.client.ipAddress}:${server.port} # hostname: ${spring.cloud.client.ipAddress} # ipAddress: 120.79.66.240 server: port: 8763 info: name: 服务提供者-8763 version: 0.0.1 spring: application: name: eureka-client
3 编写启动类
package yunlingfly.mavenspringcloudclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; // 启动类 @SpringBootApplication // Eureka服务提供者 @EnableDiscoveryClient public class MavenspringcloudclientApplication { public static void main(String[] args) { SpringApplication.run(MavenspringcloudclientApplication.class, args); } }
4 编写服务提供者的Controller层
package yunlingfly.mavenspringcloudclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class ServiceInstanceRestController { @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "hi " + name + ",i am from port:" + port; } }
请另外再新建一个和这个服务提供者一样的项目,只需要改动application.yml的server.port属性即可例如server.port=8765(不要与前面的重复)
首先给出项目结构
1 更新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>yunlingfly</groupId> <artifactId>mavenspringcloudribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mavenspringcloudribbon</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>Dalston.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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</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>
2 更新application.yml
eureka: client: serviceUrl: defaultZone: http://xxx.xxx.xxx.xxx:8761/eureka/,http://xxx.xxx.xxx.xxx:8762/eureka/ server: port: 8764 spring: application: name: service-ribbon info: app: name: 一个消费者 version: 0.0.1
3 编写启动类
package yunlingfly.mavenspringcloudribbon; 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.client.RestTemplate; // 启动类 @SpringBootApplication // 向服务中心注册 @EnableDiscoveryClient public class MavenspringcloudribbonApplication { public static void main(String[] args) { SpringApplication.run(MavenspringcloudribbonApplication.class, args); } @Bean // @LoadBalanced注解表明这个restRemplate开启负载均衡的功能 @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
4 编写服务消费者的Controller层
package yunlingfly.mavenspringcloudribbon.controller; 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; import org.springframework.web.client.RestTemplate; @RestController public class HelloControler { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/hi") public String hi(@RequestParam String name){ return restTemplate.getForObject("http://eureka-client/hi?name="+name,String.class); } }
首先启动服务提供者*2,然后启动服务消费者,然后浏览器输入http://localhost:8764/hi?name=芸灵fly即可看到效果(服务由8763与8765端口的服务交替提供)
查看Eureka发现Eureka-client的服务有两个
注意事项:使用@LoadBalanced为RestTemplate开启负载均衡;测试时如果服务提供者与服务消费者不在同一台机器上,需要加上eureka.instance的各种属性配置,具体查看上文application.yml的注释
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。