接上文,我们得到了服务提供者,user 微服务提供者,接下来我们要插件消费者来使用这个微服务
构建电影微服务
够一个电影微服务,名称ms-simple-consumer-movie
依赖和之前的提供者相同,需要增加的其他依赖,可以在后续 POM 里面增加
依赖
最终得到项目骨架
电影微服务项目骨架
项目骨架修改
首先把 application.properties
修改为 yml
文件结尾
在 application.yml
里面修改服务端口为8010
建立几个包,包括 model
,Controller
增加 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 调用微服务的时候,我们是写的硬编码,这个很有问题,虽然我们可以把这个硬编码写到配置里面去,但是还是存在问题,比如需要修改提供者的配置,消费者也需要修改,无法动态伸缩,那么如何解决这个问题呢,请继续关注后续文章
网友评论