1) Maven配置文件
4.0.0
com.itmuch.cloud
microservice-simple-provider-user
0.0.1-SNAPSHOT
jar
demo1
Simple for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR3
org.springframework.cloud
spring-cloud-starter
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
package com.itmuch.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
package com.itmuch.cloud;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Data
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
package com.itmuch.cloud;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository {
}
package com.itmuch.cloud;
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;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/{id}") // 在Spring 4.3规则中,@GetMapping等价于@RequestMapping(method = RequestMethod.GET), 用于简化, 同理还有@PostMapping, @PutMapping, @DeleteMapping, @PatchMapping等
public User findById(@PathVariable Long id) {
User findOne = this.userRepository.findOne(id);
return findOne;
}
}
server:
port: 8000
session:
timeout: 30
tomcat:
max-threads: 0
uri-encoding: UTF-8
spring:
datasource:
url: jdbc:mysql://localhost:3306/boot
username: root
password: xshdb
driver-class-name: com.mysql.jdbc.Driver
jpa:
# Specify the DBMS
database: MYSQL
# Show or not log for each sql query
show-sql: true
# Hibernate dll auto (create, create-drop, update)
hibernate:
ddl-auto: update
1) Maven 配置文件
4.0.0
com.itmuch.cloud
microservice-simple-sonsumer-movie
0.0.1-SNAPSHOT
jar
demo2
Simple for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR3
org.springframework.cloud
spring-cloud-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
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 Demo2Application {
@Bean // 等价于 RestTemplate restTemplate = new RestTemplate();
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
}
package com.itmuch.cloud;
import java.io.Serializable;
import java.math.BigDecimal;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String name;
private Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
package com.itmuch.cloud;
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;
@RestController
public class UserController {
@Value("${user.userServiceUrl}") // 在yml文件中已经配置, 解决它的硬编码问题
private String userServiceUrl;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/user/{id}") // 在Spring 4.3规则中,@GetMapping等价于@RequestMapping(method = RequestMethod.GET), 用于简化, 同理还有@PostMapping, @PutMapping, @DeleteMapping, @PatchMapping等
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject(this.userServiceUrl + id, User.class);
}
}
server:
port: 8100
user:
userServiceUrl: http://localhost:8000/
CREATE TABLE `user` (
`id` bigint(20) NOT NULL,
`username` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`balance` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'account1', '张三', '20', '100.00');
INSERT INTO `user` VALUES ('2', 'account2', '李四', '30', '180.00');
INSERT INTO `user` VALUES ('3', 'account3', '王五', '28', '280.00');