SpringBoot+Nacos
通过 Nacos Server 和 spring-cloud-starter-alibaba-nacos-config 实现配置的动态变更。
通过 Nacos Server 和 spring-cloud-starter-alibaba-nacos-discovery 实现服务的注册与发现。
配置中心,自定义DataId
我们要读取test-nacos命名空间下的DataId为 nacos-demo.yml的文件,Group是dev
读取nacos-demo.yml 文件里 的person.name 和 person.age属性
引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.properties
nacos.config.server-addr=127.0.0.1:8848
# 配置 nacos 命名空间,也就是那个 test-nacos 命名空间的 id
nacos.config.namespace=14dbb2cf-e01a-426e-8c28-f9d384371962
启动类上
@SpringBootApplication
// dataId : 指定我们要读取的 文件名(加后缀)
// groupId : 是group的名字
@NacosPropertySource(dataId = "nacos-demo.yml", groupId = "dev")
public class NacosSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(NacosSpringBootApplication.class, args);
}
}
写一个Controller测试一下
@RestController
public class HelloController {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private String age;
@GetMapping("/hello")
public String hello() {
return "hello" + name + "--" + age;
}
}
Ok,没问题
服务注册与发现
服务者
服务注册到 test-nacos 命名空间下
引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.properties:
# nacos 服务地址
nacos.discovery.server-addr=127.0.0.1:8848
# nacos 命名空间
nacos.discovery.namespace=14dbb2cf-e01a-426e-8c28-f9d384371962
# 服务名
spring.application.name=boot-provider
server.port=8080
写启动类
@SpringBootApplication
public class SpringBootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootProviderApplication.class, args);
}
}
一个Controller 实现 CommandLineRunner
注入NamingService接口
重写 run 方法
@RestController
public class MyController implements CommandLineRunner {
@NacosInjected
private NamingService namingService;
@Value("${spring.application.name}")
private String serverName;
@Value("${server.port}")
private Integer serverPort;
@Override
public void run(String... args) throws Exception {
/*
注册一个实例到 nacos server中
serverName 项目服务名称
"127.0.0.1" ip
serverPort: 端口
*/
namingService.registerInstance(serverName,"127.0.0.1", serverPort);
}
// 写了一个 hello http接口
@GetMapping("/hello")
public String hello() {
return "hello world";
}
}
启动项目后
发现 注册了一条 服务
访问一下 hello ,也没问题
消费者
引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.12.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.properties
nacos.discovery.server-addr=127.0.0.1:8848
nacos.discovery.namespace=14dbb2cf-e01a-426e-8c28-f9d384371962
spring.application.name=boot-consumer
server.port=8080
启动类
@SpringBootApplication
public class BootConsumerApplication {
// 反转 RestTemplate
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(BootConsumerApplication.class, args);
}
}
Controller调用 服务者的接口
@RestController
public class ConsumerController {
// Nacos 注入 NamingService 接口
@NacosInjected
private NamingService namingService;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
public String hello() {
String url = null;
try {
// 根据服务名从注册中心获取一个健康的服务实例
Instance instance = namingService.selectOneHealthyInstance("boot-provider");
// 将url 拼接一下
url = String.format("http://%s:%d/hello", instance.getIp(), instance.getPort());
} catch (NacosException e) {
e.printStackTrace();
}
// 使用 RestTemplate 远程调用
return restTemplate.getForObject(url, String.class);
}
}
如果想把 消费者 也注册到 Nacos Server
实现 CommandLineRunner
注入NamingService接口
重写 run 方法
参考 服务者 注册入 Nacos Server的方法实现
启动两个项目
访问消费者的 http接口 http://localhost:8080/hello
远程调用 成功,注意:
如果 服务不是注册到 public 命名空间,一定要在 消费者 项目的 配置文件里 声明是在哪个 命名空间
否则找不到,也要注意项目名(spring.application.name)