微服务分布式事务实战(六)编写第二个微服务

本文详细介绍使用Spring Cloud搭建微服务的过程,涵盖工程创建、POM配置、编写实体与服务层、控制器及主程序等内容,深入解析分布式事务的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(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 和映射

@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 {
    @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)编写控制层

@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应用程序
@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)测试
启动注册中心,启动微服务
然后启动浏览器
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值