RestTemplate+Ribbon

Ribbon can be used alone, but it is easier to use Ribbon in Spring Cloud. Because Spring Cloud has a layer of encapsulation based on Ribbon, many configurations have been integrated. This article will use Ribbon in Spring Cloud.

1. What is RestTemplate?

RestTemplate is a client provided by Spring for accessing Rest services. RestTemplate provides a variety of convenient methods for accessing remote HTTP services, which can greatly improve the writing efficiency of the client.

二、RestTemplate+Ribbon

1.Create project

 Here I use Ribbon and Eureka together. If you don’t know how, you can read my previous article. First, we create three modules, two of which are a cluster, and the remaining modules are responsible for calling the interfaces in the cluster.

When creating the three projects, Eureka-client and SpringWeb dependencies were selected. Note that the created project must correspond to the SpringBoot and SpringCloud version numbers.

Let's first write the configuration files of the two modules in the visited cluster. Pay attention to the server port number and the address registered by eureka.

 These are two modules in a cluster. Since they are in a cluster, the instance names must be consistent .

After writing, we will write an interface in each of the two modules so that we can simply test it later.

The first module in the cluster:

package com.example.provider01.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @GetMapping("hello")
    public String hello(){
        return "我是提供者aaa的接口";
    }
}

The second module in the cluster:

package com.example.provider02.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @GetMapping("/hello")
    public String hello(){
        return "我是提供者bbb的接口";
    }
}

In this way, the modules in the cluster are roughly completed, and then we will write and call the module of the cluster. This is the configuration file, much the same. 

 This is the module that accesses the cluster. After creating the three modules, the @EnableEurekaClient annotation must be added to the Application running file , and it needs to be registered in Eureka. In this way, the preparation steps are roughly completed.

2. Combine

Before integration, we need to import the Ribbon dependency in the third module pom.xml, because it is responsible for calling the interface in the cluster.

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

Then we find the Application running file in the third module and inject the RestTemplate object into it .

package com.example.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {

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

    @Bean
    @LoadBalanced //被ribbon来操作
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

Here you need to add a @LoadBalanced annotation. The main logic of this annotation is to add an interceptor to the RestTemplate, replace the requested address before the request, or select the service address according to the specific load policy, and then call it. This is @ The principle of LoadBalanced annotation.

Then we write an interface in this module. First inject RestTemplate.

package com.example.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("testRibbon")
    public String Ribbon(String serviceName){
        String result = restTemplate.getForObject("http://"+serviceName+"/hello",String.class);
        return result;
    }
}

The serviceName here is the instance name of one of the two modules in the cluster.

Then we access the third module.

 

Click the refresh button. 

 

 

 

You can see that both interfaces are successfully called, and the order is changed with one click. 


Summarize

This is RestTeplate combined with Ribbon. Again, it is very simple as long as you clarify your ideas! If you like it, don’t forget to like + follow! ! ! ! ! Thanks for watching! ! ! ! ! ! ! ! !

Guess you like

Origin blog.csdn.net/m0_66403070/article/details/130524309