SpringCloud微服务架构-服务提供者与消费者2

作者: breezedancer | 来源:发表于2017-10-17 08:40 被阅读342次

接上文,我们得到了服务提供者,user 微服务提供者,接下来我们要插件消费者来使用这个微服务

构建电影微服务

够一个电影微服务,名称ms-simple-consumer-movie

ms-simple-consumer-movie微服务

依赖和之前的提供者相同,需要增加的其他依赖,可以在后续 POM 里面增加


依赖

最终得到项目骨架


电影微服务项目骨架

项目骨架修改

首先把 application.properties 修改为 yml 文件结尾
application.yml 里面修改服务端口为8010
建立几个包,包括 modelController
增加 druid 的依赖
增加 yml 的数据源链接信息,雷同服务提供者

代码部分

这个 User 对象和提供者一样,这里貌似有重复的地方,实际情况应该是和返回过来的 Json 对象进行匹配

package cn.ts.ms.movie.model;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private String nickName;
    private Date lastLoginDt;
    private String password;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNickName() {
        return nickName;
    }
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    public Date getLastLoginDt() {
        return lastLoginDt;
    }
    public void setLastLoginDt(Date lastLoginDt) {
        this.lastLoginDt = lastLoginDt;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

在入口处,增加 RestTemplate 类的@Bean注解,这是为了能在 Controller 里面引用该类,然后方便调用服务提供者的接口

package cn.ts.ms.movie;

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

@SpringBootApplication
public class MsSimpleConsumerMovieApplication {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(MsSimpleConsumerMovieApplication.class, args);
    }
}

最后来到 Controller 里面进行调用

package cn.ts.ms.movie.controller;

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

import cn.ts.ms.movie.model.User;

@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/user/{id}")
    public User findByUserId(@PathVariable Integer id){
        return restTemplate.getForObject("http://localhost:8000/user/find?id="+id, User.class);
    }
}

上面的代码里面,使用 RestTemplate 的getForObject方法,里面传入远程的 URL,设定返回类型是 User,我们再浏览器里面测试看看

运行与调试

首先打开服务提供者,然后打开服务调用者,最后在浏览器输入http://localhost:8010/user/1

服务都已经打开
可以看到一个是8000端口,一个8010端口

可以看到正常调用

扩展

为项目增加监控,增加 Spring Boot Actuator,Spring Boot Actuator提供很多监控端点,以http://{server}:{port}/{endpoint}的形式访问

端点 描述 http方法
autoconfig 显示字段配置信息 GET
beans 显示应用程序上下文所有的 bean GET
configprops 显示@ConfiguratonProperties配置属性列表 GET
dump 显示线程活动快照 GET
env 显示 环境变量 GET
health 显示 健康指标 GET
info 显示应用信息 GET
mappings 显示所有 URL 路径 GET
metrics 显示应用度量标准信息 GET
shutdown 关闭应用(默认不开启) POST
trace 显示跟踪信息 GET

增加依赖,这里把提供者和消费者都增加这个依赖

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

然后访问:http://localhost:8000/health,http://localhost:8010/health

{
"status": "UP"
}

http://localhost:8000/info将返回{}
需要返回更多信息,需要在 yml 里面配置 info 的信息

info:
  app:
    name: ms-simple-consumer-movie
    encoding: UTF-8

这时返回的信息就是

{
"app": {
"name": "ms-simple-consumer-movie",
"encoding": "UTF-8"
}
}

至此为止,我们把服务提供和服务消费集成起来了,但我们在 RestTemplate 调用微服务的时候,我们是写的硬编码,这个很有问题,虽然我们可以把这个硬编码写到配置里面去,但是还是存在问题,比如需要修改提供者的配置,消费者也需要修改,无法动态伸缩,那么如何解决这个问题呢,请继续关注后续文章

相关文章

  • Eureka Server学习笔记

    基础架构服务注册中心服务提供者服务消费者 服务提供者 服务消费者 服务注册中心

  • SpringCloudH第二章:Eureka服务注册中心

    springCloud :1) 服务注册中心2)服务提供者向注册中心注册服务3)服务消费者向注册中心获取服务 这一...

  • SpringCloud微服务架构-服务提供者与消费者2

    接上文,我们得到了服务提供者,user 微服务提供者,接下来我们要插件消费者来使用这个微服务 构建电影微服务 够一...

  • Dubbo入门

    架构 服务容器负责启动,加载,运行服务提供者。 服务提供者在启动时,向注册中心注册自己提供的服务。 服务消费者在启...

  • springcloud-服务注册与发现Eureka

    什么是服务注册与发现 服务消费者找到服务提供者的这种机制称为服务发现,又或者服务注册。 服务提供者、服务消费者、服...

  • Feign声明式服务调用

    技术需求点: 在SpringCloud部署的微服务系统中,消费者通过Feign组件调用提供者的微服务。 一.Fei...

  • Eureka详解

    Eureka的三个核心角色:服务注册中心、服务提供者和服务消费者 基础架构 -服务注册中心:Eureka提供的服务...

  • 第三篇 服务提供者和服务消费者

    一、服务提供者和服务消费者概念 服务提供者:服务的被调用方(即:为其他服务提供服务的服务)服务消费者: 服务的调用...

  • Spring Cloud系列--Spring Cloud Eur

    Eureka 核心要素 服务注册中心:提供服务注册与发现功能。 服务提供者:提供服务的应用。 服务消费者:消费者从...

  • 微服务系列-注册和发现服务

    注册中心原理 在微服务架构下,主要有三种角色:服务提供者(RPC Server)、服务消费者(RPC Client...

网友评论

    本文标题:SpringCloud微服务架构-服务提供者与消费者2

    本文链接:https://www.haomeiwen.com/subject/hkeruxtx.html