Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8e5798

Browse files
Merge pull request #177
* 📝 添加 MyBatisCodeHelper-Pro 鸣谢 * Merge branch 'dev' into master * Merge branch 'dev' into master * Merge branch 'dev' into master * Merge branch 'dev' into master * springboot nacos的demo,增加了pom依赖文件。 * SpringBoot整合nacos案例,增加了pom依赖文件。
1 parent dc519fb commit f8e5798

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

‎demo-nacos/pom.xml‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-boot-demo</artifactId>
7+
<groupId>com.xkcoding</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>demo-nacos</artifactId>
13+
14+
<dependencies>
15+
16+
<dependency>
17+
<groupId>com.alibaba.boot</groupId>
18+
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
19+
<version>${nacos.version}</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.alibaba.boot</groupId>
29+
<artifactId>nacos-config-spring-boot-starter</artifactId>
30+
<version>${nacos.version}</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.alibaba.boot</groupId>
35+
<artifactId>nacos-config-spring-boot-actuator</artifactId>
36+
<version>${nacos.version}</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
</dependencies>
46+
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.xkcoding.nacos;
2+
3+
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@NacosPropertySource(dataId = "example", autoRefreshed = true)
9+
public class SpringBootDemoNacosApplication {
10+
public static void main(String[] args) {
11+
SpringApplication.run(SpringBootDemoNacosApplication.class, args);
12+
}
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.xkcoding.nacos.config;
2+
3+
import com.alibaba.nacos.api.config.annotation.NacosValue;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
import static org.springframework.web.bind.annotation.RequestMethod.GET;
8+
9+
@Controller
10+
@RequestMapping("config")
11+
public class ConfigController {
12+
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
13+
private boolean useLocalCache;
14+
15+
/**
16+
* 1.通过nacos官网下载nacos服务,启动nacos服务。
17+
* 通过调用 Nacos Open API 向 Nacos server 发布配置:dataId 为example
18+
* curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=useLocalCache=true"
19+
* @return
20+
*/
21+
@RequestMapping(value = "/get", method = GET)
22+
@ResponseBody
23+
public boolean get() {
24+
return useLocalCache;
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.xkcoding.nacos.discover;
2+
3+
import com.alibaba.nacos.api.annotation.NacosInjected;
4+
import com.alibaba.nacos.api.exception.NacosException;
5+
import com.alibaba.nacos.api.naming.NamingService;
6+
import com.alibaba.nacos.api.naming.pojo.Instance;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
12+
import java.util.List;
13+
14+
import static org.springframework.web.bind.annotation.RequestMethod.GET;
15+
16+
@Controller
17+
@RequestMapping("discovery")
18+
public class DiscoveryController {
19+
20+
@NacosInjected
21+
private NamingService namingService;
22+
23+
@RequestMapping(value = "/get", method = GET)
24+
@ResponseBody
25+
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
26+
return namingService.getAllInstances(serviceName);
27+
}
28+
}
29+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nacos.config.server-addr=127.0.0.1:8848

‎pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<module>demo-https</module>
7171
<module>demo-flyway</module>
7272
<module>demo-pay</module>
73+
<module>demo-nacos</module>
7374
</modules>
7475
<packaging>pom</packaging>
7576

@@ -87,6 +88,7 @@
8788
<hutool.version>5.4.5</hutool.version>
8889
<guava.version>29.0-jre</guava.version>
8990
<user.agent.version>1.20</user.agent.version>
91+
<nacos.version>0.2.4</nacos.version>
9092
</properties>
9193

9294
<repositories>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /