spring-cloud配置ribbon负载均衡
ribbon提供的负载均衡就是开箱即用的,简单的不能再简单了
- 为了顺利演示此demo,你需要如下
需要提前配置eureka服务端,具体看 https://www.cnblogs.com/ye-hcj/p/10292944.html
需要提前配置eureka客户端,具体看 https://www.cnblogs.com/ye-hcj/p/10293048.html
目录结构
依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</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
package test1.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import test1.test.Config.TestConfig;
@RibbonClient(name = "stores", configuration = TestConfig.class)
@EnableEurekaClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
配置
spring:
application:
name: eureka-ribbon-clustered
server:
port: 7000
# -- Configure for Ribbon:
stores:
ribbon:
eureka:
enabled: false # Disable Default Ping
listOfServers: localhost:8000,localhost:8001,localhost:8002,,localhost:8003
ServerListRefreshInterval: 15000
# -- Configure Discovery Client (Eureka Client).
eureka:
instance:
appname: ribbion-service
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka1:9001/eureka
Config
package test1.test.Config;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
public class TestConfig {
@Autowired
private IClientConfig ribbonClientConfig;
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
@Bean
public IRule ribbonRule(IClientConfig config) {
return new WeightedResponseTimeRule();
}
}
Controller
package test1.test.Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private LoadBalancerClient loadBalancer;
@ResponseBody
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "<a href='testCallAbcService'>/testCallAbcService</a>";
}
@ResponseBody
@RequestMapping(value = "/testCallAbcService", method = RequestMethod.GET)
public String showFirstService() {
String serviceId = "EUREKA-CLIENT-CLUSTERED".toLowerCase();
// (Need!!) eureka.client.fetchRegistry=true
List<ServiceInstance> instances = this.discoveryClient.getInstances(serviceId);
if (instances == null || instances.isEmpty()) {
return "No instances for service: " + serviceId;
}
String html = "<h2>Instances for Service Id: " + serviceId + "</h2>";
for (ServiceInstance serviceInstance : instances) {
html += "<h3>Instance :" + serviceInstance.getUri() + "</h3>";
}
// Create a RestTemplate.
RestTemplate restTemplate = new RestTemplate();
html += "<br><h4>Call /hello of service: " + serviceId + "</h4>";
try {
// May be throw IllegalStateException (No instances available)
ServiceInstance serviceInstance = this.loadBalancer.choose(serviceId);
html += "<br>===> Load Balancer choose: " + serviceInstance.getUri();
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/loadBalancer";
html += "<br>Make a Call: " + url;
html += "<br>";
String result = restTemplate.getForObject(url, String.class);
html += "<br>Result: " + result;
} catch (IllegalStateException e) {
html += "<br>loadBalancer.choose ERROR: " + e.getMessage();
e.printStackTrace();
} catch (Exception e) {
html += "<br>Other ERROR: " + e.getMessage();
e.printStackTrace();
}
return html;
}
}
运行
先启动eureka服务端和客户端,再用spring-boot运行此程序
然后访问,即可看到其他同时注册的程序
http://localhost:7000
ribbon的负载均衡是它自己完成的,开箱即用