总览

服务提供者: 服务的被调用方,为其他服务提供服务的服务
服务消费者: 服务的调用方,即依赖其他服务的服务
场景

围绕该场景,需要两个微服务
- 用户微服务,作为服务提供者为电影微服务提供服务
- 电影微服务,作为消费者调用用户微服务提供的服务
用户微服务
Spring Boot
Spring Data JPA
H2
lombok简化编码
用户库表
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) );
datasource.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);
model
@Entity @Data public class User implements Serializable{ private static final long serialVersionUID=226695758444267342L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Integer age; @Column private BigDecimal balance; }
Dao层
使用Spring Data JPA操作数据库
@Repository public interface UserRepository extends JpaRepository<User,Long>{ }
Controller 暴露Rest API
@RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/user/{id}") public User findById(@PathVariable Long id){ User user = new User(); user.setId(id); User user1=new User(); user1.setName("no this user"); return userRepository.findOne(Example.of(user)).orElse(user1); } }
配置文件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 logging: level: root: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE com.smart: DEBUG
电影微服务
Controller通过 RestTemplate 调用用户微服务提供的服务
仅仅作为服务消费者,所有只要在Controller层调用对应的rest接口即可
@RestController public class MovieController { @Autowired private RestTemplate restTemplate; @Value("${user.userServicePath}") private String userServicePath; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { return this.restTemplate.getForObject(this.userServicePath + id, User.class); } }
新建个User.java
@Data public class User implements Serializable { private static final long serialVersionUID = 8912111288470833198L; private Long id; private String username; private String name; private Integer age; private BigDecimal balance; }
启动类入口处,通过@Bean实例化RestTemplate
通过@Bean实例化RestTemplate
@SpringBootApplication public class MicorserviceSimpleConsumerMovieApplication { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MicorserviceSimpleConsumerMovieApplication.class, args); } }
配置文件 application.yml
配置端口及用户微服务的地址
server: port: 7901 user: userServicePath: http://localhost:7900/user/
测试
启动用户微服务,访问7901
访问数据库中存在的用户
http://localhost:7901/movie/1
{"id":1,"username":"user1","name":"张三","age":20,"balance":100.00}
参考:
https://blog.csdn.net/yangshangwei/article/details/84679248
立志如山 静心求实
浙公网安备 33010602011771号