一、安装nacos:https://blog.csdn.net/HXNLYW/article/details/97101028
二、工程依赖nacos的jar包:
<!--springCloud-nacos-配置管理功能依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<!-- springCloud-nacos-服务发现功能依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
三、具体实现:
一)、nacos作为配置中心:
1).在bootstrap.yml配置:
spring:
profiles:
active: dev
application:
name: gourd
# springCloud
cloud:
# 配置中心
nacos:
config:
server-addr: 47.103.5.190:8848
file-extension: yaml
encode: UTF-8
management:
endpoints:
web:
exposure:
include: '*'
注意:
1)配置项需配置在bootstrap.yml中。
2)如果配置在了application.yml中,启动会报空指针:com.alibaba.nacos.api.exception.NacosException: null ;
3)application.yml和bootstrap.yml区别:https://blog.csdn.net/jeikerxiao/article/details/78914132
2).nacos中配置:
dataId 的完整格式如下:${prefix}-${spring.profile.active}.${file-extension}
prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
spring.profile.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。 注意:当 spring.profile.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。
3).在需要自动刷新配置的类上加上注解 @RefreshScope
二)、nacos作为服务注册发现中心(本文中服务的提供者和消费者是同一个工程项目):
1).配置信息(服务提供端和消费端):
spring:
profiles:
active: dev
application:
name: gourd
# springCloud
cloud:
# 服务发现
nacos:
discovery:
server-addr: 47.103.5.190:8848
2).启动类增加注解@EnableDiscoveryClient 开启服务注册发现功能
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableTransactionManagement
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
给RestTemplate实例添加 @LoadBalanced 注解,实现负载均衡。
3).消费端调用:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
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;
/**
* @author gourd
*/
@RestController
@RequestMapping("/nacos")
@Api(tags = "nacos", description = "NacosConsumer控制器")
@Slf4j
@RefreshScope
public class NacosConsumerController {
@Value("${spring.application.name:gourd}")
private String applicationName;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/test")
@ApiOperation("nacos测试接口")
public String testServer() {
String callServiceResult = restTemplate.getForObject( "http://"+applicationName+"/"+applicationName+"/gourd/test", String.class);
log.warn("返回结果:{}",callServiceResult);
return callServiceResult;
}
}
至此整合完成。