java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springcloud nacos赋值均衡和动态刷新

springcloud nacos的赋值均衡和动态刷新

作者:健康平安的活着

nacos是一个分布式的配置中心和注册发现中心,这篇文章主要介绍了springcloud nacos的赋值均衡和动态刷新,需要的朋友可以参考下

一 nacos

1.1 nacos作用介绍

nacos是一个分布式的配置中心和注册发现中心。

nacos是 dynamic naming configuration service

nacos=config+bus 实现动态刷新;nacos=consul

1.2 各个注册中心对比

注册中心CAP模型控制台管理社区活跃度
EureakaAp支持
zkcp不支持
consulcp支持
nacosAP,和cp支持

二 Nacos的安装

2.1 nacos的安装搭建

1.本次安装版本为2.2.3,下载软件包,进行解压启动,输入命令

E:\nacos-server2.2.3\bin>startup.cmd -m standalone

2.截图

3.访问地址

http://localhost:8848/nacos

三 服务端的注册到Nacos

3.1 流程说明以及注意事项

1.客户端:ms-alibaba-provider8081

2.服务端:ms-alibaba-provider9091

3.注意事项:请求服务端的地址 不能只写 服务名称:ms-alibaba-provider9091,否则请求不成功

需要改成如下:http://ms-alibaba-provider9091

3.2 客户端配置

1.pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.jurf.ms</groupId>
        <artifactId>mscloud-demo-2024</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.jurf.ms.alibaba.consumer8081</groupId>
    <artifactId>ms-alibaba-consumer-8081</artifactId>
    <packaging>jar</packaging>
    <name>ms-alibaba-consumer-8081</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- 引入自己定义的api通用包 -->
<!--        <dependency>-->
<!--            <groupId>com.jurf.ms.api</groupId>-->
<!--            <artifactId>ms-api-commons</artifactId>-->
<!--            <version>1.0-SNAPSHOT</version>-->
<!--        </dependency>-->
        <!--openfeign-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-openfeign</artifactId>-->
<!--        </dependency>-->
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--loadbalancer-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <!--web + actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-actuator</artifactId>-->
<!--        </dependency>-->
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

2.application配置

server:
  port: 8081
spring:
  application:
    name: ms-alibaba-consumer-8081
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
#消费者将要去访问的微服务名称(nacos微服务提供者叫什么你写什么)
service-url:
  nacos-user-service: http://ms-alibaba-provider9091
 

3.业务

a)controller

package com.jurf.ms.alibaba.consumer8081.controller;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
 * @auther zzyy
 * @create 2024-01-01 15:43
 */
@RestController
public class OrderNacosController
{
    @Resource
    private RestTemplate restTemplate;
    @Value("${service-url.nacos-user-service}")
    private String serverURL;
    @GetMapping(value = "/consumer/pay/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Integer id)
    {
        System.out.println("进来了要。。。。。");
       // String serverUrl="http://localhost:9091";
        String result = restTemplate.getForObject(serverURL + "/pay/nacos/"+id, String.class);
        return result+"\t"+"    我是OrderNacosController83调用者。。。。。。";
       // return "\t"+"    我是OrderNacosController83调用者。。。。。。";
    }
}
 

b)config

package com.jurf.ms.alibaba.consumer8081.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
 * @auther zzyy
 * @create 2024-01-01 15:42
 */
@Configuration
public class RestTemplateConfig
{
    @Bean
    @LoadBalanced //赋予RestTemplate负载均衡的能力
    public RestTemplate restTemplate()
    {
        return new RestTemplate();
    }
}

4.启动类

package com.jurf.ms.alibaba.consumer8081;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
 * Hello world!
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class App8081
{
    public static void main( String[] args )
    {
        SpringApplication.run(App8081.class,args);
        System.out.println( "Hello World!" );
    }
}

3.3 服务端

1.pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.jurf.ms</groupId>
        <artifactId>mscloud-demo-2024</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.jurf.ms.alibaba.provider9091</groupId>
    <artifactId>ms-alibaba-provider-9091</artifactId>
    <packaging>jar</packaging>
    <name>ms-alibaba-provider-9091</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--web + actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

2.application配置

server:
  port: 9091
spring:
  application:
    name: ms-alibaba-provider9091
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

3.业务

package com.jurf.ms.alibaba.provider9091.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
/**
 * @auther zzyy
 * @create 2024-01-01 15:31
 */
@RestController
public class PayAlibabaController
{
    @Value("${server.port}")
    private String serverPort;
    @GetMapping(value = "/pay/nacos/{id}")
    public String getPayInfo(@PathVariable("id") Integer id)
    {
        return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
    }
}

4.启动类

package com.jurf.ms.alibaba.provider9091;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
 * Hello world!
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class Nacos9091App
{
    public static void main( String[] args )
    {
        SpringApplication.run(Nacos9091App.class,args);
        System.out.println( "Hello World!" );
    }
}

3.4 调试

1启动服务

2.调试:http://localhost:8081/consumer/pay/nacos/456

四 nacos模拟负载均衡

4.1 模拟操作

1.在服务9091的基础上,复制一份服务

2.进行配置:配置端口为:9092,在vm parmater参数项配置如下:-DServer.port=9092

3.进行启动

1.查看nacos

2.查看

3.动态不停刷新访问

提供服务为9091

提供服务为9092

五 nacos的动态刷新

5.1 动态刷新配置逻辑

1.配置规则:

2.配置对应关系

5.2 配置操作

5.2.1 pom配置文件

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <!--nacos-config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

5.2.2 修改bootstrap和application配置文件

1.bootstrap配置文件

# nacos配置
spring:
  application:
    name:  ms-alibaba-consumer-8081
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置
#        group: DEFAULT_GROUP
#        namespace: c364e76c-e133-4bc0-951e-205cf732a19f
# nacos端配置文件DataId的命名规则是:
# ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 本案例的DataID是:nacos-config-client-dev.yaml  nacos-config-client-test.yaml

2.application配置文件

server:
  port: 8081
app:
  appid: 94933
spring:
  profiles:
    #active: dev # 表示开发环境
    active: prod # 表示生产环境
    #active: test # 表示测试环境
#消费者将要去访问的微服务名称(nacos微服务提供者叫什么你写什么)
service-url:
  nacos-user-service: http://ms-alibaba-provider9091
 

5.2.3 在nacos进行文件配置

1.新建namespace

2.新建文件:注意配置文件后族为yaml,不能写成yml,否则无法识别

5.2.4 验证

1.启动服务

2.验证:http://localhost:8081/consumer/pay/nacos/456

3.修改配置文件

4.再次查看

到此这篇关于springcloud nacos的赋值均衡和动态刷新的文章就介绍到这了,更多相关springcloud nacos赋值均衡和动态刷新内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文