The previous one Service registration and discovery , friends who haven't read it first read the previous one, which is based on the previous one.
Ribbon introduction
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.
<!-- more -->
Ribbon is a load balancing client, which can well control the behavior of HTTP and TCP clients. Feign has integrated the ribbon.
Spring Cloud Netflix provides the following beans for ribbon (BeanType beanName: ClassName) by default:
-
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
-
IRule ribbonRule: ZoneAvoidanceRule
-
IPing ribbonPing: NoOpPing
-
ServerList<Server> ribbonServerList: ConfigurationBasedServerList
-
ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter
-
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
Preparation
Based on the previous article, this article starts the Eureka server project, starts the Eureka client project, with the port of 8040, changes the profile port of Eureka client to 8041, and starts it. In the same project, modify the port number, and start multiple instances. Just check Allow parallel run in the IDEA:
At this point, you will find that two Eureka client instances are registered in the registration service, which is equivalent to a cluster of two nodes. Visit http://localhost:9090:
Create a new service consumer
Use Spring Initializr to create a new project, named Ribbon service. Check Eureka Discovery Client in Spring Cloud Discovery, Ribbon in Spring Cloud Routing, and Spring Web in the Web:
After creation, the project pom.xml 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 https://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.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.noodles.mars</groupId> <artifactId>ribbon-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ribbon-service</name> <description>Ribbon Service</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <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> </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>${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>
The ribbon service configures the service center address, application name and port. The content of the configuration file is as follows:
server: port: 8050 spring: application: name: ribbon-service eureka: client: service-url: defaultZone: http://localhost:9090/eureka/
Annotate @ EnableDiscoveryClient on the project startup class, enable registration with the service center, define a RestTemplate Bean, and annotate @ LoadBalanced to enable the load balancing function:
@EnableEurekaClient @SpringBootApplication public class RibbonServiceApplication { public static void main(String[] args) { SpringApplication.run(RibbonServiceApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
Write a controller to consume the / hello interface of Eureka client service through the RestTemplate defined previously. The application name is used in the url. ribbon will select a specific service instance according to the application name, and replace the service name with a specific url when requesting according to the service instance:
@RestController public class HelloController { private final RestTemplate restTemplate; @Autowired public HelloController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @GetMapping("/hello") public String hello(@RequestParam("name") String name) { return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class); } }
Multiple access on Browser http://localhost:8050/hello?name=Mars :
Hello, My name is Mars, I'm from port: 8040 Hello, My name is Mars, I'm from port: 8041
This means that the load balancer is working.
Pay attention to the public address: JAVA half past nine class, technical articles daily service!
Personal blog: http://blog.hxpgxt.cn