In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use ribbon+rest". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use ribbon+rest.
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.
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
Start the eureka-server project; start the service-hi project, its port is 8762; service-hi
The port of the configuration file is changed to 8763 and started, to understand the springcloud architecture, you can add request: 3536247259, then you will find:
Service-hi registers two instances with eureka-server, which is equivalent to a small cluster.
Third, build a service consumer
Create a new spring-boot project named service-ribbon
Its pom.xml inherits the parent pom file and introduces the following dependencies:
4.0.0 com.forezp service-ribbon 0.0.1-SNAPSHOT jar service-ribbon Demo project for Spring Boot com.forezp sc-f-chapter2 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot- Starter-web org.springframework.cloud spring-cloud-starter-netflix-ribbon
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
The @ LoadBalanced annotation indicates that this restRemplate enables load balancer.
@ SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class ServiceRibbonApplication {public static void main (String [] args) {SpringApplication.run (ServiceRibbonApplication.class, args);} @ Bean @ LoadBalanced RestTemplate restTemplate () {return new RestTemplate ();}}
Write a test class HelloService that consumes the "/ hi" interface of the ioc service through the restTemplate previously injected into the service-hi container, where we directly
The specific url address is replaced by the program name. In ribbon, the specific service instance is selected according to the service name, and the appliance is used when the service instance is requested.
Replace the service name with the url of the body, 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; @ GetMapping (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.
At this point, I believe you have a deeper understanding of "how to use ribbon+rest". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.