搭建简单的SpringCloud项目二:服务层和消费层
GitHub:https://github.com/ownzyuan/test-cloud
前篇:搭建简单的SpringCloud项目一:注册中心和公共层
服务层(server)
搭建
用于直接操作数据库,对外提供相应接口,同时服务层也可以消费其他服务层提供的接口。
依赖:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-openfeign-core</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.github.drtrang</groupId> <artifactId>druid-spring-boot2-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.zy</groupId> <artifactId>cloud-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.zy</groupId> <artifactId>cloud-common-base</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
配置文件:
server:
port: 8082
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka/
spring:
application:
name: cloud-server
main:
allow-bean-definition-overriding: true
datasource:
url: jdbc:mysql://127.0.0.1:3306/tktest
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mapper
创建UserMapper
启动类:
@EnableDiscoveryClient(注册进注册中心)、@MapperScan(扫描mapper)
测试类测试:
import com.zy.mapper.UserMapper; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = CloudServerApplication.class) public class UserMapperTest { @Autowired private UserMapper userMapper; private Long id; @Before public void randomNum() { int max = 1000000000, min = 100000000; id = (long) (Math.random() * max) + min; } @Test public void insertTest() { UserDTO userDTO = new UserDTO(); userDTO.setId(id); userDTO.setName("inserted") .setStatus(1); int result = userMapper.insert(userDTO); System.out.println(result > 0); } @Test public void updateTest() { insertTest(); UserDTO userDTO = new UserDTO(); userDTO.setId(id); userDTO.setName("updated") .setStatus(1); int result = userMapper.updateByPrimaryKey(userDTO); System.out.println(result > 0); } @Test public void deleteTest() { insertTest(); UserDTO userDTO = new UserDTO(); userDTO.setId(id); userDTO.setName("deleted") .setStatus(0); int result = userMapper.updateByPrimaryKey(userDTO); System.out.println(result > 0); } @Test public void selectTest() { insertTest(); UserDTO userDTO = new UserDTO(); userDTO.setId(id); userDTO.setStatus(1); UserDTO result = userMapper.selectOne(userDTO); System.out.println(result); } }
service
IUserService:
UserService:
import com.zy.entity.UserDTO; import com.zy.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService extends BaseService<UserMapper, UserDTO> implements IUserService { @Autowired private UserMapper userMapper; public void deleteById(Long id) { UserDTO userDTO = new UserDTO(); userDTO.setId(id); userDTO.setStatus(0); userMapper.updateByPrimaryKey(userDTO); } }
服务接口
消费层(consumer)
搭建
依赖:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-openfeign-core</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.github.drtrang</groupId> <artifactId>druid-spring-boot2-starter</artifactId> </dependency> <dependency> <groupId>com.zy</groupId> <artifactId>cloud-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.zy</groupId> <artifactId>cloud-common-base</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.zy</groupId> <artifactId>cloud-server</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
配置:
server:
port: 8083
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka/
spring:
application:
name: consumer
main:
allow-bean-definition-overriding: true
datasource:
url: jdbc:mysql://127.0.0.1:3306/tktest
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
Api(消费接口)
Api接口:
该Api的Hystrix的实现:
这里我没做服务器降级的处理,加入了一条return以便后续验证调用是否成功
Controller
import com.zy.api.DataApi; import com.zy.entity.UserDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/consumer") public class ConsumerController { @Autowired private DataApi dataApi; @PostMapping("/insert") public String insertData(@RequestBody UserDTO userDTO){ return dataApi.insertData(userDTO); } }
启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients("com.zy.api") public class CloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(CloudConsumerApplication.class, args); } }
测试Demo
启动顺序:cloud-eureka-server ---> cloud-server ---> cloud-consumer
查看注册中心(Eureka):
说明服务和消费者都注册成功了,然后打开postman测试:
调用consumer的controller
打开数据库查看:
成功添加