1. 程式人生 > >springCloud分散式事務實戰(六)編寫第二個微服務

springCloud分散式事務實戰(六)編寫第二個微服務

(1)建立工程
springCloud分散式事務實戰(六)編寫第二個微服務

(2)新增 jar pom.xml
新增:springboot 父, mysql連線,(mybatis, spring-mybatis springboot ,阿里連線池) ,
服務中心客戶端。

<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>

<groupId>com.jh</groupId>
<artifactId>BlockMicroService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BlockMicroService</name>
<url>http://maven.apache.org</url>
<!-- 1 spring boot parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath />
</parent>

<!--1 屬性 -->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.7</java.version>
    <maven.compile.source>1.7</maven.compile.source>
    <maven.compile.target>1.7</maven.compile.target>
    <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    <lcn.last.version>4.1.0</lcn.last.version>
</properties>

<dependencies>
    <!--2 mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.43</version>
    </dependency>

    <!-- 3 包括mybatis,mybatis-spring,spring boot,spring 等 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>

    <!--4  註冊中心 -->

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <!-- 5 連線池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.19</version>
    </dependency>
    </dependencies>

<!-- spring cloud 依賴版本 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

</project>
(3)編寫配置檔案Application.properties

配置釋出服務名,埠;配置中心地址;連線mysql 引數
#1 register server
#服務名
spring.application.name =themeMicroService
#服務埠
server.port =8021
#註冊中心地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka

spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/forum2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
spring.datasource.username= root
spring.datasource.password=
spring.datasource.initialize =true
init-db= true
logging.level.com.codingapi=debug
(4)編寫實體,dao和對映。
實體:
public class Theme {
private Integer id;
private String tName;
private String tDescription;
get set
}
Dao 和對映

br/>@Mapper
public interface ThemeDao {

/**
 * 查詢
 * 
 * @return
 */
@Select(value = "select *  from theme")
public List<Theme> getThemeList();

/**
 * 插入
 * 
 * @param bname
 * @param bDescription
 * @return
 */

@Insert(value = "insert  into theme(tName,tDescription,blockId)" + " values(#{tName},#{tDescription},"
        + "#{blockId})")
public int saveTheme(@Param("tName") String tName, @Param("tDescription") String tDescription,  @Param("blockId")  Integer blockId);

}
(5)編寫服務層

服務介面

public interface ThemeService {

List<Theme> getThemeList();
int saveTheme(String tName, String  tDescription , Integer blockId);

}
服務實現:
package com.jh.service.impl;

@Service
public class ThemeServiceImpl implements ThemeService {
br/>@Autowired
private ThemeDao themeDao;

@Override
public List<Theme> getThemeList() {
    return themeDao.getThemeList();
}
@Override
public int saveTheme(String tName, String tDescription, Integer blockId) {
    // TODO Auto-generated method stub
    int rs1 = themeDao.saveTheme(tName, tDescription, blockId);// 儲存1
    return rs1;

}

}

(6)編寫控制層 br/>@RestController
public class ThemeController {

@Autowired
private ThemeService themeService;// 塊服務,第一個服務

// 1接受請求
@RequestMapping(value = "/getThemeList", method = RequestMethod.GET)
public List<Theme> getThemeList() {
    List<Theme> ThemeList = themeService.getThemeList();
    return ThemeList;
}

@RequestMapping(value = "/saveTheme", method = RequestMethod.GET)
public  int saveTheme() {
    Integer result = themeService.saveTheme("jwg2", "jwg2", 1); 
return   result 
}

}

(7) 編寫主程式
開啟springboot應用程式,註冊中心客戶端,mybatis掃描和定義一個數據源
package com.jh;

@SpringBootApplication //spring boot應用程式br/>@EnableEurekaClient
@MapperScan("com.jh.dao")
public class ThemeMicroService {
public static void main(String[] args) {

    SpringApplication.run(ThemeMicroService.class, args);
}

//1環境
@Autowired
private Environment env;

@Bean
public DataSource dataSource() {
    DruidDataSource dataSource = new DruidDataSource();

    dataSource.setUrl(env.getProperty("spring.datasource.url"));
    dataSource.setUsername(env.getProperty("spring.datasource.username"));//使用者名稱
    dataSource.setPassword(env.getProperty("spring.datasource.password"));//密碼
    dataSource.setInitialSize(10);
    dataSource.setMaxActive(50);
    dataSource.setMinIdle(1);
    dataSource.setMaxWait(60000);
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setPoolPreparedStatements(false);
    return dataSource;      
}

}
(8)測試
啟動註冊中心,啟動微服務
然後啟動瀏覽器
springCloud分散式事務實戰(六)編寫第二個微服務