为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Spring Cloud

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

构建电影微服务

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

webp

ms-simple-consumer-movie微服务


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


webp

依赖

最终得到项目骨架


webp

电影微服务项目骨架

项目骨架修改

首先把 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;@SpringBootApplicationpublic 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;@RestControllerpublic 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

webp

服务都已经打开


可以看到一个是8000端口,一个8010端口

webp


可以看到正常调用


扩展

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

端点描述http方法
autoconfig显示字段配置信息GET
beans显示应用程序上下文所有的 beanGET
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 调用微服务的时候,我们是写的硬编码,这个很有问题,虽然我们可以把这个硬编码写到配置里面去,但是还是存在问题,比如需要修改提供者的配置,消费者也需要修改,无法动态伸缩,那么如何解决这个问题呢,请继续关注后续文章



作者:breezedancer
链接:https://www.jianshu.com/p/eb19512446ea


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消