Nacos微服务配置,注册&发现快速上手

准备好centos7,配好JAVA_HOME, Nacos下载地址

tar xf nacos-server-1.1.4.tar.gz -C {指定解压目录}
cd {解压目录}
#修改持久层为mysql(默认是用derby)
#将 conf/nacos-mysql.sql怼到你的数据库
vi conf/application.properties
#添加以下内容,具体内容根据自己的环境定,比如数据,用户和密码
# spring.datasource.platform=mysql  
# db.num=1  
# db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true  
# db.user=nacos_devtest  
# db.password=youdontknow  


#启动单节点
sh startup.sh -m standalone  

#入口地址
#http://{本机ip}/nacos/index.html#/login
#默认用户名/密码为  nacos/nacos
复制代码

nacos可以做为配置中心(同类产品有apollo, springcloud config), 注册中心(同类型eureka, consul,zookeeper)。

nacos可以做高可用,集群,具体怎么搭,请参考官网文档。

SpringBoot2.X整合Nacos的配置中心

依赖

Maven Pom

<properties>
        <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
    </properties>


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

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-actuator</artifactId>
            <version>${nacos-config-spring-boot.version}</version>
        </dependency>
    </dependencies>
复制代码

本地properties配置

nacos.config.server-addr=地址:8848
复制代码

创建Nacos配置

Springboot 注入

@SpringBootApplication
@NacosPropertySource(dataId = "first_data", groupId = "first_group", autoRefreshed = true)
@RestController
public class NacosApplication {
    @NacosValue(value = "${test.mytest:defaultString}", autoRefreshed = true)
    private String test;

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

    @GetMapping("/get")
    public JsonNode get() {
        ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
        jsonNodes.put("testStringIs", test);
        return jsonNodes;
    }
}
复制代码

为什么用properties做nacos存储,因为我去整合yml,其中有一个maven依赖报错,我暂时无法解决。

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>
复制代码

报错如图,有人知道这是啥问题吗?

或者用spirng-cloud封装好的starter

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>0.9.0.RELEASE</version>
</dependency>
复制代码
spring.cloud.nacos.config.server-addr=192.168.208.102:8848
spring.application.name=first_data
spring.cloud.nacos.config.group=first_group
spring.cloud.nacos.config.file-extension=properties
复制代码

这个starter是有坑的,比如我拿之前first_data / first_group,他发http请求会发到http://192.168.208.102:8848/nacos/v1/cs/configs?dataId=first_data.properties&group=first_group,但是这个地址是拿不到的,需要去除掉first_data后缀properties才能拿到数据的,不知道是spring封装的有问题还是。

Springboot服务注册

父工程pom

<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>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                    <version>0.9.0.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                    <version>1.1.0</version>
                </dependency>
            </dependencies>
    </dependencyManagement>
复制代码

子项目pom

<properties>
        <nacos-discovery-spring-boot.version>0.2.1</nacos-discovery-spring-boot.version>
    </properties>
    <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>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
    </dependencies>
复制代码

Springboot启动类

@SpringBootApplication
@EnableDiscoveryClient
public class Producer {
    public static void main(String[] args) {
        SpringApplication.run(Producer.class, args);
    }

    @RestController
    class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery " + string;
        }
    }


}
复制代码

application.properties

server.port=8100
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=192.168.208.102:8848
复制代码

服务发现于调用

pom

<properties>
        <spring-cloud-openfeign.version>2.0.0.RELEASE</spring-cloud-openfeign.version>
        <spring-cloud-netflix.version>2.0.0.RELEASE</spring-cloud-netflix.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>${spring-cloud-netflix.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>${spring-cloud-openfeign.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
    </dependencies>
复制代码

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://nacos-provider/echo/" + str, String.class);
        }
    }
}
复制代码

application.properties

server.port=8200
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=192.168.208.102:8848
复制代码
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章