SpringCloud項目中使用Nacos作為注冊中心


SpringCloud和Nacos的介紹原理在這里就不多說了,百度一大堆,這里就只是記錄一下剛開始學習時候項目的使用過程

Nacos-server

我這里是從官網下載的Nacos-server

下載地址: https://github.com/alibaba/nacos/releases

下載解壓之后就可以啟動使用

修改端口:

  用編輯器打開bin目錄下的startup.cmd文件 添加一行代碼

set "JAVA_OPT=%JAVA_OPT% --server.port=9090

解壓后進入bin文件夾,直接雙擊執行startup.cmd文件,啟動成功如下圖:

 

 啟動成功后,此時Nacos控制台就可以訪問了,瀏覽器訪問:http://127.0.0.1:8848/nacos/index.html ,默認的賬號密碼為nacos/nacos,控制台頁面如下:

 創建項目父工程

IDEA中創建聚合項目nacos作為父工程,其pom.xml如下

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

    <groupId>com.example</groupId>
    <artifactId>nacos</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>nacosprovider</module>
        <module>nacosconsumer</module>
        <module>nacosconfig</module>
    </modules>


    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.0.4.RELEASE</spring-boot.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <nacos.version>0.2.2.RELEASE</nacos.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${nacos.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

創建服務提供方

在父工程Nacos下創建springboot子工程nacos-provider,其pom.xml文件為:

<?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">
    <parent>
        <artifactId>nacos</artifactId>
        <groupId>com.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-provider</artifactId>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

    </dependencies>

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

</project>

在啟動類NacosProviderApplication.java中增加@EnableDiscoveryClient注解

package com.example.nacosprovider;

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

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

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

}

配置文件application.yml進行如下配置

server:
  port: 8000
spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

 

在服務提供方創建一個對外接口

package com.example.nacosprovider.controller;

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

@RestController
public class ProviderController {

    @GetMapping("/helloProvider")
    public String helloProvider(){
        return "你好,我是服務提供者";
    }
}

創建服務消費者

仍然在nacos工程下創建一個SpringBoot項目子工程命名為nacos-consumer,其pom文件與nacos-provider相同。

在啟動類NacosConsumerApplication.java中增加@EnableDiscoveryClient和@EnableFeignClients注解

package com.example.nacosconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApplication {

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

}

同樣為nacos-consumer增加配置文件,內容如下

server:
  port: 9000
spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
feign:
  hystrix:
    enabled: true

使用feign進行服務調用,hystrix進行熔斷

項目目錄

 

 在service中進行服務提供者的接口調用

package com.example.nacosconsumer.service;

import com.example.nacosconsumer.service.impl.ConsumerServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "nacos-provider", fallback = ConsumerServiceImpl.class)
public interface ConsumerService {

    @GetMapping("/helloProvider")
    String getHello();
}

在service的實現類中進行重寫

package com.example.nacosconsumer.service.impl;

import com.example.nacosconsumer.service.ConsumerService;
import org.springframework.stereotype.Component;

@Component
public class ConsumerServiceImpl implements ConsumerService{

    @Override
    public String getHello() {
        return "服務出錯";
    }
}

在controller中調用service的接口,像一般的接口調用一樣

package com.example.nacosconsumer.controller;

import com.example.nacosconsumer.service.ConsumerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ConsumerController {

    @Resource
    private ConsumerService consumerService;

    @GetMapping("/getHello")
    public String getHello(){
        return consumerService.getHello();
    }
}

調用測試

啟動完成后,在服務提供者和消費者的日志中應該可以分別看到如下信息

2019-12-10 16:36:37.800  INFO 18452 --- [           main] o.s.c.a.n.registry.NacosServiceRegistry  : nacos registry, application 172.16.74.34:9001 register finished

現在登錄Nacos控制台,你會發現服務列表中,已經顯示了我們剛才創建的兩個項目,並可以對其進行簡單的監控和管理。

 

 瀏覽器中訪問服務消費者的接口 http://localhost/consumer, 可以看到成功返回結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM