Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the three ways to use SpringCloud's Ribbon+RestTemplate?

Shulou Source: shulou.com Published: 2022-06-02 00:18:46 11月03日 Update

What are the three ways of using SpringCloud's Ribbon+RestTemplate? many beginners are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Method 1: instantiate RestTemplate objects directly using new

@ GetMapping ("getUserList")

Public List getUserList () {

RestTemplate template = new RestTemplate ()

Return template.getForObject (

"http://localhost:8080/getUserList",//

List.class)

}

Disadvantages:

1. Url hard coding. If the ip changes, it needs to be changed in the code.

2. If client is a cluster and has multiple url, this method can only be equipped with one url; and cannot use cluster mode.

Method 2: inject LoadBalancerClient, obtain one of the instances of the application whose name is providerServcidName (Note: service provider name), obtain url, and then use RestTemplate to obtain data to achieve load balancing.

@ RestController

Public class UserController {

@ Autowired

Private LoadBalancerClient loadBalancerClient

@ GetMapping ("getUserList")

Public List getUserList () {

RestTemplate template = new RestTemplate ()

/ / Select a service instance, based on the passed service name serviceId

/ / choose a corresponding service instance from the load balancer.

ServiceInstance instance = loadBalancerClient

.choose ("providerServcidName")

String url = String.format ("http://%s:%s",

Instance.getHost ()

Instance.getPort () + "/ getUserList")

Return template.getForObject (url, List.class)

}

}

Method 3: RestTemplate is used by configuring the injection Spring container.

Import org.springframework.cloud.client.loadbalancer.LoadBalanced

Import org.springframework.context.annotation.Bean

Import org.springframework.context.annotation.Configuration

Import org.springframework.web.client.RestTemplate

@ Configuration

Public class RestTemplateConfig {

@ Bean

@ LoadBalanced

Public RestTemplate restTemplate () {

Return new RestTemplate ()

}

}

Inject the RestTemplate object into controller and call the getForObject method directly. Note that the application name is written directly in url, not ip:port.

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 UserController {

@ Autowired

Private RestTemplate restTemplate

@ GetMapping ("getUserList")

Public String getUserList () {

Return restTemplate

.getForObject ("http://providerServcidName/getUserList", List.class)

}

}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Mode instance service name application equilibrium object method cluster help clarity code content remarks multiple containers for this provider data article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux Shulou Technology Shulou Tech Info Xiaomi