Spring Cloud Alibaba: Nacos used as a registration center and distribution center

SpringBoot actual electricity supplier item mall (20k + star) Address: github.com/macrozheng/...

Summary

Spring Cloud Alibaba is committed to providing one-stop solutions for micro-service development, Nacos as one of its core components, can be used as registration centers and configuration centers, this article will detail its usage.

Nacos Profile

Nacos committed to helping you discover, configure, and micro-management services. Nacos provides a set of simple-to-use set of features that help you quickly realize dynamic service discovery, service configuration, service metadata and traffic management.

Nacos has the following characteristics:

  • Service discovery and health monitoring services: supports DNS and RPC-based service discovery, support for real-time health check services, preventing the host or service instance unhealthy send a request;
  • Dynamic configuration services: dynamic configuration service allows you to center, Externalization and dynamic way to manage all application environment configuration and service configuration;
  • Dynamic DNS service: DNS service supports dynamic routing weights, making it easier to achieve the intermediate layer load balancing, simple and more flexible DNS routing strategy, flow control, and data center network analysis service;
  • Service and metadata management: support for all services and metadata from the perspective of micro-managing data center platform construction services.

Use Nacos as a registration center

Install and run Nacos

  • We start with the official website to download Nacos, here to download the nacos-server-1.1.4.zipfile, Download: github.com/alibaba/nac...

  • Configuring JAVA_HOMEenvironment variables, configuration will not lead to not run Nacos;

JAVA_HOME=D:\developer\env\Java\jdk1.8.0_91
复制代码
  • Extracting installation package, run directly binunder the directory startup.cmd;

  • After a successful run, access http://localhost:8848/nacoscan view Nacos homepage, default account passwords are nacos.

Create an application to register Nacos

Let's demonstrate the features found under the service registration through the transformation of consul-user-service and consul-ribbon-service, mainly to the original application Consul registry support instead Nacos registry support.

  • Creating nacos-user-service module and nacos-ribbon-service module;

  • To use Spring Cloud Alibaba need to add the following components are arranged in pom.xml;

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
复制代码
  • Modify its dependencies, the original registration Consul dependency discovery changed Nacos of:
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
复制代码
  • Modify the configuration file application.yml, the Consul of registered discovery configuration to Nacos of:
server:
  port: 8206
spring:
  application:
    name: nacos-user-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址
management:
  endpoints:
    web:
      exposure:
        include: '*'
复制代码
  • Run two nacos-user-service and a nacos-ribbon-service, on Nacos page you can see the following information:

Load balancing

Since we ran two nacos-user-service, while nacos-ribbon-service default interface to call it, we call nacos-ribbon-service interface to demonstrate the load balancing function.

Multiple calls to the interface: HTTP: // localhost: 8308 / the User / 1 , can be found in two nacos-user-service console alternately print the following information.

2019-11-06 14:28:06.458  INFO 12092 --- [nio-8207-exec-2] c.macro.cloud.controller.UserController  : 根据id获取用户信息,用户名称为:macro
复制代码

Nacos use as a distribution center

We create nacos-config-client module and add the configuration information Nacos page to demonstrate under configuration management functions.

Creating nacos-config-client module

  • Add its dependencies in pom.xml:
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
复制代码
  • Add a profile application.yml, is configured to enable the dev environment:
spring:
  profiles:
    active: dev
复制代码
  • Add a profile bootstrap.yml, mainly for Nacos to be configured as a functional configuration of the center:
server:
  port: 9101
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos地址
      config:
        server-addr: localhost:8848 #Nacos地址
        file-extension: yaml #这里我们获取的yaml格式的配置
复制代码
  • Creating ConfigClientController, obtain configuration information from the configuration Nacos Center:
/**
 * Created by macro on 2019/9/11.
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}
复制代码

Add the configuration Nacos

  • Let's composition in terms of format Nacos under the dataid and attribute correspondence between the file and SpringBoot configuration:
${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
复制代码
  • For example, we want to get the application name for the nacos-config-clientapplication in the devenvironment of yamlthe configuration, dataid as follows:
nacos-config-client-dev.yaml
复制代码
  • Adding a configuration according to the above dataid:
config:
  info: "config info for dev"
复制代码
  • Fill configuration diagram:

config info for dev
复制代码

Nacos dynamic refresh configuration

As long as we modify the configuration information Nacos, call interface to check the configuration again, you will find the configuration has been refreshed, Nacos and Consul as support dynamic refresh configuration. When we modify the configuration and posted on Nacos page, the application will refresh the configuration and print the following information

2019-11-06 14:50:49.460  INFO 12372 --- [-localhost_8848] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$ec395f8e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-06 14:50:49.608  INFO 12372 --- [-localhost_8848] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'nacos-config-client-dev.yaml', group: 'DEFAULT_GROUP'
2019-11-06 14:50:49.609  INFO 12372 --- [-localhost_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='nacos-config-client-dev.yaml'}, NacosPropertySource {name='nacos-config-client.yaml'}]}
2019-11-06 14:50:49.610  INFO 12372 --- [-localhost_8848] o.s.boot.SpringApplication               : The following profiles are active: dev
2019-11-06 14:50:49.620  INFO 12372 --- [-localhost_8848] o.s.boot.SpringApplication               : Started application in 0.328 seconds (JVM running for 172.085)
2019-11-06 14:50:49.638  INFO 12372 --- [-localhost_8848] o.s.c.e.event.RefreshEventListener       : Refresh keys changed: [config.info]
复制代码

Reference material

Spring Cloud Alibaba official document: github.com/alibaba/spr...

To use the module

springcloud-learning
├── nacos-config-client -- 用于演示nacos作为配置中心的nacos客户端
├── nacos-user-service -- 注册到nacos的提供User对象CRUD接口的服务
└── nacos-service -- 注册到nacos的ribbon服务调用测试服务
复制代码

Project Source Address

github.com/macrozheng/…

No public

mall project a full tutorial serialized in public concern number the first time to obtain.

No public picture

Guess you like

Origin juejin.im/post/5dcbf7bc5188250d1f5a78ea