nacos服务注册与发现及服务配置实现

Posted baby123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nacos服务注册与发现及服务配置实现相关的知识,希望对你有一定的参考价值。

Nacos 提供了一组简单易用的特性集,可快速实现动态服务发现、服务配置、服务元数据及流量管理。

更敏捷和容易地构建、交付和管理微服务平台。

关键特性:

  服务发现和服务健康监测

  动态配置服务

  动态 DNS 服务

  服务及其元数据管理

1.启动服务

下载 地址

解压

双击 startup.cmd

技术图片

2.服务端provider

(1)添加依赖

   <properties>
        <java.version>1.8</java.version>
        <nacos.version>2.1.1.RELEASE</nacos.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>

    </dependencies>

    <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>
        </dependencies>
    </dependencyManagement>

(2)添加配置

server.port=8010
spring.application.name=service-provider
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

(3)测试方法

package com.xyz.provider1.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {
    @RequestMapping("/hello")
    public String Hello(){
        return "hello, provider";
    }

}

再添加provider1,端口8011,测试方法输出

"hello,another provider"

3.客户端customer

(1)添加依赖

  <properties>
        <java.version>1.8</java.version>
        <nacos.version>2.1.1.RELEASE</nacos.version>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>
    </dependencies>

    <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>
        </dependencies>
    </dependencyManagement>

(2)添加配置

server.port=8015
spring.application.name=service-comsumer

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}

#service-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=properties

(3)测试方法

package com.xyz.comsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RefreshScope
@RestController
public class HelloController {
    @Autowired
    private LoadBalancerClient loadBalancer;
    @Autowired
    private DiscoveryClient discoveryClient;
    private String serviceName = "service-provider";

    /**
     * 获取所有服务
     */
    @RequestMapping("/services")
    public Object services() {
        return discoveryClient.getInstances(serviceName);
    }

    /**
     * 从所有服务中选择一个服务(轮询)
     */
    @RequestMapping("/discover")
    public Object discover() {
        return loadBalancer.choose(serviceName).getUri().toString();
    }

    @RequestMapping("/hello")
    public String hello() {
        ServiceInstance serviceInstance = loadBalancer.choose(serviceName);
        String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
        callServiceResult += ",throw service";
        return callServiceResult;
    }

}

4.网关Zuul

(1)添加依赖

<properties>
        <java.version>1.8</java.version>
        <nacos.version>2.1.1.RELEASE</nacos.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>

    </dependencies>

(2)添加配置

server.port=8090

spring.application.name=service-zuul

spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

zuul.routes.api.path=/api/**
zuul.routes.api.serviceId=service-provider

(3)启动类

package com.xyz.zuul;

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

@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {

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

}

4.测试

启动provider,provider1,customer

http://localhost:8848/nacos/   ,用户名nacos,密码nacos

技术图片

http://localhost:8090/api/hello

交替返回

hello,provider或hello,another provider

5.修改客户端,添加统一配置

(1)修改添加依赖

<properties>
        <java.version>1.8</java.version>
        <nacos.version>2.1.1.RELEASE</nacos.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>

    </dependencies>

    <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>
        </dependencies>
    </dependencyManagement>

(2)将配置写入 bootstrap.properties

server.port=8015
spring.application.name=service-comsumer

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=properties

(3)测试方法

package com.xyz.comsumer.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class HelloController {

    @Value("${hello-string}")
    private String helloString;
    
    @RequestMapping("/configTest")
    public String configTest() {
        return helloString;
    }
}

登录http://localhost:8848/nacos

添加配置service-comsumer.properties

hello-string=customer

重新启动customer

http://localhost:8015/configTest

输出

  customer

修改配置后,刷新会立即生效

 

以上是关于nacos服务注册与发现及服务配置实现的主要内容,如果未能解决你的问题,请参考以下文章

深入浅出Nacos原理及调优「原理分析专题」服务注册与发现相关的原理分析

服务注册与发现

SpringCloud学习之NACOS实现服务的注册与发现

71 nacos 实现微服务的注册与发现

使用Nacos作为分布式注册中心,实现服务注册与发现

Alibaba中间件技术系列「Nacos技术专题」服务注册与发现相关的原理分析