社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
graph LR
A[用户] --> |购票|B[电影微服务]
B--> |查询用户信息|C[用户微服务]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(40) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`balance` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `user`
-- ----------------------------
BEGIN;
INSERT INTO `user` VALUES ('1', 'account1', '张三', '20', '100.00'), ('2', 'account2', '李四', '28', '180.00'), ('3', 'account3', '王五', '32', '230.00');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
@Data
@Entity(name = "user")
public class UserEntity {
@Id
@GeneratedValue
private int id;
private String username;
private String name;
private int age;
private BigDecimal balance;
}
@Repository
public interface UserRepository extends JpaRepository<UserEntity,Integer>{}
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/{id}")
public UserEntity findById(@PathVariable int id){
UserEntity userEntity = userRepository.findOne(id);
return userEntity;
}
}
spring:
datasource:
url: jdbc:mysql://localhost:3306/flim?useSSL=false
username: root
password: 123456ly
jpa:
show-sql: true
database-platform: org.hibernate.dialect.MySQL5Dialect
{"id":1,"username":"account1","name":"张三","age":20,"balance":100.00}
@SpringBootApplication
public class ConsumerApplication {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/user/{id}")
public UserEntity findById(@PathVariable int id){
return this.restTemplate.getForObject("http://localhost:8080/"+id,UserEntity.class);
}
}
server:
port: 8010
spring:
datasource:
url: jdbc:mysql://localhost:3306/flim?useSSL=false
username: root
password: 123456ly
{"id":1,"username":"account1","name":"张三","age":20,"balance":100.00}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!