1)数据库准备
1
2
3
4
5 1在数据库中mysql 分别创建2个数据库forum1和forum2
2forum1 下创建表block
3forum2下创建表Theme
4
5
sql代码如下:
1 数据库1Forum1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 1CREATE DATABASE IF NOT EXISTS forum1;
2USE forum1;
3DROP TABLE IF EXISTS block;
4CREATE TABLE block (
5id int(11) NOT NULL AUTO_INCREMENT,
6bname varchar(45) DEFAULT NULL,
7bDescription varchar(45) DEFAULT NULL COMMENT '板块描述\n',
8PRIMARY KEY (id)
9) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='板块';
10
11LOCK TABLES block WRITE;
12INSERT INTO block VALUES (1,'java','讨论java'),(2,'C++','讨论c++方面问题');
13UNLOCK TABLES;
14
15
2 数据库forum2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1CREATE DATABASE IF NOT EXISTS forum2 ;
2USE forum2;
3
4DROP TABLE IF EXISTS theme;
5CREATE TABLE theme (
6id int(11) NOT NULL AUTO_INCREMENT,
7tName varchar(50) DEFAULT NULL COMMENT '主题名',
8tDescription varchar(200) DEFAULT NULL COMMENT '主题描述',
9blockid int(11) DEFAULT NULL COMMENT '区id',
10PRIMARY KEY (id)
11) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='主题';
12
13LOCK TABLES theme WRITE;
14INSERT INTO theme VALUES (1,'java基础','java 基础',1),(2,'javaWeb','java web',1);
15UNLOCK TABLES;
16
17
2)编写第一个微服务BlockMicroService
(1)创建工程
(2)添加 jar pom.xml
添加:springboot 父, mysql连接,(mybatis, spring-mybatis springboot ,阿里连接池) ,
服务中心客户端。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3<modelVersion>4.0.0</modelVersion>
4<groupId>com.jh</groupId>
5<artifactId>BlockMicroService</artifactId>
6<version>0.0.1-SNAPSHOT</version>
7<packaging>jar</packaging>
8
9<name>BlockMicroService</name>
10<url>http://maven.apache.org</url>
11<!-- 1 spring boot parent -->
12<parent>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-parent</artifactId>
15 <version>1.4.3.RELEASE</version>
16 <relativePath />
17</parent>
18
19<!--1 属性 -->
20<properties>
21 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
23 <java.version>1.7</java.version>
24 <maven.compile.source>1.7</maven.compile.source>
25 <maven.compile.target>1.7</maven.compile.target>
26 <spring-cloud.version>Dalston.SR1</spring-cloud.version>
27 <lcn.last.version>4.1.0</lcn.last.version>
28</properties>
29
30<dependencies>
31 <!--2 mysql -->
32 <dependency>
33 <groupId>mysql</groupId>
34 <artifactId>mysql-connector-java</artifactId>
35 <version>5.1.43</version>
36 </dependency>
37
38 <!-- 3 包括mybatis,mybatis-spring,spring boot,spring 等 -->
39 <dependency>
40 <groupId>org.mybatis.spring.boot</groupId>
41 <artifactId>mybatis-spring-boot-starter</artifactId>
42 <version>1.1.1</version>
43 </dependency>
44
45 <!--4 注册中心 -->
46
47 <dependency>
48 <groupId>org.springframework.cloud</groupId>
49 <artifactId>spring-cloud-starter-eureka</artifactId>
50 </dependency>
51
52 <!-- 5 连接池 -->
53 <dependency>
54 <groupId>com.alibaba</groupId>
55 <artifactId>druid</artifactId>
56 <version>1.0.19</version>
57 </dependency>
58 </dependencies>
59
60<!-- spring cloud 依赖版本 -->
61<dependencyManagement>
62 <dependencies>
63 <dependency>
64 <groupId>org.springframework.cloud</groupId>
65 <artifactId>spring-cloud-dependencies</artifactId>
66 <version>Dalston.SR3</version>
67 <type>pom</type>
68 <scope>import</scope>
69 </dependency>
70 </dependencies>
71</dependencyManagement>
72</project>
73
74
(3)编写配置文件Application.properties
配置发布服务名,端口;配置中心地址;连接mysql 参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1#1 register server
2#服务名
3spring.application.name =blockMicroService
4#服务端口
5server.port =8020
6#注册中心地址
7eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka
8
9spring.datasource.driver-class-name =com.mysql.jdbc.Driver
10spring.datasource.url=jdbc:mysql://localhost:3306/forum1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
11spring.datasource.username= root
12spring.datasource.password=
13spring.datasource.initialize =true
14init-db= true
15logging.level.com.codingapi=debug
16
17
(4)编写实体,dao和映射。
实体:
1
2
3
4
5
6
7
8
9 1public class Block {
2
3private int id;
4private String bname;
5private String bDescription;
6get set …
7}
8
9
Dao 和映射
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 1package com.jh.dao;
2
3@Mapper
4public interface BlockDao {
5
6/**
7 * 查询
8 *
9 * @return
10 */
11@Select(value = "select * from block")
12public List<Block> getBlockList();
13
14/**
15 * 插入
16 *
17 * @param bname
18 * @param bDescription
19 * @return
20 */
21
22@Insert(value = "insert into block(bname,bDescription)" + " values(#{bname},#{bDescription})")
23
24public int saveBlock(@Param("bname") String bname, @Param("bDescription") String bDescription);
25}
26
27
(5)编写服务层
服务接口
1
2
3
4
5
6
7
8 1public interface BlockService {
2
3List<Block> getBlockList();
4
5int saveBlock(String bname, String bDescription);
6}
7
8
服务实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1@Service
2public class BlockServiceImpl implements BlockService {br/>@Autowired
3private BlockDao blockDao;
4
5@Override
6public List<Block> getBlockList() {
7return blockDao.getBlockList();br/>}
8*/
9@Override
10public int saveBlock(String bName, String bDescription)
11{
12int rs1 = blockDao.saveBlock("jwg1", "111");// 保存1
13return rs1;
14}
15}
16
17
(6)编写控制层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1package com.jh.controller;br/>@RestController
2public class BlockController {
3/ private final Logger logger = Logger.getLogger(getClass()); /
4
5 @Autowired
6private BlockService blockService;// 区块服务,第一个服务
7
8// 1接受请求
9@RequestMapping(value = "/getBlockList", method = RequestMethod.GET)
10public List<Block> getBlockList() {
11 List<Block> blockList = blockService.getBlockList();
12 return blockList;
13}
14//
15@RequestMapping(value = "/saveBlock", method = RequestMethod.GET)
16public String saveBlock() {
17 //调用服务
18 Integer result=blockService.saveBlock("aa", "aa");
19 return ;
20}
21}
22
23
(7) 编写主程序
开启springboot应用程序,注册中心客户端,mybatis扫描和定义一个数据源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 1package com.jh;
2
3import javax.sql.DataSource;
4@SpringBootApplication //spring boot应用程序
5@EnableEurekaClient //服务中心客户端
6@MapperScan("com.jh.dao")//mybatis 扫描
7public class BlockMicroService {
8public static void main(String[] args) {
9
10 SpringApplication.run(BlockMicroService.class, args);
11}
12
13//1环境
14@Autowired
15private Environment env;
16
17@Bean
18public DataSource dataSource() {
19 DruidDataSource dataSource = new DruidDataSource();
20
21 dataSource.setUrl(env.getProperty("spring.datasource.url"));
22 dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
23 dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
24 dataSource.setInitialSize(10);
25 dataSource.setMaxActive(50);
26 dataSource.setMinIdle(1);
27 dataSource.setMaxWait(60000);
28 dataSource.setValidationQuery("SELECT 1");
29 dataSource.setTestOnBorrow(false);
30 dataSource.setTestWhileIdle(true);
31 dataSource.setPoolPreparedStatements(false);
32
33 return dataSource;
34 }
35}
36
37
(8)测试
启动注册中心,启动微服务
然后启动浏览器