Spring cloud implements inter service call (resttemplate mode)


The previous article “spring cloud building registry and service registration” introduces the building of registry and service registration. This article will introduce the process of service consumers calling service providers.

Contents of this article

I. service calling processII. Service providerIII. service consumersIV. service call practice

I. service calling process

The overall process is to start the registration center first, the service provider provides services and registers to the registration center, and the consumer obtains services from the registration center and executes them.

There are three roles to realize service invocation: service registration center, service provider and service consumer. The service registration center and service provider have been implemented in the previous article. Next, the process of service consumer building and invoking service will be demonstrated in detail.

II. Service provider

Modify the service provider in the previous article, add a controller, and the usercontroller.java code is as follows:

/**User services * / @ slf4j @ restcontroller @ requestmapping ("/ provider") public class usercontroller {{staticmap map map usermap = newhashmap < > (); static {{{{@ slf4j @ slf4j @ rescontroller @ requestmapping ("/ provider") public class usercontroller {{static map map map map = newhashmap < > (); static {{// // // // // // database // database query useruser user 1 = user1 = = = = = = (user 1 = user1 = user1 = = = = = = (user 1 = user user 1 = user1 = user1 = user user 1 = = = = = = = (user 1 = user user user user user user user user user 1) user1 = database // // // // // // // // // new user (1, Zhang San, 123456); usermap.put (1, user1); user user2 = New u User (2, 'Li Si', '123123');} / * * * * * * * * query * * * // / @ requestmapping ("/ getuser") publicstring getuser (integer ID ID) {{{{{{{{{{{{'user' user = 'usermap. Get (ID);' return 'JSON. Tojsonstring (user);'

III. service consumers

First, create a new spring boot project, name it spring cloud user consumer, and then write the code as follows.

  1. Pom.xml code

Add the dependency of Eureka server as follows:

<dependencies>    <dependency>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-webartifactId>    dependency>    <dependency>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-testartifactId>        <scope>testscope>    dependency>    <dependency>        <groupId>org.springframework.cloudgroupId>        <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>    dependency>    <dependency>        <groupId>org.projectlombokgroupId>        <artifactId>lombokartifactId>        <optional>trueoptional>    dependency>dependencies><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.springframework.cloudgroupId>            <artifactId>spring-cloud-dependenciesartifactId>            <version>Finchley.RELEASEversion>            <type>pomtype>            <scope>importscope>        dependency>    dependencies>dependencyManagement>
  1. Start class code

Start the class and add the annotation @ enablediscoveryclient. After adding the annotation, the project has the function of service registration. The code is as follows:

@[email protected] class SpringCloudUserConsumerApplication {    @Bean    public RestTemplate restTemplate(){        return new RestTemplate();    }    public static void main(String[] args) {        SpringApplication.run(SpringCloudUserConsumerApplication.class, args);    }}
  1. configuration file

Using the configuration file of YML, application.yml is configured as follows:

Server: Port: 8082 service port Eureka: client: serviceurl: defaultzone: http: // localhost: 9001 / Eureka / spring: application: Name: user service consumer

IV. service call practice

  1. Start service center and register service

After the code is written, start spring cloud Eureka, spring cloud user service and spring cloud user consumer in order. First, visit the Registration Center http: / / localhost: 9001 /. The following figure shows the registration center and the two services and the successful registration.

Screenshot of registry operation

  1. Service invocation

Open postman to access http: / / localhost: 8082 / consumer / getuser? Id = 2. The following figure shows that the service has been called successfully.

Screenshot of postman call


This spring cloud service call function has been fully implemented. If you have any questions, please leave a message!

Full source address:https://github.com/suisui2019/springboot-study

Recommended reading
1. Spring cloud sets up registration center and service registration
2. Spring boot integrates ActiveMQ. This is enough!
3. Don’t log in Java code, this is the right way to log!
4. Lombok, the coding artifact, can improve the development efficiency by at least one time after learning it!
5. Using spring boot + zxing to generate QR code can be so simple

Collect free Java related materials in limited time, covering Java, redis, mongodb, mysql, zookeeper, spring cloud, Dubbo / Kafka, Hadoop, HBase, Flink and other high concurrent distributed, big data, machine learning and other technologies.
Pay attention to the public numbers below for free.

Java碎碎念公众号Java read the public address.