windows nacos集群配置及springcloud集成nacos

一、nacos集群配置

1.笔者使用的nacos版本为nacos-server-2.1.2 下载地址 Releases · alibaba/nacos · GitHub

2.由于Nacos2.x版本相比1.x新增了gRPC的通信方式,因此需要增加2个端口。新增端口是在配置的主端口(server.port)基础上,进行一定偏移量自动生成(分别偏移了1000和1001)

所以集群3个节点设置的端口为8848,8838,8828,防止新增端口冲突。

3.在nacos-server-8848/conf/application.properties 修改以下配置

#端口
server.port=8848

spring.datasource.platform=mysql

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

### mysql数据库连接配置
db.url.0=jdbc:mysql://127.0.0.1:3307/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=root

#修改使用权限
nacos.core.auth.enabled=true

4.在nacos-server-8848/conf 复制cluster.conf.example 文件改名为cluster.conf,添加配置

192.168.31.112:8828
192.168.31.112:8838
192.168.31.112:8848

5.复制三分,注意修改application.properties 里面的server.port

6.初始化nacos-server-8848\conf\mysql-schema.sql 数据库脚本

 7.在nacos-server-88X8\bin 点击startup.cmd 启动

 8.分别访问http://localhost:8848/nacos/http://localhost:8838/nacos/,http://localhost:8838/nacos/

验证是否集群配置成功

二、springcloud集成nacos

1.主项目pom文件信息

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>nacosDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacosDemo</name>
    <description>nacosDemo</description>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath></relativePath>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--       web场景依赖-->
        <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>
        <!--nacos场景依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

2.子模块pom文件信息

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>nacosDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>producer</name>
    <description>producer</description>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>
</project>

3.项目bootstrap.yml 信息

server:
  port: 9002
spring:
  profiles:
    active: pro
  application:
    name: producer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.112:8848,192.168.31.112:8838,192.168.31.112:8828 #nocas server
        username: nacos
        password: nacos
      config:
        server-addr: 192.168.31.112:8848,192.168.31.112:8838,192.168.31.112:8828 #nocas-server
        username: nacos
        password: nacos
        file-extension: yaml #m默认读取的是配置中心文件类型为properties的文件,如果像读取其他格式需要在此处指定

4.ConfigController 控制类代码实例

package com.example.producer;

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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping("")
@RefreshScope
public class ConfigController {

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;
    @Value("${name:hu}")
    private String name;

    @Value("${server.port}")
    private int port;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public String get() {
        if(port==9001){
            int m=0;
            //throw new RuntimeException("123123");
        }
        System.err.println(useLocalCache+"==="+name+"==="+port);
        return useLocalCache+"==="+name+"==="+port;
    }
}

5.nacos 系统配置

6.启动项目

访问http://localhost:9002/get

7.代码集成完成。

 三、nginx 代理nacos注册中心

1.nginx 下载nginx-1.10.3 nginx: download

2.在nginx-1.10.3\conf\nginx.conf 添加配置,注意结构不要乱掉了

upstream nacos-cluster {
		server 192.168.31.112:8848 max_fails=2 fail_timeout=2s weight=1;
		server 192.168.31.112:8838 max_fails=2 fail_timeout=2s weight=1;
		server 192.168.31.112:8828 max_fails=2 fail_timeout=2s weight=1;
	}
	
	
	server {
		listen       80;
		server_name  localhost;
		proxy_send_timeout 1s; #请求超时时间设置
		proxy_read_timeout 1s; #读取响应超时时间设置
		proxy_connect_timeout 1s; #连接超时时间设置
		send_timeout 1s; #请求超时时间设置
 
		location /nacos {
			proxy_pass http://nacos-cluster;
			proxy_next_upstream timeout; #超时转发
			proxy_read_timeout 1s; #读取响应超时时间设置
		}
	}

3.在nginx-1.10.3 双击 nginx.exe 启动nginx服务器

4.访问http://localhost/nacos 登录即说明代理成功

5.项目中bootstrap.yml 配置可修改成以下内容

server:
  port: 9002
spring:
  profiles:
    active: pro
  application:
    name: producer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.112 #nocas-server
        username: nacos
        password: nacos
      config:
        server-addr: 192.168.31.112 #nocas-server
        username: nacos
        password: nacos
        file-extension: yaml #m默认读取的是配置中心文件类型为properties的文件,如果像读取其他格式需要在此处指定

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值