springcloud load balancing learning 6--

Learn from the station b springcloud, now summarize the summary removal of a small error appearing in the video, some of the error-prone places were reminded
b outbound links: https://www.bilibili.com/video/av55304977

data link:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
extraction code: 21ru

On a link: https://blog.csdn.net/qq_40893824/article/details/103332091
next section link: https://blog.csdn.net/qq_40893824/article/details/103336449

The following list summarizes: sub-project → pom → application → Student → handler → entity classes classes start

Implementation details:
1. Create a module module, named Ribbon
2. The module's POM file, add the code:

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>

3. Chong application.yml in resource, add code:

server:
  port: 8040
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    #确认注册

4. In java package record com.southwind, creating class RibbonApplication.java in southwind start, fill in the code: @LoadBalanced not leak!

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

    @Bean
    @LoadBalanced//负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

5. In the record southwind package entity, will be copied to the Student the packet
6. In the record southwind packet controller, creating RibbonHandler.java, add code:

package com.southwind.controller;

import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

@RestController
@RequestMapping("/ribbon")
public class RibbonHandler {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return restTemplate.getForObject("http://provider/student/findAll",Collection.class);
    }

    @GetMapping("/index")
    public String index(){
        return restTemplate.getForObject("http://provider/student/index",String.class);
    }

}

7. Check transferred through
a enter HTTP:. // localhost: 8761
Here Insert Picture Description
b enter http:. // localhost: 8040 / ribbon / index
Here Insert Picture Description
port is 8010, f5 to refresh the page:
Here Insert Picture Description
this port has been converted between 8010 and 8011, it is realized the function of load balancing.

Here I do not have to write code additions and deletions to change search, because it is simple to add, copy before the additions and deletions to change the code to check up on it.

zuul and ribbon are load balancing, as springcloud white, I think zuul currently operating simpler than the ribbon.

On a link: https://blog.csdn.net/qq_40893824/article/details/103332091
next section link: https://blog.csdn.net/qq_40893824/article/details/103336449

Published 42 original articles · won praise 2 · Views 1189

Guess you like

Origin blog.csdn.net/qq_40893824/article/details/103332281