Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report