8-创建用户注册服务
2025年9月13日大约 2 分钟SpringCloud Alibaba For Page项目
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>com.page</groupId>
<artifactId>page-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../page-dependencies/pom.xml</relativePath>
</parent>
<artifactId>page-service-reg</artifactId>
<packaging>jar</packaging>
<name>page-service-reg</name>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<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 Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
<exclusion>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
<exclusion>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Cloud End -->
<!-- Projects Begin -->
<dependency>
<groupId>com.page</groupId>
<artifactId>page-commons-service</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- Projects End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.page.service.reg.PageServiceRegApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application
package com.page.service.reg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = "com.page")
@MapperScan(basePackages = "com.page.commons.mapper")
@EnableBinding({Source.class})
@EnableAsync
@EnableSwagger2
public class PageServiceRegApplication {
public static void main(String[] args) {
SpringApplication.run(PageServiceRegApplication.class, args);
}
}
Controller
package com.page.service.reg.controller;
import com.page.commons.domain.TbUser;
import com.page.commons.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
@RequestMapping(value = "reg")
public class RegController {
@Autowired
private TbUserMapper tbUserMapper;
/**
* 根据 ID 测试查询用户信息
* @param id
* @return
*/
@GetMapping(value = {"{id}"})
public String reg(@PathVariable long id) {
TbUser tbUser = tbUserMapper.selectByPrimaryKey(id);
return tbUser.getUsername();
}
}
配置文件
- bootstrap.properties
spring.application.name=page-service-reg-config
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.server-addr=119.3.110.207:8848
logging.level.com.page=DEBUG
- bootstrap-prod.properties
spring.profiles.active=prod
spring.application.name=page-service-reg-config
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.server-addr=119.3.110.207:8848
Nacos Config
spring:
application:
name: page-service-reg
datasource:
druid:
url: jdbc:mysql://47.107.89.207:3306/myshop?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: cc123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: 119.3.110.207:8848
sentinel:
transport:
port: 8719
dashboard: 119.3.110.207:8081
server:
port: 9501
mybatis:
type-aliases-package: com.page.commons.domain
mapper-locations: classpath:mapper/*.xml
management:
endpoints:
web:
exposure:
include: "*"
SkyWalking
-javaagent:F:\page-cloud-alibaba\page-external-skywalking\agent\skywalking-agent.jar
-Dskywalking.agent.service_name=page-service-reg
-Dskywalking.collector.backend_service=119.3.110.207:11800