nacos 注册中心示例

This commit is contained in:
YunaiV
2020-01-24 19:03:55 +08:00
parent 1f00348ea4
commit ac9fbbeab3
5 changed files with 96 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-44-nacos-discovery-demo</artifactId>
<dependencies>
<!-- 实现对 SpringMVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 实现对 Nacos 作为注册中心的自动化配置 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.4</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab44.nacosdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,40 @@
package cn.iocoder.springboot.lab44.nacosdemo.controller;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/demo")
public class DemoController {
@NacosInjected
private NamingService namingService;
private RestTemplate restTemplate = new RestTemplate();
@GetMapping("/provider")
public String provider() {
return "echo";
}
@GetMapping("/consumer")
public String consumer() throws IllegalStateException, NacosException {
// 获得实例
List<Instance> instances = namingService.getAllInstances("demo-application");
// 获得首个实例,进行调用
Instance instance = instances.stream().findFirst()
.orElseThrow(() -> new IllegalStateException("未找到对应的 Instance"));
// 执行请求
return restTemplate.getForObject("http://" + instance.toInetAddr() + "/demo/provider",
String.class);
}
}

View File

@@ -0,0 +1,12 @@
spring:
application:
name: demo-application # 应用名
nacos:
discovery:
server-addr: 127.0.0.1:18848 # Nacos 服务器地址
auto-register: true # 是否自动注册到 Nacos 中。默认为 false。
namespace: # 使用的 Nacos 的命名空间,默认为 null。
register:
group-name: DEFAULT_GROUP # 使用的 Nacos 服务分组,默认为 DEFAULT_GROUP。
cluster-name: cluster01 # 集群名,默认为空。

View File

@@ -18,6 +18,7 @@
<module>lab-44-nacos-config-demo-jasypt</module>
<module>lab-44-nacos-config-demo-actuator</module>
<module>lab-44-nacos-config-demo-multi</module>
<module>lab-44-nacos-discovery-demo</module>
</modules>