spring boot nacos注册微服务示例demo_亲测成功
先安装好Nacos
创建Maven项目
结构如图
例如项目名为: test-demo
下面有个子模块: test-demo-data-process
父模块pom.xml
<?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>test-demo</artifactId>
<groupId>com.beyond.demo</groupId>
<version>0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>demo数据服务</description>
<artifactId>test-demo-data-process</artifactId>
<packaging>pom</packaging>
<modules>
<module>test-demo-data-process-server</module>
</modules>
</project>
子模块pom.xml
<?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>test-demo-data-process</artifactId>
<groupId>com.beyond.demo</groupId>
<version>0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-demo-data-process-server</artifactId>
<dependencies>
<!-- javax.servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 增加feign-httpclient,使用连接池机制 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8080
management:
endpoints:
web:
exposure:
include: "*"
exclude:
endpoint:
health:
show-details: always
banner.txt
springboot项目启动时,控制台会打印自带的banner
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
Application Version: ${application.version}${application.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
bootstrap.yaml
注意: namespace和nacos上对应
spring:
# 选择加载不同环境的配置文件
profiles:
active: dev
application:
name: test-demo-data-process-server
---
#开发环境配置
spring:
profiles: dev,local
cloud:
nacos:
discovery:
server-addr: 192.168.3.1:8848
namespace: xxxx-8ef1-4393-xxxx-xxxxx
config:
server-addr: 192.168.3.1:8848
namespace: xxxx-8ef1-4393-xxxx-xxxxx
active-profiles: ${spring.profiles.active:dev}
fileExtension: yml
shared-dataids: cloud.yml,rabbitmq-mqtt.yml
refreshable-dataids: cloud.yml,rabbitmq-mqtt.yml
---
##公司环境配置
spring:
profiles: comp
cloud:
nacos:
discovery:
server-addr: 192.168.1.18:8848
namespace: xxxxx-8ef1-xxxx-b28f-xxxxx
config:
server-addr: 192.168.1.18:8848
namespace: xxxxx-8ef1-xxxx-b28f-xxxxx
active-profiles: ${spring.profiles.active:comp}
fileExtension: yml
shared-dataids: cloud.yml,rabbitmq-mqtt.yml
refreshable-dataids: cloud.yml,rabbitmq-mqtt.yml
---
##生产环境配置
spring:
profiles: prod
cloud:
nacos:
discovery:
server-addr: ${NACOS_HOST:10.10.70.89}:${NACOS_PORT:8848}
namespace: ${NAMESPACE:559d7cb0-xxx-xxx-xxxx-41843b0106ae}
config:
server-addr: ${NACOS_HOST:10.10.70.89}:${NACOS_PORT:8848}
namespace: ${NAMESPACE:559d7cb0-xxx-xxx-xxxx-41843b0106ae}
active-profiles: ${spring.profiles.active:prod}
fileExtension: yml
shared-dataids: cloud.yml,rabbitmq-mqtt.yml
refreshable-dataids: cloud.yml,rabbitmq-mqtt.yml
logback-spring.xml
logback日志配置
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="false" scanPeriod="60000" debug="false">
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
<property name="GLOBAL-LOG-LEVEL" value="ERROR" /> <!-- TRACE < DEBUG < INFO < WARN < ERROR -->
<property name="GLOBAL-LOG-PATH" value="log/" /> <!-- 日志输出路径变量 -->
<property name="GLOBAL-LOG-MODULE" value="${springAppName}" /> <!-- 日志文件命名变量,一般以模块名命名 -->
<!-- You can override this to have a custom pattern -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- Minimum logging level to be presented in the console logs-->
<level>DEBUG</level>
</filter>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<appender name="ROLLING-FILE-1" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${GLOBAL-LOG-PATH}/${GLOBAL-LOG-MODULE}-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<!-- 异步输出 -->
<appender name ="ASYNC" class= "ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失日志.默认的,如果队列的90%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>10</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>256</queueSize>
<!-- 添加附加的appender,最多只能添加一个 -->
<appender-ref ref ="ROLLING-FILE-1"/>
</appender>
<!-- Appender to log to file in a JSON format
<appender name="logstash" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}.json</file>;
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${GLOBAL-LOG-PATH}/${GLOBAL-LOG-MODULE}.json.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>UTC</timeZone>
</timestamp>
<pattern>
<pattern>
{
"severity": "%level",
"service": "${springAppName:-}",
"trace": "%X{X-B3-TraceId:-}",
"span": "%X{X-B3-SpanId:-}",
"parent": "%X{X-B3-ParentSpanId:-}",
"exportable": "%X{X-Span-Export:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"rest": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
-->
<!-- 日志级别最好每个logger节点都设置,不要引用传递root节点的日志级别,有利于性能 -->
<springProfile name="dev,comp">
<logger name="com.beyond" level="INFO"/>
<logger name="com.alibaba.nacos" level="ERROR"/>
<root level="${GLOBAL-LOG-LEVEL}">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC"/>
</root>
</springProfile>
<springProfile name="test,prod">
<logger name="com.beyond" level="ERROR"/>
<logger name="com.alibaba.nacos" level="ERROR"/>
<root level="${GLOBAL-LOG-LEVEL}">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC"/>
</root>
</springProfile>
</configuration>
DataApp.java
启动类
package com.beyond.demo.data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableScheduling
public class DataApp
{
public static void main(String[] args)
{
final SpringApplication sa = new SpringApplication(DataApp.class);
sa.addListeners(new ApplicationPidFileWriter("test-data-demo-server.pid"));
sa.run(args);
}
}
其他包的代码不做详细介绍了,都是业务逻辑相关代码