Play Turn Springcloud (f version) two. Service consumer (1) ribbon+resttemplate

Source: Internet
Author: User
on a blog Someone asked me, Springcloud series will not be serialized, you can see my label classification has opened the Springcloud topic, so of course will be serialized, I recently also bought a book in learning Springcloud Micro-service Framework, Knowledge will be shared at any time!!!!!!!!!!!!!!!!!!!!! Two Service Consumers

in a microservices architecture, the business is split into a separate service, and the service-to-service communication is based on http restful. Spring Cloud has two methods of service invocation, one ribbon+resttemplate and the other feign

This piece blog above a blog play turn Springcloud one. The registration and discovery of services (Eureka) is based on the project www.cnblogs.com/lsy131479/p/9613755.html

This blog will explain the ribbon+resttemplate mode, the next explanation feign mode

1. ribbon+resttemplate

The Ribbon is a load balancer client that can control some of the behavior of HTT and TCP very well .

Start the Demo1 project, start the Demo2 project, its port is 8762, change the port of the Demo2 configuration file to 8763, and start, and you will find: Demo2 Demo1 Registers 2 instances , which is equivalent to a small cluster .

Turn off the Demo2 startup Setup List before starting

Change the port number of the DEMO2 after the project starts and turns off the single-instance startup

Start Demo2 again to view the Registry's services http://localhost:8761

Will find: Demo2 registered 2 instances in Demo1, which is equivalent to a small cluster.

Build a service consumer

Project structure:

re-create a new Spring-boot project, named: DEMO3;

introduce the main project, and the associated jar package:

<Parent>   <groupId>Com.fsdm</groupId>   <Artifactid>Springcloud_test1</Artifactid>   <version>1.0-snapshot</version></Parent><Dependencies>   <Dependency>      <groupId>Org.springframework.cloud</groupId>      <Artifactid>Spring-cloud-starter-netflix-eureka-client</Artifactid>   </Dependency>   <Dependency>      <groupId>Org.springframework.boot</groupId>      <Artifactid>Spring-boot-starter-web</Artifactid>   </Dependency>   <Dependency>      <groupId>Org.springframework.cloud</groupId>      <Artifactid>Spring-cloud-starter-netflix-ribbon</Artifactid>   </Dependency></Dependencies>

YML configuration:

Eureka:  Client:    serviceurl:      Defaultzone: http: // localhost:8761/eureka/ Server:  Port: 8764 Spring:  Application:   #工程名称     name: service-ribbon

in the startup class of the project , the service center is registered through @enablediscoveryclient, and a bean:resttemplate is injected into the IOC of the program; And the @loadbalanced annotation shows that this restremplate turns on the load balancing function.

@EnableEurekaClient @enablediscoveryclient@springbootapplication  Public class demo3application {   publicstaticvoid  main (string[] args) {      Springapplication.run (demo3application. class , args);   }   @Bean   @LoadBalanced   resttemplate resttemplate () {      returnnew  Resttemplate ();   }}

Annotation parsing:

@EnableDiscoveryClient

1. based on spring-cloud-commons, and implemented in classpath .

2. Is if the selected registry is Eureka recommended @EnableEurekaClient, if it is the other registry recommended to use @ Enablediscoveryclient, if Eurekais added to the classpath , they do the same thing.

@Bean

1, Java object -oriented, the object has methods and properties, then you need an object instance to invoke methods and properties (i.e. instantiation);

2. All classes that have methods or attributes need to be instantiated to use these methods and attributes in order to be figurative;

3. rule: all subclasses and classes with methods or attributes are added to the annotations of the registered Bean to Spring IoC ;

4, the Bean is understood as the agent or spokesperson of the class (in fact, it is implemented by reflection, proxy), so that it can represent the class has the possession of the thing

5, We are on the Weibo @ over xxx, the other party will first see this message, and give you feedback, then in Spring , You identify an @ symbol, then Spring will take a look and get a bean from here or a bean

@LoadBalanced

1. indicates that the restremplate is enabled for load balancing.

2. Achieve load Balancing

Write a test class HelloService, the "/hi" interface of the Service-hi service is consumed by the resttemplate that was injected into the IOC container, where we directly use the program name instead of the specific URL, and in the ribbon it chooses the specific service instance according to the service name. , the service name is replaced with a specific URL, depending on the service instance at the time of the request.

 Public class HelloService {    @Autowired    resttemplate resttemplate;      Public string Hiservice (string name) {        return resttemplate.getforobject ("Http://SERVICE-HI/hi? Name= "+name,string. class );    }}

Write a controller that calls the HelloService method in the controller

@RestController  Public class Hellocontroler {    @Autowired    helloservice helloservice;     = "/hi")    public  string hi (@RequestParam string name) {        return Helloservice.hiservice (name);}    }

Start Demo3 Project

Operation of the project:

(Demo2 dual-boot)

multiple accesses to HTTP://LOCALHOST:8764/HI?NAME=FOREZP on the browser, the browser alternately executes the display :

This means that when we call the Resttemplate.getforobject ("http://SERVICE-HI/hi?name=" +name,string.class) method, A service instance that has been load balanced and has access to different ports.

the schema at this point: ( net pick )

  · One service registry,Eureka Server, Port 8761

  · Service-hi Project ran two instances, the ports are 8762,8763, respectively, registered with the Service registration center

  ·  Sercvice-ribbon Port 8764, registering with the Service registration center

  ·  when Sercvice-ribbon calls the Hi interface of Service-hi via Resttemplate, because the ribbon is load balanced, the Hi interface of service-hi:8762 and 87,632 ports is called in turn ;

                                    Not finished, to be continued ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.