(1)创建工程
springCloud分布式事务实战(六)编写第二个微服务
(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
75 1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi: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
5 <groupId>com.jh</groupId>
6 <artifactId>BlockMicroService</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <packaging>jar</packaging>
9
10 <name>BlockMicroService</name>
11 <url>http://maven.apache.org</url>
12 <!-- 1 spring boot parent -->
13 <parent>
14 <groupId>org.springframework.boot</groupId>
15 <artifactId>spring-boot-starter-parent</artifactId>
16 <version>1.4.3.RELEASE</version>
17 <relativePath />
18 </parent>
19
20 <!--1 属性 -->
21 <properties>
22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24 <java.version>1.7</java.version>
25 <maven.compile.source>1.7</maven.compile.source>
26 <maven.compile.target>1.7</maven.compile.target>
27 <spring-cloud.version>Dalston.SR1</spring-cloud.version>
28 <lcn.last.version>4.1.0</lcn.last.version>
29 </properties>
30
31 <dependencies>
32 <!--2 mysql -->
33 <dependency>
34 <groupId>mysql</groupId>
35 <artifactId>mysql-connector-java</artifactId>
36 <version>5.1.43</version>
37 </dependency>
38
39 <!-- 3 包括mybatis,mybatis-spring,spring boot,spring 等 -->
40 <dependency>
41 <groupId>org.mybatis.spring.boot</groupId>
42 <artifactId>mybatis-spring-boot-starter</artifactId>
43 <version>1.1.1</version>
44 </dependency>
45
46 <!--4 注册中心 -->
47
48 <dependency>
49 <groupId>org.springframework.cloud</groupId>
50 <artifactId>spring-cloud-starter-eureka</artifactId>
51 </dependency>
52
53 <!-- 5 连接池 -->
54 <dependency>
55 <groupId>com.alibaba</groupId>
56 <artifactId>druid</artifactId>
57 <version>1.0.19</version>
58 </dependency>
59 </dependencies>
60
61 <!-- spring cloud 依赖版本 -->
62 <dependencyManagement>
63 <dependencies>
64 <dependency>
65 <groupId>org.springframework.cloud</groupId>
66 <artifactId>spring-cloud-dependencies</artifactId>
67 <version>Dalston.SR3</version>
68 <type>pom</type>
69 <scope>import</scope>
70 </dependency>
71 </dependencies>
72 </dependencyManagement>
73</project>
74
75
(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 =themeMicroService
4#服务端口
5server.port =8021
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/forum2?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 1public class Theme {
2private Integer id;
3private String tName;
4private String tDescription;
5get set
6}
7
8
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 1@Mapper
2public interface ThemeDao {
3
4 /**
5 * 查询
6 *
7 * @return
8 */
9 @Select(value = "select * from theme")
10 public List<Theme> getThemeList();
11
12 /**
13 * 插入
14 *
15 * @param bname
16 * @param bDescription
17 * @return
18 */
19
20 @Insert(value = "insert into theme(tName,tDescription,blockId)" + " values(#{tName},#{tDescription},"
21 + "#{blockId})")
22 public int saveTheme(@Param("tName") String tName, @Param("tDescription") String tDescription, @Param("blockId") Integer blockId);
23
24}
25
26
(5)编写服务层
服务接口
1
2
3
4
5
6
7 1public interface ThemeService {
2
3List<Theme> getThemeList();
4int saveTheme(String tName, String tDescription , Integer blockId);
5}
6
7
服务实现:
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 1package com.jh.service.impl;
2
3@Service
4public class ThemeServiceImpl implements ThemeService {
5 @Autowired
6 private ThemeDao themeDao;
7
8 @Override
9 public List<Theme> getThemeList() {
10 return themeDao.getThemeList();
11 }
12 @Override
13 public int saveTheme(String tName, String tDescription, Integer blockId) {
14 // TODO Auto-generated method stub
15 int rs1 = themeDao.saveTheme(tName, tDescription, blockId);// 保存1
16 return rs1;
17
18 }
19
20}
21(6)编写控制层
22
23@RestController
24public class ThemeController {
25
26 @Autowired
27 private ThemeService themeService;// 块服务,第一个服务
28
29 // 1接受请求
30 @RequestMapping(value = "/getThemeList", method = RequestMethod.GET)
31 public List<Theme> getThemeList() {
32 List<Theme> ThemeList = themeService.getThemeList();
33 return ThemeList;
34 }
35
36 @RequestMapping(value = "/saveTheme", method = RequestMethod.GET)
37 public int saveTheme() {
38 Integer result = themeService.saveTheme("jwg2", "jwg2", 1);
39 return result
40 }
41}
42
43
(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 1package com.jh;
2
3@SpringBootApplication //spring boot应用程序
4@EnableEurekaClient
5@MapperScan("com.jh.dao")
6public class ThemeMicroService {
7 public static void main(String[] args) {
8
9 SpringApplication.run(ThemeMicroService.class, args);
10 }
11
12 //1环境
13 @Autowired
14 private Environment env;
15
16 @Bean
17 public DataSource dataSource() {
18 DruidDataSource dataSource = new DruidDataSource();
19
20 dataSource.setUrl(env.getProperty("spring.datasource.url"));
21 dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
22 dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
23 dataSource.setInitialSize(10);
24 dataSource.setMaxActive(50);
25 dataSource.setMinIdle(1);
26 dataSource.setMaxWait(60000);
27 dataSource.setValidationQuery("SELECT 1");
28 dataSource.setTestOnBorrow(false);
29 dataSource.setTestWhileIdle(true);
30 dataSource.setPoolPreparedStatements(false);
31 return dataSource;
32 }
33}
34
35
(8)测试
启动注册中心,启动微服务
然后启动浏览器