一,Nacos注册中心
1,服务提供者
(1)修改pom.xml文件
<!--nacos客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
(2)建立application.yml文件
server:
port: 8090
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.208.133 #naocs注册中心的地址
application:
name: nacos-provider #注册到nacos的服务名
###(3)配置启动类
@SpringBootApplication
@EnableDiscoveryClient//向注册中心注册该服务,并可以获取其他服务的调用地址
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class);
}
}
(4)创建服务
Controller层
@RestController
@RequestMapping("/provider")
public class ProviderController {
@Autowired
private UserService userService;
@RequestMapping("/getUserById/{id}")
public User getUserById(@PathVariable Integer id){
return userService.getUserById(id);
}
}
Service层
@Service
public class UserServiceImpl implements UserService {
@Override
public User getUserById(Integer id) {
return new User(id,"张三",18);
}
}
2,服务消费者
(1)修改pom.xml文件
<!--naocs注册中心的启动器-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
(2)建立application.yml文件
server:
port: 80
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.208.133 #nacos注册中心的地址
application:
name: nacos-consumer #注册到nacos的服务名
(3)配置启动类
@SpringBootApplication
@EnableDiscoveryClient //开启nacos,允许注册当前服务并发现其他服务
public class NacosConsumerApp {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApp.class,args);
}
}
(4)创建服务
Controller层
@RestController
@RequestMapping(value = "/consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
//springcloud提供的发现服务的工具栏
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/getUserById/{id}")
public User getUserById(@PathVariable Integer id){
List<String> serviceList = discoveryClient.getServices();
for (String service : serviceList) {
System.out.println(service);
}
ServiceInstance instance = discoveryClient.getInstances("nacos-provider").get(0);
//--问题:无法实现负载均衡(ip和port是硬编码)--
String url = "http://"+instance.getHost()+":"+instance.getPort()+"/provider/getUserById/"+id;
return restTemplate.getForObject(url, User.class);
}
}
3,测试
访问服务消费者返回的结果
二,配置中心
Nacos的配置中心特点:
1,动态配置消除了配置变更时重新部署应用和服务的需要,让配置管理变得更加高效和敏捷。
2,配置中心化管理让实现无状态服务变得更简单,让服务按需弹性扩展变得更容易。
1,pom.xml引入依赖
<!--nacos配置中心的启动器-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2,bootstrap.yml
客户端配置的文件必须为 bootstrap.yml
bootstrap.yml和 application.yml 都可以用来配置参数。
bootstrap.yml 用来程序引导时执行,应用于更加早期配置信息读取。可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。一旦bootstrap.yml 被加载,则内容不会被覆盖。
application.yml 可以用来定义应用级别的, 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
spring:
cloud:
nacos:
config:
server-addr: 192.168.145.131:8848 #nacos配置中心的地址
prefix: nacos-config #可省略,默认是${spring.application.name}
file-extension: yaml #后缀,目前仅支持 properties 和 yaml
namespace: test # namespace隔离
group: NACOS_GROUP #
#DataId语法:${spring.cloud.nacos.config.prefix}.${spring.cloud.nacos.config.file-extension}
注意:pringcloud 2020.0 默认不优先加载bootstrap.yml
解决方法:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.4</version>
</dependency>
3,App 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigApp {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApp.class);
4,Controller测试
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.RestController;
@RestController
/**
* @RefreshScope注解实现了Nacos配置的的动态更新
* 重新从BeanFactory获取一个新的实例(该实例使用新的配置)
*/
@RefreshScope
public class ConfigController {
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.type}")
private String type;
@GetMapping("/config/info")
public String getConfigInfo() {
System.out.println(this);
String configInfo = driverClassName+"<br>"+url+"<br>"+username+"<br>"
+password+"<br>"+type;
return configInfo;
}
}
5,在Nacos控制台添加配置信息
Data ID (配置文件的命名规则) 格式如下:
${spring.cloud.nacos.config.prefix}.${spring.cloud.nacos.config.file-extension}
说明:
- spring.cloud.nacos.config.prefix: 默认是当前服务的服务名称
- spring.cloud.nacos.config.file-extension: 配置文件的格式(后缀),目前仅支持 properties 和 yaml 。
server:
port: 80
spring:
cloud:
inetutils:
ignored-interfaces: 'VMware Virtual Ethernet Adapter for VMnet1,VMware Virtual Ethernet Adapter for VMnet8'
nacos:
discovery:
server-addr: 192.168.208.133:8848 #nacos服务的地址
application:
name: nacos-consumer #向注册中心注册的名字
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/health?characterEncoding=UTF-8
username: root
password: 1111
type: com.alibaba.druid.pool.DruidDataSource
6,测试
1,项目启动时到Nacos配置中心里查找配置文件并加载
配置文件在本机的位置
2, 修改配置文件后,Nacos监听到配置文件的MD5有变化就会推送消息到客户端,金额护短收到消息后会拉取最新配置
3,浏览器访问Controller’