Learning service consumers of microservices -- ribbon and restTemplate

Microservices will split a single large project into several independent small services. The call between these small services uses HTTP restful. Spring cloud provides ribbon+restTemplate. Ribbon is a load balancing client.

1. First, start the Eureka server project and the Eureka client say hi project. Its port is 8792. Then change the port from 8792 to 8793 and start it. It is found that the registry 8792 is Down

At this time, click edit configuration to remove the check mark in front of Single instance only; then start two instances

At this point, it is found that Eureka server registers two instances, which is equivalent to a small cluster.

2. Create a new service consumer

build.gradle file

buildscript {
	ext {
		springBootVersion = '2.0.4.RELEASE'
	repositories {
	dependencies {

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {

ext {
	springCloudVersion = 'Finchley.SR1'

dependencies {

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"

application.yml file

  port: 8794

    name: service-ribbon

      defaultZone: http://localhost:8791/eureka/

Main method

package com.example.serviceribbon;

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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient   //Register with service center
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);

    @Bean   //Inject a Bean
    @LoadBalanced  //It shows that the bean has the function of load balancing
    RestTemplate restTemplate() {
        return new RestTemplate();


package com.example.serviceribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

public class HelloService {

    RestTemplate restTemplate;

//The "/ Hi" interface of Eureka client say hi service is consumed by the restTemplate injected into ioc 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 requesting according to the service instance:
    public String hiService() {
        return restTemplate.getForObject("http://EUREKA-CLIENT-SAY-HI/hi", String.class);


package com.example.serviceribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloController {
    HelloService helloService;

    public String sayHi() {
        return helloService.hiService();

At this time, start the service ribbon of the project and visit http://localhost:/hi. The following two situations occur:



Registration Center at this time:

Read the above:

A service registry with Eureka server port of 8791

The Eureka client say hi project runs two instances, with ports of 8792 and 8793 registered with the service registry respectively;

The service ribbon port is 8794, which is registered with the service registry;

When the service ribbon calls the interface of Eureka client say hi through restTemplate, because the ribbon performs load balancing, it will call the hi interface of Eureka client say hi, 8792, and 8793 instances in turn.

Keywords: Spring Java Gradle Eclipse

Added by Uzm on Sat, 04 Jan 2020 03:01:02 +0200