一、ribbon简介
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
- Excerpt from the official website
ribbon is a client load balancing, you can be well controlled some of the behavior of htt and tcp. Feign default integrated ribbon.
ribbon has achieved these default configuration bean:
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList ribbonServerList: ConfigurationBasedServerList
ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
Second, the preparation of
this article based on an article of the project, starting eureka-server project; project start service-hi, the port is 8762; the port configuration file service-hi changed to 8763, and started, then you will find: service-hi registered two instances eureka-server, which is equivalent to a small cluster. Access localhost: 8761 as shown:
Third, build a service consumer
to re-create a spring-boot project, named: service-ribbon;
in its initial introduction pom.xml files are dependent on spring-cloud-starter-eureka, spring-cloud-starter-ribbon, spring-boot-starter-web, code is as follows:
<?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.forezp</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.2.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>
In the address registration center of the project configuration file specifies services to http: // localhost: 8761 / eureka /, program name for the service-ribbon, the program for the 8764 port. Profile application.yml as follows:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
In the startup class project, the registration by @EnableDiscoveryClient to the service center; and to inject a bean ioc program: restTemplate; and by @LoadBalanced annotation indicates that this restRemplate open load balancing capabilities.
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Write a test class HelloService, to consume service-hi services by injecting restTemplate ioc containers before "/ hi" interfaces, where we direct replacement for specific url address with the name of the program, it will be based on the service name in the ribbon selection of a particular service instance, according to the service instance when the replacement request of a specific service name url out, as follows:
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
}
A write controller, the call controller used HelloService method, as follows:
/**
* Created by fangzhipeng on 2017/4/6.
*/
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
Visit http many times in the browser: // localhost:? 8764 / hi name = forezp, browser alternates:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
This shows that when we by calling restTemplate.getForObject ( " HTTP: // the SERVICE-HI / hi name =?" + Name, String.class ) when the method has been done load balancing, access to different services, for instance ports. spring cloud b2b2c e-commerce social platform source code, please add penguin beg: three four five three six II qi II fifty-nine