一、Spring Cloud 微服务
一、服务提供者
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
2) Spring Boot 主程序
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);}
}
3) Entity 类
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;@Columnprivate String username;@Columnprivate String name;@Columnprivate Integer age;@Columnprivate 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;}}
4) DAO操作
package com.itmuch.cloud;import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository {}
5) Controller 控制层
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 {@Autowiredprivate 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;}}
6) 配置文件
server:port: 8000session:timeout: 30tomcat:max-threads: 0uri-encoding: UTF-8spring:datasource:url: jdbc:mysql://localhost:3306/bootusername: rootpassword: xshdbdriver-class-name: com.mysql.jdbc.Driverjpa:# Specify the DBMSdatabase: MYSQL# Show or not log for each sql queryshow-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
2) Spring Boot 主程序
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);}
}
3) Entity 类
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;}}
4) Controller 控制层
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;@Autowiredprivate 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);}}
4) 配置文件
server:port: 8100user: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'); 本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
