Spring Cloud EureKa Ribbon Service Registration discovery and calling, eurekaribbon

Source: Internet
Author: User

Spring Cloud EureKa Ribbon Service Registration discovery and calling, eurekaribbon

Overview

A simple example is provided to demonstrate the basic usage of EureKa and Ribbon in Spring Cloud.

Version and Environment

  1. IDEA
  2. Spring Boot 1. 5. · 0
  3. JDK 1.8
  4. Maven 3

Build eureka server

In Spring Cloud, you can use eureka to manage microservices. microservices can be registered in eureka.

First, you can use the IDEASpring InitialzrTo create the eureka server registration center.

Modify the application. properties file and add the following content:

spring.application.name=eureka-server eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false server.port=8881

Start class generated for us in Spring BootServerApplicationJoin@EnableEurekaServerAnnotation

package com.springcloud.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class ServerApplication {  public static void main(String[] args) {    SpringApplication.run(ServerApplication.class, args);  }}

Enter http: // localhost: 8881/in the browser/

You can see the following interface:

We can see that there is no service registration yet. At this point, a simple microservice registration center has been built up.

Write the microservice userservice

Next, use rest to build a microservice interface and register it with the Registration Center. Still usedSpring InitialzrTo build a new project. The usage method is the same as above.

Note: Check this timeEureka DiscoveryComponent. InsteadEureka Server.

Modify the application. properties file and add the following content:

spring.application.name=user server.port=8882 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/

InSpring BootForUserApplicationClass@EnableDiscoveryClientAnnotation.

package com.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@SpringBootApplicationpublic class UserApplication {  public static void main(String[] args) {    SpringApplication.run(UserApplication.class, args);  }}

Create a rest full microservice interface.

package com.springcloud;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController {  @GetMapping("/getUser")  public String getUser() {    return "I am user list.";  }}

After running UserApplication, access http: // localhost: 8881/again/

You will find that the user service has been registered.

Write a microservice order

Next, we will build an ordered microservice and access the interfaces in the user microservice.

Still usedSpring InitialzrTo build a new project. The user microservice can be deployed on multiple machines. When the client accesses this service, the request can be routed to any machine where the user service is deployed. Therefore, the client needs to use a routing algorithm to schedule the user service. In Spring Cloud, you can use the Ribbon component for client routing. Ribbon will go to the Service Registration Center to obtain the service list to call the corresponding service.

In additionEureka DiscoveryComponent. You also need to checkRibbon.

Modify the application. properties file and add the following content:

spring.application.name=order server.port=8883 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/

InSpring BootForOrderApplicationClass.

package com.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@EnableDiscoveryClient@SpringBootApplicationpublic class OrderApplication {  @Bean  @LoadBalanced  RestTemplate restTemplate() {    return new RestTemplate();  }  public static void main(String[] args) {    SpringApplication.run(OrderApplication.class, args);  }}

Ribbon is used.@LoadBalancedAnnotation.

WriteOrderController.

package com.springboot;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;@RestControllerpublic class OrderController {  @Autowired  private RestTemplate restTemplate;  @GetMapping("/getOrderUser")  public String getOrderUser() {    return restTemplate.getForEntity("http://user/getUser",String.class).getBody();  }}

RunOrderApplicationAccess http: // localhost: 8881/

The order Service is also registered in the Registration Center.

Next, visitOrderControllerIngetOrderUserMethod To trigger the callUserControllerOfgetUserMethod.
Enter http: // localhost: 8883/getOrderUser in the browser

The returned result is: I am user list.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.