用户注册服务
在根目录新建一个模块business并添加到根目录的模块里
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ml.yompc.myshop.plus</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>business</artifactId>
<packaging>pom</packaging>
<url>http://www.yompc.ml</url>
<inceptionYear>2018-Now</inceptionYear>
<licenses>
<license>
<name>Apache 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>yompc</id>
<name>FengJia Liu</name>
<email>yom535@outlook.com</email>
</developer>
</developers>
<modules>
<module>business-reg</module>
</modules>
</project>
创建business-reg模块,并添加到business中
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ml.yompc.myshop.plus</groupId>
<artifactId>business</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>business-reg</artifactId>
<packaging>jar</packaging>
<url>http://www.yompc.ml</url>
<inceptionYear>2018-Now</inceptionYear>
<licenses>
<license>
<name>Apache 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>yompc</id>
<name>FengJia Liu</name>
<email>yom535@outlook.com</email>
</developer>
</developers>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Cloud Alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Apache Dubbo -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-kryo</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>ml.yompc.myshop.plus</groupId>
<artifactId>ums-admin-provider-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>ml.yompc.myshop.plus.business.BusinessRegApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
创建BusinessRegApplication
package ml.yompc.myshop.plus.business;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 16:41
*/
@SpringBootApplication
public class BusinessRegApplication {
public static void main(String[] args) {
SpringApplication.run(BusinessRegApplication.class,args);
}
}
测试一下能不能连上dubbo
创建controller
package ml.yompc.myshop.plus.business.controller;
import ml.yompc.myshop.plus.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 16:42
*/
@RestController
@RequestMapping(value = "echo")
public class EchoController {
@Reference(version = "1.0.0")
private EchoService echoService;
@GetMapping(value = "{string}")
public String echo(@PathVariable String string){
return echoService.Echo(string);
}
}
application.yml
spring:
application:
name: business-reg
main:
allow-bean-definition-overriding: true
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
cloud:
nacos:
discovery:
server-addr: nacos.yompc.ml:30848
dubbo:
scan:
base-packages: ml.yompc.myshop.plus.business.controller
protocol:
name: dubbo
port: -1
serialization: kryo
registry:
address: nacos://nacos.yompc.ml
port: 30848
server:
# ${random.int[9000,9999]} 随机端口
port: 9000
management:
endpoints:
web:
exposure:
include: "*"
访问服务查看效果
http://localhost:9000/echo/Dubbo
正式创建注册服务
在provider的api下创建UmsAdminService
package ml.yompc.myshop.plus.provider.api;
import ml.yompc.myshop.plus.provider.domain.UmsAdmin;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 22:46
*/
public interface UmsAdminService {
/**
* 新增用户
* @param umsAdmin
* @return
*/
int inster(UmsAdmin umsAdmin);
}
将provider-service中的domain移动到provider-api下
package ml.yompc.myshop.plus.provider.domain;
在provider-service中的service实现之前创建的接口
package ml.yompc.myshop.plus.provider.service;
import ml.yompc.myshop.plus.provider.api.UmsAdminService;
import ml.yompc.myshop.plus.provider.domain.UmsAdmin;
import ml.yompc.myshop.plus.provider.mapper.UmsAdminMapper;
import org.apache.dubbo.config.annotation.Service;
import javax.annotation.Resource;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 22:55
*/
@Service(version = "1.0.0")
public class UmsAdminServiceImpl implements UmsAdminService {
@Resource
UmsAdminMapper umsAdminMapper;
@Override
public int inster(UmsAdmin umsAdmin) {
return umsAdminMapper.insert(umsAdmin);
}
}
在测试类中写测试方法testInster()
断言
Assert.assertEquals(result,1);//断言result=1
事务
@Transactional//事务
@Rollback//回滚
package ml.yompc.myshop.plus.provider.tests;
import java.time.LocalDateTime;
import ml.yompc.myshop.plus.provider.api.UmsAdminService;
import ml.yompc.myshop.plus.provider.domain.UmsAdmin;
import ml.yompc.myshop.plus.provider.mapper.UmsAdminMapper;
import org.junit.Assert;
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.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 14:31
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
@Rollback
public class UmsAdminTests {
@Autowired
private UmsAdminMapper umsAdminMapper;
@Resource
private UmsAdminService umsAdminService;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Test
public void selectAll(){
List<ml.yompc.myshop.plus.provider.domain.UmsAdmin> umsAdmins = umsAdminMapper.selectAll();
umsAdmins.forEach(umsAdmin -> {
System.out.println(umsAdmin);
});
}
@Test
public void testInster(){
UmsAdmin umsAdmin = new UmsAdmin();
umsAdmin.setUsername("YOMPC");
umsAdmin.setPassword(passwordEncoder.encode("123456"));
umsAdmin.setIcon("");
umsAdmin.setEmail("");
umsAdmin.setNickName("");
umsAdmin.setNote("");
umsAdmin.setCreateTime(LocalDateTime.now());
umsAdmin.setLoginTime(LocalDateTime.now());
umsAdmin.setStatus(0);
int result = umsAdminMapper.insert(umsAdmin);
Assert.assertEquals(result,1);
}
}
注意密码要加密 先在POM中加入spring-cloud-starter-oauth2的依赖
之后新建一个configure包并添加Bean
密码加密
package ml.yompc.myshop.plus.provider.configure;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* @email yom535@outlook.com
* @author: 有民(yom535)
* @date: 2019/9/20
* @time: 23:03
*/
@Configuration
public class UmsAdminResourceConfiguration {
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}