目錄前端
詳細演示user-service的搭建過程,user-consume的搭建過程相似,只說明有區別的部分。
這兩個項目沒有構成完整的微服務demo,完整的須要去看主文章。java
一個Module就是一個項目,一個微服務。mysql
選擇Spring Initializr,JDK1.8及以上,Default URL(訪問這個URL:https://start.spring.io也能夠在頁面上選擇模塊構建並下載工程)web
填寫Group和Artifact信息spring
選擇模塊sql
因爲咱們要提供接口,查詢並返回數據,因此須要Web和SQL的相應模塊。數據庫
點Next->Finish完成。apache
項目結構如圖所示:json
除了自動生成的部分,我還添加了三個依賴api
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> </parent> <groupId>com.vplus.demo</groupId> <artifactId>user-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>user-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--*******************本身添加的依賴開始*****************--> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> <!--*******************本身添加的依賴結束*****************--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
來解決時區和亂碼問題。server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/你的數據庫名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: 你的數據庫用戶名 password: 你的數據庫用戶密碼 hikari: maximum-pool-size: 20 minimum-idle: 10 mybatis: type-aliases-package: com.vplus.demo.userservice.pojo
@RestController註解至關於@Controller註解外加給全部方法加上@ResponseBody註解。表現爲:每一個方法返回的都是json字符串。
@RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User queryById(@PathVariable("id") Long id) { return this.userService.queryById(id); } }
爲了簡便,再也不寫service接口和Impl實現了,直接合成一個Service類。
@Service public class UserService { @Autowired private UserMapper userMapper; public User queryById(Long id) { return this.userMapper.selectByPrimaryKey(id); } }
繼承通用Mapper,因爲@Mapper註解的存在,和通用Mapper重名,所以通用Mapper寫成全類名tk.mybatis.mapper.common.Mapper<User>
@Mapper public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{ }
啓動UserServiceApplication
的main函數,訪問http://localhost:8081/user/20便可查到ID爲20的用戶。
至此user-service模塊搭建完成
搭建流程和建立user-service模塊一致,須要注意一點:選擇依賴時只選Web模塊,不須要SQL模塊。由於數據是從user-service提供的接口裏面讀的。
添加第二個模塊後,Idea會提示你是否顯示Run DashBoard,要選擇顯示。
省略...... </dependencies> 省略....... <!--添加OKhttp支持--> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> 省略......
配置項目的tomcat端口爲8082
server: port: 8082
註冊RestTemplate
@SpringBootApplication public class UserConsumeApplication { @Bean public RestTemplate restTemplate() { return new RestTemplate(new OkHttp3ClientHttpRequestFactory()); } public static void main(String[] args) { SpringApplication.run(UserConsumeApplication.class, args); } }
這個項目還須要User類來承載數據,但不須要關聯數據表了,因此@Table
註解就不用寫了
@Data public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String username; private String phone; }
@RestController @RequestMapping("consume") public class ConsumerController { @Autowired private UserService userService; @GetMapping public List<User> consume(@RequestParam("ids") List<Long> ids) { return this.userService.queryUserByIds(ids); } }
@Service public class UserService { @Autowired private UserDao userDao; public List<User> queryUserByIds(List<Long> ids){ List<User> users = new ArrayList<>(); for (Long id : ids) { User user = this.userDao.queryUserById(id); users.add(user); } return users; } }
這裏的Dao並非「真正的Dao」,不是從數據庫查詢數據,而是使用RestTemplate
訪問遠程的rest接口。
@Component public class UserDao { @Autowired private RestTemplate restTemplate; public User queryUserById(Long id){ String url = "http://localhost:8081/user/" + id; return this.restTemplate.getForObject(url, User.class); } }
從Run DashBoard中依次啓動兩個項目。訪問:http://localhost:8082/consume?ids=20,21便可查詢id=20和21的兩個用戶的信息