保姆教程系列二、Nacos實現註冊中心

南國以南i發表於2022-03-30

前言:

請各大網友尊重本人原創知識分享,謹記本人部落格:南國以南i

上篇我們介紹到  保姆教程系列一、Linux搭建Nacos 

註冊中心原理

一、環境準備

二、建立專案

2.1 建立專案父工程

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

    <groupId>com.example</groupId>
    <artifactId>nacos</artifactId>
    <version>0.0.1</version>
    <name>nacos</name>
    <description>Spring Cloud Nacos</description>

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

    <!--引入子模組-->
    <modules>
        <module>provider</module><!--生產者-->
        <module>consumer</module><!--消費者-->
    </modules>

    <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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--nacos配置中心依賴-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--nacos配置中心依賴-->
        <!--<dependency>-->
            <!--<groupId>org.springframework.cloud</groupId>-->
            <!--<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
            <!--<version>0.2.1.RELEASE</version>-->
        <!--</dependency>-->
        <!--lombok依賴-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--註冊中心依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
    </dependencies>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2.2 建立Provider服務生產者

在父工程Nacos下建立springboot子工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--父工程-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>nacos</artifactId>
        <version>0.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1</version>
    <name>provider</name>
    <description>Provider Nacos</description>

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

    <dependencies>
        <!--lombok依賴-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

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

</project>

在啟動類ProviderApplication.java中增加@EnableDiscoveryClient註解

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

@SpringBootApplication
@EnableDiscoveryClient//開啟註冊
public class ProviderApplication {

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

}

配置檔案application.yml進行如下配置

#生產者配置
server:
  port: 8081

spring:
  application:
    name: nacos-provider #服務名稱
  cloud:
    nacos:
      discovery:  #使用註冊中心
        server-addr: 192.168.36.135:8848 #Nacos訪問地址
        enabled: true

在服務提供方建立一個對外介面

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


@RequestMapping("/provider")
@RestController
public class IndexController {

    @GetMapping("/hello")
    public String hello() {
        return "我是provider服務生產者";
    }
}

2.3 建立Consumer服務消費者

在父工程Nacos下建立springboot子工程consumer,其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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--父工程-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>nacos</artifactId>
        <version>0.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1</version>
    <name>consumer</name>
    <description>Consumer Nacos</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--feign依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--hystrix斷路器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

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

</project>

在啟動類ConsumerApplication.java中增加註解

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 //開啟Feign服務
public class ConsumerApplication {

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

}

配置檔案application.yml進行如下配置

#消費者配置
server:
  port: 8082

spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      discovery:  #使用註冊中心
        server-addr: 192.168.36.135:8848
        enabled: true

#開啟斷路器
feign:
 hystrix:
   enabled: true

使用FeginClient進行服務呼叫,hystrix進行熔斷

import com.example.consumer.hystrix.HystrixUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "nacos-provider",fallback = HystrixUtils.class)//服務出現異常進行容錯
public interface ProviderFeignClient {

    /**
     * .
     * 呼叫生產者服務
     * @return
     */
    @GetMapping("/provider/hello")
    String hello();
}

HystrixUtils.class容錯類

import com.example.consumer.client.ProviderFeignClient;
import org.springframework.stereotype.Component;

@Component
public class HystrixUtils implements ProviderFeignClient {

    /**
     * .
     * 方法重寫
     *
     * @return
     */
    @Override
    public String hello() {
        return "斷路器容錯,服務開小差了,稍等片刻...";
    }
}

service層服務呼叫

import com.example.consumer.client.ProviderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexService {

    @Autowired
    private ProviderFeignClient client; //注入

    public String hello() {
        return client.hello(); // 服務呼叫
    }
}

controller中呼叫service的介面,像一般的介面呼叫一樣

import com.example.consumer.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/consumer")
@RestController
public class IndexController {

    @Autowired
    private IndexService service;


    @GetMapping("/hello")
    public String hello(){
          return service.hello();
    }
}

三、服務呼叫測試

3.1 專案啟動

啟動完成後,在服務提供者和消費者的日誌中應該可以分別看到如下資訊

INFO 18388 --- [           main] o.s.c.a.n.registry.NacosServiceRegistry  : nacos registry, nacos-provider 192.168.22.1:8081 register finished

3.2 登入Nacos控制檯

你會發現服務列表中,已經顯示了我們剛才建立的兩個專案,並可以對其進行簡單的監控和管理。

3.3 使用樣例專案

開啟瀏覽器輸入:http://localhost:8082/consumer/hello     

 

 

 

 

 

 Nacos服務發現與Eureak服務發現無差異

 敬請關注下篇 保姆教程系列三、Nacos Config–服務配置中心

 總結:

我是南國以南i記錄點滴每天成長一點點,學習是永無止境的!轉載請附原文連結!!!

 參考連結

相關文章