搭建支付微服務的環境。java
新增一個 spring_cloud_payment 的數據庫,再新建 payment_info 數據庫表。建表語句以下:mysql
-- spring_cloud_payment.payment_info definition CREATE TABLE `payment_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `order_id` bigint(20) NOT NULL, `amount` varchar(100) NOT NULL, `status` varchar(100) NOT NULL, `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
package jiangbo.springcloud.entity; import java.util.Date; public class PaymentInfo { private long id; private long orderId; private String amount; private String status; private Date createTime; }
package jiangbo.springcloud.dao; import java.util.List; import jiangbo.springcloud.entity.PaymentInfo; public interface PaymentDao { List<PaymentInfo> queryAllPayments(); }
package jiangbo.springcloud.dao; import java.util.List; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import jiangbo.springcloud.entity.PaymentInfo; @Repository public class PaymentDaoImpl implements PaymentDao { private static final String QUERY_ALL_PAYMENT_SQL = "select * from payment_info"; private static final RowMapper<PaymentInfo> ROW_MAPPER = new BeanPropertyRowMapper<>(PaymentInfo.class); private final JdbcTemplate jdbcTemplate; public PaymentDaoImpl(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public List<PaymentInfo> queryAllPayments() { return jdbcTemplate.query(QUERY_ALL_PAYMENT_SQL, ROW_MAPPER); } }
package jiangbo.springcloud.service; import java.util.List; import jiangbo.springcloud.entity.PaymentInfo; public interface PaymentService { List<PaymentInfo> queryAllPayments(); }
package jiangbo.springcloud.service.impl; import java.util.List; import org.springframework.stereotype.Service; import jiangbo.springcloud.dao.PaymentDao; import jiangbo.springcloud.entity.PaymentInfo; import jiangbo.springcloud.service.PaymentService; @Service public class PaymentServiceImpl implements PaymentService { private final PaymentDao paymentDao; public PaymentServiceImpl(PaymentDao paymentDao) { this.paymentDao = paymentDao; } @Override public List<PaymentInfo> queryAllPayments() { return paymentDao.queryAllPayments(); } }
package jiangbo.springcloud.controller; import java.util.List; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import jiangbo.springcloud.entity.PaymentInfo; import jiangbo.springcloud.service.PaymentService; @RestController @RequestMapping("/payment") public class PaymentContrller { private final PaymentService paymentService; public PaymentContrller(PaymentService paymentService) { this.paymentService = paymentService; } @GetMapping List<PaymentInfo> allPayemtns() { return paymentService.queryAllPayments(); } }
server: port: 4420 spring: application: name: payment datasource: url: jdbc:mysql://localhost/spring_cloud_payment?useUnicode=true&characterEncoding=utf-8&useSSL=false driver-class-name: com.mysql.jdbc.Driver username: root password: jiangbo
package jiangbo.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication public class JiangBoApplication { public static void main(String[] args) { SpringApplication.run(JiangBoApplication.class, args); } }
訪問 http://localhost:4420/ ,未看到明顯的報錯信息,則證實支付微服務環境搭建成功。web
<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.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Edgware.SR6</version> </parent> <groupId>jiangbo.springcloud</groupId> <artifactId>07spring-cloud-payment</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>