Nacos【Dynamic Naming and Configuration Service】提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
1.搭建Nocos服务
01.下载服务
下载地址:Releases · alibaba/nacos · GitHub
?在window环境学习我下载了zip包
02.生成数据库
03.修改文件
a.编辑application
b.复制文件
复制cluster.conf.example 去掉后缀【.example】生成配置文件
04.执行启动
?启动成功
05.访问服务后台
在浏览器上访问? ? ?
?账号密码默认为:nacos?
?Nacos环境搭建完成
2.创建服务
01.创建父级
a.创建服务
b.添加依赖
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<nacos.version>0.2.2.RELEASE</nacos.version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
02.创建提供者服务模块
a.创建服务
?b.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
为了springboot版本和springcloud版本兼容,这里我修改了
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
?c.添加注解
@EnableDiscoveryClient
d.添加配置?
server.port=8087
spring.application.name=provider
spring.cloud.nacos.discovery.server-addr=192.168.110.179:8848
?e.创建接口提供服务
@RestController
public class ProviderController {
@GetMapping("/helloProvider")
public String helloProvider(){
return "你好,我是提供服务者";
}
}
03.创建消费者服务
a.创建服务
b.添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.7.6</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 服务调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
为了springboot版本和springcloud版本兼容,这里我修改了
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
c.添加注解
@EnableDiscoveryClient
@EnableFeignClients
d.添加配置
server.port=8099
spring.application.name=consumer
spring.cloud.nacos.discovery.server-addr=192.168.110.179:8848
e.创建service
@FeignClient(name = "provider", fallback = ConsumerServiceImpl.class)
@Service
public interface ConsumerService {
@GetMapping("/helloProvider")
String getHello();
}
@Component
public class ConsumerServiceImpl implements ConsumerService {
@Override
public String getHello() {
return "服务出错";
}
}
f.创建接口
@RestController
public class ConsumerController {
@Resource
private ConsumerService consumerService;
@GetMapping("/getHello")
public String getHello(){
return consumerService.getHello();
}
}
3.运行
把创建的提供者服务与消费者服务运行起来,再回到nacos服务管理后台页面看
我们会看到有两个服务在nacos服务上注册成功了?
4.调用服务
我们创建一个网页访问消费者服务的接口
?我们会发现成功调用了提供者服务
5.配置中心接入
在这里我把消费者实现配置中心功能
01.创建配置项
在nacos服务后台中的配置列表中创建一个配置文件相当于application.properties或者yaml等配置
?这里我使用的是application.properties并配置了
?2.添加依赖
<!-- **************************************************** -->
<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.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!-- **************************************************** -->
3.添加注解
@NacosPropertySource(dataId = "test-dev.properties", autoRefreshed = true)
?4.添加配置
spring.cloud.nacos.config.server-addr=192.168.110.179:8848
spring.profiles.active=dev
spring.cloud.nacos.config.file-extension=properties
spring.cloud.nacos.config.group=DEFAULT_GROUP
?5.修改接口
@RestController
@RefreshScope
@Validated
public class ConsumerController {
@NacosValue(value = "${datasource.url:123}", autoRefreshed = true)
private String url;
@NacosValue(value = "${datasource.username:123}", autoRefreshed = true)
private String username;
@NacosValue(value = "${datasource.password:123}", autoRefreshed = true)
private String password;
@Autowired
private RestTemplate restTemplate;
@Resource
private ConsumerService consumerService;
@GetMapping("/getHello")
public String getHello(){
System.out.println(url);
System.out.println(username);
System.out.println(password);
return consumerService.getHello();
}
}
6.启动调用
01.访问接口
?02.监听输出
服务输出为
?03.修改服务配置
?04.访问接口
05.监听输出
服务输出为
项目源码
链接:https://pan.baidu.com/s/12yC3cjK12_izzFO9imcPjQ? 提取码:k5sj
|