From Getting Started to Mastering springcloud - The Age Without a Registry

This section focuses on invoking the http service without using springcloud.Let's first clear up a few concepts:

(1) Producer: the provider of a service whose services can be invoked by other programs or within itself;

(2) Consumer: the caller of a service, whose services depend on one or more services of the producer to complete.

A service can be both a producer and a consumer.

 

1. Producers:

(1)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.john.springcloud</groupId>
  <artifactId>springcloud-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/>
  </parent>
  
  <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
       <java.version>1.8</java.version>
  </properties>
  
  <dependencies>
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

</project>

(2) application.properties

server.port=8070

(3) Start the program:

package com.john.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {
	public static void main(String[] args) {
		SpringApplication.run(ProviderApplication.class, args);
	}
}

(4) Controller: It will be called by the consumer

package com.john.springcloud.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {
	
	@RequestMapping("/provider")
	public String hello() {
		
		return "hello, i am provider";
	}
}

2. Consumers:

(1) pom.xml: same producer

(2)application.properties

server.port=8080

(3) Start the program:

package com.john.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@Configuration
public class ConsumerApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerApplication.class, args);
	}
	
	/**
	 * RestTemplate is managed by IOC for http service calls
	 * @return
	 */
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

(4)Controller:

package com.john.springcloud.controller;

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

@RestController
public class ConsumerController {
	/**
	 * Inject RestTemplate
	 */
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("/consumer")
	public String hello() {	
		//Service call, need to know the address, port of the called service
		return restTemplate.getForObject("http://localhost:8070/provider", String.class);
	}
}

3. Start producers and consumers, visit http://localhost:8080/consumer , returned to hello, i am provider successfully

 

From the code above, you can see that remote service calls can be made through http, but you need to know the url address of the producer, the port number, and the mapping address of the corresponding controller, which the consumer needs to modify synchronously when the producer's information changes.It can also be maintained with a small number of calls, which can become a major problem when the call relationships are complex and the calls to each other are frequent.

The next section on resolving this problem is the Service Registry.

(Source download address for this section: https://github.com/JohnZhaowen/1.springcloud-no-register-center.git)

Tags: Maven Apache xml Spring

Posted on Tue, 03 Dec 2019 00:28:35 -0800 by n9ne