Spring Cloud learning -- load balancing ribbon

The last one summarizes Eureka , and created a client provider. This time, summarize the ribbon to achieve micro service load balancing.

Introduction to Spring Cloud Ribbon

A client load balancer that provides access control to a large number of HTTP and TCP clients.

Client load balancing means that when the browser sends a request to the background, the client will read and register to Eureka Server The server According to the set load balancing policy (default if not set), choose which server to send the request to.

Difference between load balancing server and client

Server side

Generally, nginx is used to achieve load balancing, as shown in the following figure

nginx proxy server, and then select the available server to send the request.

Client

That's our Ribbon, as shown below

Ribbon load balancing implementation

Construction of Ribbon subproject

Next, we created a sub project consumer ribbon

pom file

Everything else is the same. Bring in the ribbon

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

yml configuration

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka01:8800/eureka/,http://eureka02:8810/eureka/
server:
  tomcat:
    uri-encoding: UTF-8
  port: 10001
#  servlet:
#    context-path: /gbq_consumer_ribbon
spring:
  application:
    name: consumer_ribbon

Note: do not configure context path or server path, so that when your microservice is called, there will be an error that cannot be found! If you have to add it, please Baidu yourself, what I found is not very easy to use!

Startup class

@SpringBootApplication
//@EnableDiscoveryClient / / starting from Spring Cloud Edgware, @ EnableDiscoveryClient or @ EnableEurekaClient can be omitted. You can register the microservice to the service discovery component by adding related dependencies and configuring them accordingly.
public class ConsumerRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerRibbonApplication.class, args);
    }


    //Enable restTemplate load balancing
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * To configure the random load policy, you need to configure the property service-B.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
     */
    @Bean
    public IRule ribbonRule() {
        return new RandomRule();
    }


}

Controller

@RestController
public class HiController {

    @Autowired
    private HiService hiService;

    @GetMapping("/hi")
    public String sayHello(String name){
        return hiService.sayHello(name);
    }
}

Service implementation class

@Service
public class HiServiceImpl implements HiService {

    
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String sayHello(String name) {
        String url = "http://provider/hello?name="+name; 
        return restTemplate.getForObject(url,String.class);
    }
}

So the ribbon is finished.

Then the ribbon needs to call the hello interface of the provider service. We need to write such an interface in the provider project!

provider subproject interface implementation

controller

@RestController
public class HelloController {

    @Value("${server.port}")
    private int port;


    @GetMapping("/hello")
    public String sayHello(String name){
        return "Hello, cloud"+"name: "+name+"port:"+port;
    }
}

If you want to achieve load balancing, you need a provider service as a reference, and the result will be obvious

Construction of provider copy subproject

Copy a provider directly, and modify the name and yml configuration

yml configuration

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka01:8800/eureka/,http://eureka02:8810/eureka/
  instance:
    prefer-ip-address:  true  #Register your ip address with Eureka service
server:
  tomcat:
    uri-encoding: UTF-8
  port: 9902  #Just change the port number
#  servlet:
#    context-path: /gbq_provider
spring:
  application:
    name: provider  #Name needs to be the same

Note: just modify the port

Everything else is the same.

start-up

Let's take a look at the effect of load balancing. Here take the postman test!

Port number, see the change!!!

Join us

If necessary, welcome to join our QQ group! (QQ search 1074281704, join our QQ group!)
If you have any questions, you can also join our QQ group. Welcome to join us!

Published 26 original articles, won praise 14, visited 4847
Private letter follow

Tags: Spring Nginx Tomcat encoding

Posted on Sun, 15 Mar 2020 02:18:02 -0400 by rifts