上篇文章创建好了eurka注册中心,现在来创建服务提供者
首先来看看项目结构:
服务提供者需要从数据库查询记录,数据库名是:db_test,数据库脚本是:
CREATE TABLE `tb_user` ( `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `username` VARCHAR(50) NOT NULL COMMENT '用户名', `age` INT(11) NOT NULL COMMENT '年龄', `ctm` VARCHAR(50) NOT NULL COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 INSERT INTO `db_test`.`tb_user` (`username`, `age`, `ctm`) VALUES('张三', '18', NOW()) ; INSERT INTO `db_test`.`tb_user` (`username`, `age`, `ctm`) VALUES('李四', '20', NOW()) ; INSERT INTO `db_test`.`tb_user` (`username`, `age`, `ctm`) VALUES('王五', '19', NOW()) ;
服务提供者的pom文件:
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springcloud-parent</artifactId> <groupId>com.xiami</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>user-service</artifactId> <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> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
属性配置文件application.properties:
server.port=8080 spring.application.name=user-service eureka.client.service-url.defaultZone=http://localhost:8888/eureka spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/db_test?characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root
服务启动文件UserServiceApplication.java:
package com.xiami.user; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class,args); } }
User.java文件:
package com.xiami.user.entity; public class User { private int id; private String username; private int age; private String ctm; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCtm() { return ctm; } public void setCtm(String ctm) { this.ctm = ctm; } }
UserController.java文件(为了简单起见,这个控制器直接访问数据库,实际开发中很少这样操作的哦!):
package com.xiami.user.controller; import com.xiami.user.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.Mapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping("/{id}") public User queryById(@PathVariable Long id){ List<User> list = jdbcTemplate.query("select * from tb_user where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class)); if(list!=null && list.size()>0){ return list.get(0); }else{ return null; } } }
最后就启动UserServiceApplication,再访问localhost:8888,服务提供者已经成功注册到注册中心了:
再访问localhost:8080/user/2,显示服务提供者启动正常,可以访问数据库了:
微服务springcloud入门系列二(创建服务提供者),大功告成!