搭建微服务(服务提供者与服务消费者)

概念

名词 概述
服务提供者 服务的被调用方(即:为其他服务提供服务的服务)
服务消费者 服务的调用方(即:依赖其他服务的服务)
实例 1.首先打开官网http://start.spring.io/ 输入相应内容,并选好WEB和JPA以及H2这3个组件,点击生成服务者项目骨架。
搭建微服务(服务提供者与服务消费者)
文章图片

2.再次输入相应内容,并选好WEB组件,然后生成消费者项目骨架。
搭建微服务(服务提供者与服务消费者)
文章图片

下载好骨架后,解压。
搭建微服务(服务提供者与服务消费者)
文章图片

打开IDE工具分别导入两个项目骨架
搭建微服务(服务提供者与服务消费者)
文章图片

搭建微服务(服务提供者与服务消费者)
文章图片

服务者 在resources下创建schema.sql和data.sql文件(H2数据库)
搭建微服务(服务提供者与服务消费者)
文章图片

【搭建微服务(服务提供者与服务消费者)】schema.sql中写入创建表语句
drop table user if exists; create table user( id bigint generated by default as identity, username varchar(40), name varchar(20), age int(3), balance decimal(10,2), primary key(id) );

data.sql中写入插入数据语句
insert into user(id,username,name,age,balance) values(1,'user1','孙林',20,100.00); insert into user(id,username,name,age,balance) values(2,'user2','小灰灰',20,100.00); insert into user(id,username,name,age,balance) values(3,'user3','沈阳',20,100.00); insert into user(id,username,name,age,balance) values(4,'user4','大辉哥',20,100.00); insert into user(id,username,name,age,balance) values(5,'user5','蛋蛋',20,100.00);

分别创建entityControllerdao
搭建微服务(服务提供者与服务消费者)
文章图片

创建实体类User.java
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Short age; @Column private BigDecimal balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; }}

创建UserRepository.java
package com.itmuch.cloud.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.itmuch.cloud.entity.User; @Repository public interface UserRepository extends JpaRepository{}

创建UserController.java
package com.itmuch.cloud.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 com.itmuch.cloud.dao.UserRepository; import com.itmuch.cloud.entity.User; @RestController public class UserController { @Autowired private UserRepository userRepository; /** * 根据Id查询用户信息 * @param id * @return */ @GetMapping("/simple/{id}") public User findById(@PathVariable Long id) { return userRepository.findById(id).orElse(null); } }

配置application.yml
server: port: 7900 spring: jpa: generate-ddl: false show-sql: true hibernate: ddl-auto:none datasource: platform: h2 schema: classpath:schema.sql data: classpath:data.sql

消费者 搭建微服务(服务提供者与服务消费者)
文章图片

创建MovieController.java
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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 com.itmuch.cloud.entity.User; @RestController public class MovieController { @Autowired private RestTemplate restTemplate; @Value("${user.userServicePath}") private String userServicePath; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { return restTemplate.getForObject(userServicePath+ id,User.class); } }

创建实体类User.java
package com.itmuch.cloud.entity; import java.math.BigDecimal; public class User { private Long id; private String username; private String name; private Short age; private BigDecimal balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; }}

配置application.yml
server: port: 7901 user: userServicePath: http://localhost:7900/simple/

启动类里创建restTemplate实例
package com.itmuch.cloud; 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 MicroserviceSimpleConsumerMovieApplication { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args); }}

测试 分别启动两个项目后,在浏览器中输入http://localhost:7901/movie/1 显示查询结果,证明消费者已成功调用服务者。
搭建微服务(服务提供者与服务消费者)
文章图片


    推荐阅读