Skip to content

Commit

Permalink
add springboot-nacos
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwujing committed Jan 27, 2024
1 parent e85acf4 commit 6684f9d
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 0 deletions.
1 change: 1 addition & 0 deletions springboot-nacos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
87 changes: 87 additions & 0 deletions springboot-nacos/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<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>

<groupId>1.0.0</groupId>
<artifactId>springboot-nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springboot-nacos</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mysql-connector>5.1.44</mysql-connector>
<logback>1.2.3</logback>
<slf4j>1.7.25</slf4j>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>


<!--日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j}</version>
</dependency>


<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback}</version>
</dependency>



</dependencies>







</project>
28 changes: 28 additions & 0 deletions springboot-nacos/src/main/java/com/pancm/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.pancm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


/**
* @Author pancm
* @Description springboot集成nacos配置中心
* @Date 2024/1/26
* @Param
* @return
**/
@SpringBootApplication
@EnableDiscoveryClient
public class App
{
private static final Logger logger = LoggerFactory.getLogger(App.class);
public static void main( String[] args )
{
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(App.class, args);
logger.info("程序启动成功!");
}
}
42 changes: 42 additions & 0 deletions springboot-nacos/src/main/java/com/pancm/web/ConfigController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.pancm.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
* @Author pancm
* @Description 官方示例文档
* @Date 2024/1/26
* @Param
* @return
**/
@RestController
@RequestMapping("config")
@RefreshScope
public class ConfigController {



@Value("${pcm.name:pcm-1001}")
private String name;

@Value("${pcm.age:18}")
private String age;


@RequestMapping(value = "/getAge", method = GET)
public String getAge() {
return age;
}

@RequestMapping(value = "/getName", method = GET)
public String getName() {
return name;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pancm.web;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping("discovery")
public class DiscoveryController {

@NacosInjected
private NamingService namingService;

@RequestMapping(value = "/get", method = GET)
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
return namingService.getAllInstances(serviceName);
}
}
28 changes: 28 additions & 0 deletions springboot-nacos/src/main/resources/bootstrap.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
spring.banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
server.port=8299
spring.application.name=springboot-nacos
spring.profiles.active=dev


# nacos??
nacos.discovery.autoRegister=true
spring.cloud.nacos.discovery.server-addr= http://127.0.0.1:8848
spring.cloud.nacos.config.server-addr= http://127.0.0.1:8848
spring.cloud.nacos.config.namespace= pcm-namespace--id
spring.cloud.nacos.config.group= dev
spring.cloud.nacos.config.fileExtension= properties


# ??????
##???????????dataId
spring.cloud.nacos.config.extension-configs[0].data-id=pcm-dev.properties
##?????????????
spring.cloud.nacos.config.extension-configs[0].group=dev



32 changes: 32 additions & 0 deletions springboot-nacos/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds" >
<property name="LOG_HOME" value="logs"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<!-- each file should be at most 10MB, keep 31 days worth of history, but at most 10GB -->
<maxFileSize>10MB</maxFileSize>
<maxHistory>31</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</Pattern>
</layout>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE" />
</root>

</configuration>
1 change: 1 addition & 0 deletions springboot-nacos/src/main/resources/pcm.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pcm.age = 24

0 comments on commit 6684f9d

Please sign in to comment.