版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shenzhen_zsw/article/details/89173519
高可用ribbon负载均衡
Ribbon说明
ribbon 是一个客户端负载均衡器,可以简单的理解成类似于 nginx的负载均衡模块的功能。
创ribbon测试工程
ribbontest工程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">
<parent>
<artifactId>youfanshop</artifactId>
<groupId>com.youfan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ribbonTest</artifactId>
<packaging>pom</packaging>
<modules>
<module>ribboServer1</module>
<module>ribboServer2</module>
<module>ribbocostumer</module>
</modules>
</project>
创建工程ribbonServer1
扫描二维码关注公众号,回复:
5878113 查看本文章
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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ribboServer1</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</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>
application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/youfanshop?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=7071
spring.application.name=cloud-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.appname=cloud-service
说明:
1)这里端口号是7071;
提供服务getMyBlogNameApi
package com.youfan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/ribbon")
public class RibbonTestApi {
/**
* 获取博客名称API
*
* @return 相关信息
*/
@RequestMapping(value = "name", method = RequestMethod.GET)
public String getMyBlogNameApi() {
return "该服务器端口号:7071";
}
}
说明:
1)这里返回端口7071,用于识别;
启动类ServiceTwoApplication
package com.youfan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceTwoApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceTwoApplication.class, args);
}
}
创建工程ribbonServer2
说明:
1)此工程和 工程基本一样,主要需要修改端口号,用于做区别;
2)修改RibbonTestApi文件;
3)修改application.properties文件;
创建服务消费者工程
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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ribbocostumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</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>
application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/youfanshop?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=888888
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=7073
spring.application.name=ribboclient1
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.appname=ribbon-client
TestRibbonApi
package com.youfan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping(value = "/test")
public class TestRibbonApi {
/**
* 注入RestTemplate
*/
@Autowired
RestTemplate restTemplate;
// http://localhost:7073/test/blog/name
@RequestMapping(value = "/blog/name" ,method = RequestMethod.GET)
public String testGetNameOfBlog(){
String url="http://CLOUD-SERVICE/ribbon/name";
return restTemplate.getForObject(url,String.class);
}
}
说明:
1)这里的CLOUD-SERVICE是前面2个工程提供服务的名称;
2)通过这个地址可以访问到对应的服务,通过轮训的方式;
启动类RibbonServiceApplication
package com.youfan;
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 RibbonServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonServiceApplication.class, args);
}
/**
* Spring提供的用于访问Rest服务的客户端
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
说明:
1)这里需要加上LoadBalanced注解支持负责均衡策略;
测试
1)启动上面三个工程;
2)当然Eureka也需要提前启动;
3)测试访问
http://localhost:7073/test/blog/name
说明:
1)多次访问,会有不同的结果;
2)说明负载均衡的策略已经实现;