最近国内网站网站做的最好的是哪个,wordpress主题文章页面不显示,专业商城网站建设哪家便宜,西安网站优化推广公司一、什么是Nacos#xff1f;
官方介绍是这样的#xff1a; Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集#xff0c;帮助您实现动态服务发现、服务配置管理、服务及流量管理。 Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Na…一、什么是Nacos
官方介绍是这样的 Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集帮助您实现动态服务发现、服务配置管理、服务及流量管理。 Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构(例如微服务范式、云原生范式)的服务基础设施。 官方网址http://nacos.io
二、怎么搭建一个Nacos服务
1先下载一个Nacos服务中间件然后解压
最新版可以去官网下载Releases · alibaba/nacos · GitHub
2解压之后再bin目录下点击startup.cmd运行如果使用的是Linux系统则点击startup.sh
启动后界面如下 输入网址http://localhost:8848/nacos/#/login 登录名和密码都是nacos
登录后界面如下 其中【配置列表】可作为配置中心【服务列表】可作为注册中心
三、什么是配置中心什么是服务注册中心
1什么是配置中心
使用过SpringBoot项目的开发者都知道SpringBoot有一个application.yml或者application.properties作为配置文件里面有着各种各样的配置比如数据库的url、username等。但是这个配置文件在文件打包之后就无法修改了所以这时就需要一个配置中心来管理这些文件以达到配置动态更新的效果。
从微服务的角度来说微服务意味着要将单体应用中的业务拆分成一个个子服务每个服务的粒度相对较小因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行所以一套集中式的动态的配置管理设施是必不可少的。
2什么是注册中心
注册中心主要包括服务注册和服务发现可以说是微服务架构中的”通讯录“,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。
提高篇使用Nacos作为注册中心与配置中心
一、新建一个SpringBoot项目nacos_server
项目结构图如下 二、在pom.xml文件中添加如下内容 dependencies!--web组件--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--actuator--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!--SpringCloud-Nacos相关--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependencydependencygroupIdcom.alibaba.boot/groupIdartifactIdnacos-config-spring-boot-starter/artifactId/dependencydependencygroupIdcom.alibaba.boot/groupIdartifactIdnacos-config-spring-boot-actuator/artifactId/dependencydependencygroupIdcom.alibaba.nacos/groupIdartifactIdnacos-spring-context/artifactId/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency!--MySQL--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency
/dependencies 其中SpringCloud的版本为 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionGreenwich.SR2/versiontypepom/typescopeimport/scope
/dependency
三、在resources目录下新建application.yml与bootstarp.yml配置文件 bootstarp.yml与application.yml的区别 1、加载顺序 bootstrap.ymlbootstrap.properties先加载application.ymlapplication.properties后加载 2、配置区别 bootstrap.yml 可以理解成系统级别的一些参数配置这些参数一般是不会变动的。 application.yml 可以用来定义应用级别的如果搭配 spring-cloud-config 使用 application.yml 里面定义的文件可以实现动态替换。 bootstarp.yml内容如下 spring:cloud:nacos:discovery:server-addr: 127.0.0.1:8848 #服务注册地址
# ip: 127.0.0.1application:name: nacos_server application.yml内容如下 server:port: 9668
nacos:config:server-addr: 127.0.0.1:8848group: NAOCS-SPRING-CLOUD-SERVICEfile-extension: propertiesdata-id: mysql-config.properties
management:endpoints:web:exposure:include: * 四、在NacosServerApplication.java 启动类中加上EnableDiscoveryClient注解 五、在文件夹config下新建两个类DatabaseConfig与MybatisConfiguration
DatabaseConfig代码如下 package com.wzhi.config;import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import lombok.Data;
import org.springframework.stereotype.Component;/*** 从nacos配置中心中获取数据库连接配置** author wzhi*/
Component
Data
NacosPropertySource(dataId ${nacos.config.data-id}, groupId ${nacos.config.group}, autoRefreshed true)
public class DatabaseConfig {NacosValue(value ${driver_class_name}, autoRefreshed true)private String driverClassName;NacosValue(value ${url}, autoRefreshed true)private String url;NacosValue(value ${user_name}, autoRefreshed true)private String username;NacosValue(value ${password}, autoRefreshed true)private String password;} MybatisConfiguration代码如下 package com.wzhi.config;import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;import javax.sql.DataSource;/*** 配置Mybatis和连接池* author wzhi*/
Configuration
MapperScan(basePackages{com.wzhi.dao})
public class MybatisConfiguration {Autowiredprivate DatabaseConfig dataBaseConfig;Beanpublic DataSource dataSource() {HikariDataSource dataSource new HikariDataSource();System.out.println(获取到的配置文件内容为dataBaseConfig);dataSource.setDriverClassName(dataBaseConfig.getDriverClassName());dataSource.setJdbcUrl(dataBaseConfig.getUrl());dataSource.setUsername(dataBaseConfig.getUsername());dataSource.setPassword(dataBaseConfig.getPassword());return dataSource;}Beanpublic SqlSessionFactory sqlSessionFactoryBean() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource());ResourcePatternResolver resolver new PathMatchingResourcePatternResolver();
// sqlSessionFactoryBean.setMapperLocations(resolver.getResources(classpath:mybatis/mapper/*.xml));return sqlSessionFactoryBean.getObject();}} 运行之后有如下打印内容说明配置中心搭建成功 同时Nacos管理界面如下则说明服务已经注册