Nacos学习笔记(二)----作为注册中心使用

x33g5p2x  于2021-12-20 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(234)

一.排坑(关于springboot和springcloud的版本问题)

1.昨天下午开始尝试做整合,整合完运行项目一直报错

2019-04-16 16:50:30.206 ERROR 10876 --- [           main] o.s.c.a.nacos.NacosConfigProperties      : create config service error!properties=NacosConfigProperties{serverAddr='null', encode='null', group='DEFAULT_GROUP', sharedDataids='null', refreshableDataids='null', prefix='null', fileExtension='properties', timeout=3000, endpoint='null', namespace='null', accessKey='null', secretKey='null', contextPath='null', clusterName='null', name='null', activeProfiles=[]},e=,

com.alibaba.nacos.api.exception.NacosException: null
	at com.alibaba.nacos.api.config.ConfigFactory.createConfigService(ConfigFactory.java:45) ~[nacos-api-0.6.2.jar:na]
	at com.alibaba.nacos.api.NacosFactory.createConfigService(NacosFactory.java:41) ~[nacos-api-0.6.2.jar:na]
	at org.springframework.cloud.alibaba.nacos.NacosConfigProperties.configServiceInstance(NacosConfigProperties.java:347) ~[spring-cloud-alibaba-nacos-config-0.2.1.RELEASE.jar:0.2.1.RELEASE]
	at org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator.locate(NacosPropertySourceLocator.java:63) [spring-cloud-alibaba-nacos-config-0.2.1.RELEASE.jar:0.2.1.RELEASE]
	at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:94) [spring-cloud-context-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:628) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:364) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at com.gfyw.client.ClientApplication.main(ClientApplication.java:19) [classes/:na]

通过上网查,找到一个感觉靠谱的解决方法,大致意思是说是springboot加载配置文件的问题,要把项目的配置文件” application.properties”的nacos配置写入” bootstrap.properties”中。

文章地址: https://blog.csdn.net/wk52525/article/details/88547069

一通操作之后,错是不报了,但是注册中心还是没注册上……这个给我愁的,后来发现可能是springboot和springcloud的对应版本问题。

springboot和springcloud的对应版本:https://spring.io/projects/spring-cloud

2.关于nacos注册中心的maven坐标:

地址: https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-alibaba-nacos-discovery

3.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.nacos</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二.建springboot项目(服务提供方)

1.创建一个springboot项目,作为服务提供方

2.配置文件: application.properties

#注册服务名
spring.application.name=test-nacos-server
#端口号
server.port=8001
#nacos地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

3.启动类

package com.nacos.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {

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

    @RestController
    static class TestController {
        @GetMapping("/test")
        public String hello(@RequestParam String msg) {
            String res = "这是啥?" + msg;
            System.out.println(res);
            return res;
        }
    }
}

@EnableDiscoveryClient:开启服务注册发现

4.启动:

2019-04-16 14:31:06.662  INFO 3848 --- [           main] o.s.c.a.n.registry.NacosServiceRegistry  : nacos registry, alibaba-nacos-discovery-server 192.168.99.1:8001 register finished

控制台出现此内容,说明注册成功

5.idea启动多个服务实例(简单粗暴):

①.

②.

③.启动项目:

④.启动完成后修改application.properties文件中的端口号。修改完端口号之后再启动一次项目(之前启动的项目不要关闭)

⑤.两个实例启动完成,进入nacos看一眼,没啥毛病

三.建springboot项目(服务消费方)

1.创建一个springboot项目,作为服务消费方

2.配置文件: application.properties

#注册服务名
spring.application.name=test-nacos-client
#端口号
server.port=9000
#nacos地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

3.启动类:

package com.nacos.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosClientApplication {

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

    @RestController
    static class TestController {

        @Autowired
        LoadBalancerClient loadBalancerClient;

        @GetMapping("/test")
        public String test() {

            /*springcloud common的负载均衡接口,提供服务名实现服务调用*/
            ServiceInstance serviceInstance = loadBalancerClient.choose("test-nacos-server");
            String url = serviceInstance.getUri()+"/test?msg=nacos";
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForObject(url, String.class);
            String msg = url + "---- return : " + result;
            System.out.println(msg);
            return msg;
        }
    }
}

4.启动

5.进入nacos,再看一眼是否注册上

三.用postman或其他工具测一下

看来没啥问题,并且已经实现了负载均衡,完事。

相关文章