SpringCloud集成Nacos的使用小结

Nacos 注册中心

Nacos 是 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

Nacos服务安装与启动

安装地址: https://github.com/alibaba/nacos/releases

  • 本文在Windows环境测试,下载zip版,解压。
  • 配置conf目录下的application.properties文件,配置nacos数据源,端口等。
#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacos
### Default web server port:
server.port=8848

#*************** Network Related Configurations ***************#
### If prefer hostname over ip for Nacos server addresses in cluster.conf:
# nacos.inetutils.prefer-hostname-over-ip=false

### Specify local server's IP:
# nacos.inetutils.ip-address=

#*************** Config Module Related Configurations ***************#
### If user MySQL as datasource:
spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?serverTimezone=UTC&characterEncoding=utf8
db.user=root
db.password=123456

#*************** Naming Module Related Configurations ***************#
### Data dispatch task execution period in milliseconds:
# nacos.naming.distro.taskDispatchPeriod=200

### Data count of batch sync task:
# nacos.naming.distro.batchSyncKeyCount=1000

### Retry delay in milliseconds if sync task failed:
# nacos.naming.distro.syncRetryDelay=5000

初始化数据库,脚本位于conf目录下nacos-mysql.sql

启动nacos服务,在cmd中执行bin目录下startup.cmd:

D:\>cd nacos/bin

D:\nacos\bin>startup.cmd

     ,--.
    ,--.'|
  ,--,: : |                      Nacos 1.2.1
,`--.'`| ' :            ,---.        Running in stand alone mode, All function modules
|  : : | |           '  ,'\  .--.--.  Port: 8848
:  |  \ | : ,--.--.   ,---. /  /  | / /  '  Pid: 20700
|  : ' '; | /    \  /   \.  ; ,. :| : /`./  Console: http://10.118.37.75:8848/nacos/index.html
'  ' ;.  ;.--. .-. | /  / ''  | |: :| : ;_
|  | | \  | \__\/: . ..  ' / '  | .; : \ \  `.   https://nacos.io
'  : | ; .' ," .--.; |'  ; :__|  :  | `----.  \
|  | '`--' / / ,. |'  | '.'|\  \ / / /`--' /
'  : |   ; :  .'  \  :  : `----' '--'.   /
;  |.'   | ,   .-./\  \ /      `--'---'
'---'    `--`---'   `----'

2020-05-07 16:09:06,639 INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$d2a494f3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-05-07 16:09:06,713 INFO Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-05-07 16:09:06,715 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@2de56eb2' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-05-07 16:09:06,717 INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$f77937a5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-05-07 16:09:06,721 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-05-07 16:09:07,176 INFO Tomcat initialized with port(s): 8848 (http)

2020-05-07 16:09:07,280 INFO Root WebApplicationContext: initialization completed in 2308 ms

2020-05-07 16:09:16,998 INFO Initializing ExecutorService 'applicationTaskExecutor'

2020-05-07 16:09:17,138 INFO Adding welcome page: class path resource [static/index.html]

2020-05-07 16:09:17,390 INFO Creating filter chain: Ant [pattern='/**'], []

2020-05-07 16:09:17,418 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@163d04ff, org.springframework.security.web.context.SecurityContextPersistenceFilter@1698fc68, org.springframework.security.web.header.HeaderWriterFilter@56a4479a, org.springframework.security.web.csrf.CsrfFilter@565b064f, org.springframework.security.web.authentication.logout.LogoutFilter@53d1b9b3, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@207b8649, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@57eda880, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7c209437, org.springframework.security.web.session.SessionManagementFilter@20a8a64e, org.springframework.security.web.access.ExceptionTranslationFilter@73163d48]

2020-05-07 16:09:17,502 INFO Exposing 2 endpoint(s) beneath base path '/actuator'

2020-05-07 16:09:17,528 INFO Initializing ExecutorService 'taskScheduler'

2020-05-07 16:09:17,639 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2020-05-07 16:09:17,645 INFO Nacos logs files: D:\nacos\logs\

2020-05-07 16:09:17,646 INFO Nacos conf files: D:\nacos\conf\

2020-05-07 16:09:17,646 INFO Nacos data files: D:\nacos\data\

2020-05-07 16:09:17,646 INFO Nacos started successfully in stand alone mode.

2020-05-07 16:09:17,851 INFO Initializing Servlet 'dispatcherServlet'

2020-05-07 16:09:17,858 INFO Completed initialization in 6 ms

Nacos平台

访问路径:http://localhost:8848/nacos

可以新建多个环境,项目中通过命名空间映射相应的环境配置

spring:
 profiles:
  active: dev
 datasource:
  driverClassName: com.mysql.cj.jdbc.Driver
  jdbc-url: jdbc:mysql://106.13.181.6:3306/demo
  username: root
  password: 123456

新增配置后发布

SpringCloud项目配置

依赖配置

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   <version>2.1.1.RELEASE</version>
 </dependency>
 <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
   <version>2.1.1.RELEASE</version>
 </dependency>

通过bootstrap.yml配置启动,bootstrap-dev.yml配置文件如下:

spring:
 application:
  name: demo
 cloud:
  nacos:
   server-addr: 127.0.0.1:8848
   config:
    namespace: 9c6b8156-d045-463d-8fe6-4658ce78d0cc
    file-extension: yml

启动类配置

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

数据源配置

package com.example.demo.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;

@Configuration
public class SqlSessionConfig {

  private Logger logger = LoggerFactory.getLogger(SqlSessionConfig.class);

  @Bean("datasource")
  @ConfigurationProperties(prefix="spring.datasource")
  public DataSource masterDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean("mySqlSessionFactoryBean")
  public MybatisSqlSessionFactoryBean createSqlSessionFactory(@Qualifier("datasource") DataSource dataSource,
                                @Qualifier("paginationInterceptor") PaginationInterceptor paginationInterceptor) {
    // MybatisSqlSessionFactory
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = null;
    try {
      // 实例SessionFactory
      sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
      // 配置数据源
      sqlSessionFactoryBean.setDataSource(dataSource);
      // 设置 MyBatis-Plus 分页插件
      Interceptor [] plugins = {paginationInterceptor};
      sqlSessionFactoryBean.setPlugins(plugins);
      // 加载MyBatis配置文件
      PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
      sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:mapper/*.xml"));
    } catch (Exception e) {
      logger.error("创建SqlSession连接工厂错误:{}", e.getMessage());
    }
    return sqlSessionFactoryBean;
  }

  @Bean
  public MapperScannerConfigurer myGetMapperScannerConfigurer() {
    MapperScannerConfigurer myMapperScannerConfigurer = new MapperScannerConfigurer();
    myMapperScannerConfigurer.setBasePackage("com.example.demo.mapper");
    myMapperScannerConfigurer.setSqlSessionFactoryBeanName("mySqlSessionFactoryBean");
    return myMapperScannerConfigurer;
  }
}

controller

package com.example.demo.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.bean.UserDTO;
import com.example.demo.constants.SexEnum;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping(value = "/demo")
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping(value = "/create", method = RequestMethod.POST)
  public String create(@RequestBody UserDTO userDTO){
    User user = new User();
    user.setName(userDTO.getName());
    user.setPhone(userDTO.getPhone());
    user.setCreateTime(LocalDateTime.now());
    user.setEnable(true);
    user.setVersion(1L);
    user.setSex(SexEnum.getSexEnum(userDTO.getSex()));
    userService.insert(user);
    return "ok";
  }
}

启动项目

2020-05-08 15:37:26.384 INFO 12544 --- [      main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$9ea5255e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.2.6.RELEASE)

2020-05-08 15:37:26.601 INFO 12544 --- [      main] c.a.c.n.c.NacosPropertySourceBuilder   : Loading nacos data, dataId: 'demo', group: 'DEFAULT_GROUP', data: spring:
 profiles:
  active: dev
 datasource:
  driverClassName: com.mysql.cj.jdbc.Driver
  jdbc-url: jdbc:mysql://106.13.181.6:3306/demo
  username: root
  password: 123456
2020-05-08 15:37:26.608 WARN 12544 --- [      main] c.a.c.n.c.NacosPropertySourceBuilder   : Ignore the empty nacos configuration and get it based on dataId[demo.yml] & group[DEFAULT_GROUP]
2020-05-08 15:37:26.612 WARN 12544 --- [      main] c.a.c.n.c.NacosPropertySourceBuilder   : Ignore the empty nacos configuration and get it based on dataId[demo-dev.yml] & group[DEFAULT_GROUP]
2020-05-08 15:37:26.612 INFO 12544 --- [      main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='demo-dev.yml'}, NacosPropertySource {name='demo.yml'}, NacosPropertySource {name='demo'}]}
2020-05-08 15:37:26.616 INFO 12544 --- [      main] com.example.demo.DemoApplication     : The following profiles are active: dev
2020-05-08 15:37:26.984 WARN 12544 --- [      main] o.m.s.mapper.ClassPathMapperScanner   : Skipping MapperFactoryBean with name 'userMapper' and 'com.example.demo.mapper.UserMapper' mapperInterface. Bean already defined with the same name!
2020-05-08 15:37:26.984 WARN 12544 --- [      main] o.m.s.mapper.ClassPathMapperScanner   : No MyBatis mapper was found in '[com.example.demo.mapper]' package. Please check your configuration.
2020-05-08 15:37:26.985 INFO 12544 --- [      main] o.s.c.a.ConfigurationClassPostProcessor : Cannot enhance @Configuration bean definition 'sqlSessionConfig' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2020-05-08 15:37:27.004 INFO 12544 --- [      main] o.s.cloud.context.scope.GenericScope   : BeanFactory id=118dc0e1-e30f-3c3e-b857-cf20ba8b4123
2020-05-08 15:37:27.131 INFO 12544 --- [      main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$9ea5255e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-05-08 15:37:27.302 INFO 12544 --- [      main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-08 15:37:27.309 INFO 12544 --- [      main] o.a.coyote.http11.Http11NioProtocol   : Initializing ProtocolHandler ["http-nio-8080"]
2020-05-08 15:37:27.309 INFO 12544 --- [      main] o.apache.catalina.core.StandardService  : Starting service [Tomcat]
2020-05-08 15:37:27.309 INFO 12544 --- [      main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-05-08 15:37:27.434 INFO 12544 --- [      main] o.a.c.c.C.[Tomcat].[localhost].[/]    : Initializing Spring embedded WebApplicationContext
2020-05-08 15:37:27.435 INFO 12544 --- [      main] o.s.web.context.ContextLoader      : Root WebApplicationContext: initialization completed in 810 ms
 _ _  |_ _ _|_. ___ _ |  _
| | |\/|_)(_| | |_\ |_)||_|_\
   /        |
            3.3.1
2020-05-08 15:37:27.822 WARN 12544 --- [      main] c.n.c.sources.URLConfigurationSource   : No URLs will be polled as dynamic configuration sources.
2020-05-08 15:37:27.823 INFO 12544 --- [      main] c.n.c.sources.URLConfigurationSource   : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-05-08 15:37:27.825 WARN 12544 --- [      main] c.n.c.sources.URLConfigurationSource   : No URLs will be polled as dynamic configuration sources.
2020-05-08 15:37:27.826 INFO 12544 --- [      main] c.n.c.sources.URLConfigurationSource   : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-05-08 15:37:27.951 INFO 12544 --- [      main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-08 15:37:28.151 INFO 12544 --- [      main] o.s.s.c.ThreadPoolTaskScheduler     : Initializing ExecutorService
2020-05-08 15:37:28.391 INFO 12544 --- [      main] o.a.coyote.http11.Http11NioProtocol   : Starting ProtocolHandler ["http-nio-8080"]
2020-05-08 15:37:28.409 INFO 12544 --- [      main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-08 15:37:28.549 INFO 12544 --- [      main] c.a.c.n.registry.NacosServiceRegistry  : nacos registry, DEFAULT_GROUP demo 10.118.37.75:8080 register finished
2020-05-08 15:37:28.618 INFO 12544 --- [      main] com.example.demo.DemoApplication     : Started DemoApplication in 2.941 seconds (JVM running for 3.791)
2020-05-08 15:37:33.589 INFO 12544 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]    : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-08 15:37:33.590 INFO 12544 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet    : Initializing Servlet 'dispatcherServlet'
2020-05-08 15:37:33.597 INFO 12544 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet    : Completed initialization in 5 ms
2020-05-08 15:37:33.663 INFO 12544 --- [nio-8080-exec-2] com.example.demo.config.LogAspect    : 

测试

2020-05-08 16:05:13.994 INFO 12544 --- [nio-8080-exec-4] com.example.demo.config.LogAspect    :
 请求信息:
【请求地址】:/demo/create
【请求头】:content-type = application/json, user-agent = PostmanRuntime/7.24.0, accept = */*, postman-token = fc010815-8e7b-4e11-9fed-3d061f388faf, host = localhost:8080, accept-encoding = gzip, deflate, br, connection = keep-alive, content-length = 49
【请求方法】:String com.example.demo.controller.UserController.create(UserDTO)
【请求参数】:[UserDTO(id=null, name=zhangsan, phone=17751033130, sex=1)]
2020-05-08 16:05:14.004 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@2bacd020 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.008 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@5283f4ad (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.009 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@1fe3130d (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.010 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@11977567 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.011 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@641256c2 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.013 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@29f9f071 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.015 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@2e05f538 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.016 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@41391d2 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.017 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@72c163d4 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.019 WARN 12544 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase     : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@145d7f85 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-05-08 16:05:14.545 DEBUG 12544 --- [nio-8080-exec-4] c.example.demo.mapper.UserMapper.insert : ==> Preparing: INSERT INTO user ( name, sex, phone, create_time, enable, version ) VALUES ( ?, ?, ?, ?, ?, ? )
2020-05-08 16:05:14.548 DEBUG 12544 --- [nio-8080-exec-4] c.example.demo.mapper.UserMapper.insert : ==> Parameters: zhangsan(String), MAN(String), 17751033130(String), 2020-05-08T16:05:13.995(LocalDateTime), true(Boolean), 1(Long)
2020-05-08 16:05:14.686 DEBUG 12544 --- [nio-8080-exec-4] c.example.demo.mapper.UserMapper.insert : <==  Updates: 1
2020-05-08 16:05:14.697 INFO 12544 --- [nio-8080-exec-4] com.example.demo.config.LogAspect    :
 执行结果:
【响应结果】:"ok"
【执行耗时】:701毫秒

到此这篇关于SpringCloud集成Nacos的使用小结的文章就介绍到这了,更多相关SpringCloud集成Nacos内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springboot集成nacos的配置方法

    本文介绍了springboot集成nacos的配置方法,分享给大家,具体如下: nacos仓库:https://github.com/alibaba/nacos nacos介绍文档:https://nacos.io/zh-cn/docs/architecture.html nacos使用例子:https://github.com/nacos-group/nacos-examples springboot配置-集成nacos,例子代码下载 springboot-nacos-consumer spr

  • SpringBoot集成Nacos的详细教程

    一.环境说明 1.CentOS7 2.Jdk1.8 3.Mysql5.7 4.Nacos1.3 5.SpringBoot2.3.1.RELEASE 6.Maven3.6 二.下载Nacos 1.Nacos官网:https://nacos.io/zh-cn/index.html 2.Nacos下载地址:https://github.com/alibaba/nacos/releases/download/1.3.0/nacos-server-1.3.0.tar.gz 三.解压启动Nacos 1.解压

  • SpringBoot集成nacos动态刷新数据源的实现示例

    前言 因为项目需要,需要在项目运行过程中能够动态修改数据源(即:数据源的热更新).这里以com.alibaba.druid.pool.DruidDataSource数据源为例 第一步:重写DruidAbstractDataSource类 这里为什么要重写这个类:因为DruidDataSource数据源在初始化后,就不允许再重新设置数据库的url和userName public void setUrl(String jdbcUrl) { if (StringUtils.equals(this.jd

  • SpringCloud集成Nacos的使用小结

    Nacos 注册中心 Nacos 是 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现. Nacos服务安装与启动 安装地址: https://github.com/alibaba/nacos/releases 本文在Windows环境测试,下载zip版,解压. 配置conf目录下的application.properties文件,配置nacos数据源,端口等. #*************** Spri

  • 关于springcloud集成nacos遇到的问题

    目录 springcloud集成nacos遇到的问题 1.获取不到配置文件信息 2.springcloud 服务注册不上服务列表 springcloud无法注册Nacos 这是官方推荐一起使用的版本 赶紧去pom中改成2.2.1.RELEASE,加载后重新运行 springcloud集成nacos遇到的问题 1.获取不到配置文件信息 有时候新建了配置文件后浏览器访问发现获取不到里面的值,原来springcloud对应的nacos配置中心配置Data ID是需要加后缀名的 在 Nacos Spri

  • Springcloud seata nacos环境搭建过程图解

    最近学习了一下seata,由于nacos现在也挺火,于是学习了seata注册到nacos,然后集成springcloud 1.nacos配置(自行上官网下载) 将nacos/conf/nacos-mysql.sql导入自己的数据库 2.配置修改nacos/conf/application.properties spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?character

  • springcloud alibaba nacos linux配置的详细教程

    首先从github上下载nacos的压缩包:https://github.com/alibaba/nacos/releases 下载完成之后,通过WinSCP把文件传到linux服务器上 接着通过tar -zxvf命令将此压缩包解压 解压完成之后,进入conf目录下的 clusmter.conf文件打开并在里面加上 通过:wq命令保存退出 接着通过vim命令进入startup.sh 此处修改完成之后,找到这个文件最下面的位置 添加红框中的相关配置,保存退出 接着进入nginx的conf文件中 找

  • spring cloud如何集成nacos配置中心

    目录 spring cloud集成nacos配置中心 一.添加依赖 二.添加bootstrap.yml配置文件 三.添加远程配置 nacos作为SpringCloud配置中心 一.背景介绍 二.项目实战 三.总结 spring cloud集成nacos配置中心 一.添加依赖 <properties> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <nacos.version>0.9

  • SpringCloud安装Nacos完成配置中心

    目录 1. Nacos介绍 2. docker安装Nacos 2.1 docker-compose.yaml 2.2 启动后访问控制台 3.Springboot集成Nacos 3.1 pom依赖 3.2 yaml配置 3.3 测试配置动态化 3.4 测试日志 eureka注册中心 1. Nacos介绍 官网说明:Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服务元数据及流量管理. 2. docker安装Nacos 基

  • 详解MybatisPlus集成nacos导致druid连接不上数据库

    问题 mp加密与druid和nacos结合,首次项目启动成功,后续访问无法连接数据库 导致原因 项目首次加载由于会去nacos读取一遍配置,刚好mp启动的时候也会去读取配置好key值,所以启动的时候不会报错 由于nacos有自动刷新配置功能,后面自动刷新的时候mp不会再读取命令行配置key,导致无法解密,从而连接数据库失败 解决方案 知道原因之后,我们可以修改druid连接数据库的配置,因为druid自带数据库加解密,参考ConfigFilter类就可以知道,druid会去读取外部的配置文件,可

随机推荐