SpringCloud implementation service call (RESTTTEMPLATE mode)

The previous article “Spring Cloud Building Registration Center and Service Registration” introduced the construction of the registration center and service registration. This article will introduce the process of service consumers calling service providers .

Contents of this article

< p id="tocid_0" class="toc" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px; margin-left: 25px;">1. Service call process二、 Service providerThree, serve consumersfour, service call combat

1. Service invocation process

The overall process is to start the registry first, service providers provide services and register to the registry, and consumers obtain services from the registry and execute them.

There are three roles required to implement service invocation: service registration center, service Providers and service consumers. The service registry and service providers have been implemented in the previous article. The following will demonstrate in detail the process of building and invoking services by service consumers.

Second, service provider

To transform the service provider in the previous article, add a controller, UserController. The java code is as follows:

/**
* User Service
*/

@Slf4j
@RestController
@RequestMapping("/provider")
public class UserController{

staticMap UserMap = new HashMap<>();

static{
//simulation database
Useruser1=new User(1, "张三", "123456");
userMap.put(1,user1 );
Useruser2=new User(2, "李四", "123123");
userMap.put(2, user2);
}

/**
Query by id
*/

@RequestMapping("/getUser")
public String getUser(Integer id){< br> User user = userMap.get(id);
return JSON.toJSONString(user);
}

}

Three, serving consumers

First create a SpringBoot project, name it spring-cloud-user-consumer, and then write the code according to the following steps.

    < li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;">pom.xml code

add eureka-server dependency, the code is as follows:

<dependencies> 
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>< br>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>

<dependency>
<groupId>org.springframework.cloud< /groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server artifactId>
dependency>

<dependency>
span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important ;"><groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
< br><dependencyManagement>
<dependencies>
<dependency>< /span>
<groupId>org.springframework.cloudgroupId>

span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important ;"><artifactId>spring-cloud-dependenciesartifactId>
< version>Finchley.RELEASEversion>
< type>pomtype>
<scope>import scope>
dependency>
dependencies>
de pendencyManagement>
  1. 启动类代码

启动类添加注解@EnableDiscoveryClient,添加该注解后,项目就具有了服务注册的功能,代码如下:

@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudUserConsumerApplication {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

}
  1. 配置文件

使用yml的配置文件,application.yml配置如下:

server:
  port: 8082 #服务端口
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/
spring:
  application:
    name: user-service-consumer

四、服务调用实战

  1. 启动服务中心并注册服务

代码编写之后,按顺序启动spring-cloud-eureka、spring-cloud-user-service和spring-cloud-user-consumer,先访问注册中心http://localhost:9001/,出现下图说明注册中心和两个服务以及注册成功。

分享图片
注册中心运行截图

  1. 服务调用

打开postman访问http://localhost:8082/consumer/getUser?id=2,出现下图后说明服务已经调用成功。

分享图片
postman调用截图

 

到此SpringCloud实现服务间调用功能已经全部实现,有问题欢迎留言沟通哦!

完整源码地址: https://github.com/suisui2019/springboot-study

推荐阅读
1.SpringCloud搭建注册中心与服务注册
2.SpringBoot整合ActiveMQ,看这篇就够了!
3.别在 Java 代码里乱打日志了,这才是正确的打日志姿势!
4.编码神器Lombok,学会后开发效率至少提高一倍!
5.利用Spring Boot+zxing,生成二维码还能这么简单


限时领取免费Java相关资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高并发分布式、大数据、机器学习等技术。
关注下方公众号即可免费领取:

Java碎碎念公众号Java碎碎念公众号

上一篇文章《SpringCloud搭建注册中心与服务注册》介绍了注册中心的搭建和服务的注册,本文将介绍下服务消费者调用服务提供者的过程。

本文目录

一、服务调用流程二、服务提供者三、服务消费者四、服务调用实战

一、服务调用流程

总体流程是首先启动注册中心,服务提供者提供服务并注册到注册中心,消费者从注册中心中获取服务并执行。

实现服务调用需要有三个角色:服务注册中心、服务提供者和服务消费者,其中服务注册中心和服务提供者上一篇文章已经实现了,下面会详细演示下服务消费者搭建并调用服务的过程。

二、服务提供者

改造下上篇文章中的服务提供者,添加一个controller,UserController.java代码如下:

/**
 * 用户服务
 */

@Slf4j
@RestController
@RequestMapping("/provider")
public class UserController {

    static Map userMap = new HashMap<>();

    static {
        //模拟数据库
        User user1 = new User(1"张三""123456");
        userMap.put(1, user1);
        User user2 = new User(2"李四""123123");
        userMap.put(2, user2);
    }

    /**
     * 根据id 查询
     */

    @RequestMapping("/getUser")< br>    public String getUser(Integer id) {
        User user = userMap.get(id);
        return JSON.toJSONString(user);
    }

}

三、服务消费者

首先新建一个SpringBoot项目,命名spring-cloud-user-consumer,然后按照下面步骤编写代码即可。

  1. pom.xml代码

添加eureka-server的依赖,代码如下:

<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>
depende ncyManagement>
  1. 启动类代码

启动类添加注解@EnableDiscoveryClient,添加该注解后,项目就具有了服务注册的功能,代码如下:

@EnableDiscoveryClient
@Sp ringBootApplication
public class SpringCloudUserConsumerApplication {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

}
  1. 配置文件

使用yml的配置文件,application.yml配置如下:

server:
  port: 8082 #服务端口
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/
spring:
  application:
    name: user-service-consumer

四、服务调用实战

  1. 启动服务中心并注册服务

代码编写之后,按顺序启动spring-cloud-eureka、spring-cloud-user-service和spring-cloud-user-consumer,先访问注册中心http://localhost:9001/,出现下图说明注册中心和两个服务以及注册成功。

分享图片
注册中心运行截图

  1. 服务调用

打开postman访问http://localhost:8082/consumer/getUser?id=2,出现下图后说明服务已经调用成功。

分享图片
postman调用截图

 

到此SpringCloud实现服务间调用功能已经全部实现,有问题欢迎留言沟通哦!

完整源码地址: https://github.com/suisui2019/springboot-study

推荐阅读
1.SpringCloud搭建注册中心与服务注册
2.SpringBoot整合ActiveMQ,看这篇就够了!
3.别在 Java 代码里乱打日志了,这才是正确的打日志姿势!
4.编码神器Lombok,学会后开发效率至少提高一倍!
5.利用Spring Boot+zxing,生成二维码还能这么简单


限时领取免费Java相关资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高并发分布式、大数据、机器学习等技术。
关注下方公众号即可免费领取:

Java碎碎念公众号Java碎碎念公众号

Leave a Comment

Your email address will not be published.