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

If the service consumer RestTemplate+Ribbon is implemented in Spring Cloud

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

Share

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

Editor to share with you if the implementation of service consumer RestTemplate+Ribbon in Spring Cloud, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In the previous article, I talked about the registration and discovery of services. In the micro-service architecture, businesses are split into separate services, and the communication between services and services is based on http restful. Spring cloud has two ways to invoke services, one is ribbon+restTemplate, the other is feign. In this article, we will first explain how to use ribbon+rest.

A brief introduction to ribbon

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @ FeignClient then this section also applies.

-- extracted from the official website

Ribbon is a load balancing client, which can well control some behaviors of htt and tcp. Feign integrates ribbon by default.

Ribbon has implemented these configuration bean by default:

IClientConfig ribbonClientConfig: DefaultClientConfigImpl

IRule ribbonRule: ZoneAvoidanceRule

IPing ribbonPing: NoOpPing

ServerList ribbonServerList: ConfigurationBasedServerList

ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

II. Preparatory work

This article is based on the project of the previous article, start the eureka-server project; start the service-hi project, its port is 8762; change the port of the service-hi configuration file to 8763, and start it, then you will find that service-hi has registered 2 instances in eureka-server, which is equivalent to a small cluster. Access localhost:8761 as shown in the figure:

Third, build a service consumer

Create a new spring-boot project named: service-ribbon; introduces startup dependencies spring-cloud-starter-eureka, spring-cloud-starter-ribbon and spring-boot-starter-web into its pom.xml file as follows:

Org.springframework.cloudspring-cloud-starter-eurekaorg.springframework.cloudspring-cloud-starter-ribbonorg.springframework.bootspring-boot-starter-web

The configuration file of the project specifies that the registry address of the service is http://localhost:8761/eureka/, the program name is service-ribbon, and the program port is 8764. The configuration file application.yml is as follows:

Eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8764spring: application: name: service-ribbon

In the startup class of the project, register with the service center through @ EnableDiscoveryClient; and inject a bean: restTemplate; into the ioc of the program and use the @ LoadBalanced annotation to indicate that the restRemplate enables the load balancing function.

SpringBootApplication@EnableDiscoveryClientpublic class ServiceRibbonApplication {@ Bean @ LoadBalanced RestTemplate restTemplate () {return new RestTemplate ();}}

Write a test class HelloService, and consume the "/ hi" interface of the ioc service through the restTemplate injected into the service-hi container. Here, we directly replace the specific url address with the program name. In ribbon, it will select the specific service instance according to the service name, and replace the service name with the specific url when the service instance is requested, as follows:

@ Servicepublic class HelloService {@ Autowired RestTemplate restTemplate; public String hiService (String name) {return restTemplate.getForObject ("http://SERVICE-HI/hi?name="+name,String.class);}})

Write a controller, using the method that calls HelloService in controller, as follows:

RestControllerpublic class HelloControler {@ Autowired HelloService helloService; @ RequestMapping (value = "/ hi") public String hi (@ RequestParam String name) {return helloService.hiService (name);}}

Visit http://localhost:8764/hi?name=forezp multiple times on the browser, and the browser alternately displays:

Hi forezp,i am from port:8762

Hi forezp,i am from port:8763

This means that when we call the restTemplate.getForObject ("http://SERVICE-HI/hi?name="+name,String.class) method), we have already done load balancing and accessed service instances on different ports.

IV. The structure at this time

One service registry, eureka server, port 8761

Two instances of service-hi project are registered with the service registry with port 8762j8763 respectively.

Sercvice-ribbon port is 8764, registered with the service registry

When sercvice-ribbon calls the hi interface of service-hi through restTemplate, because ribbon is used for load balancing, it will take turns to call the hi interface of service-hi:8762 and 8763 ports.

The above is all the content of this article "if you implement service consumer RestTemplate+Ribbon in Spring Cloud", thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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