0.9.0.RELEASE版本的spring cloud alibaba nacos實例


  簡而言之,nacos與eureka的不同之處有三:后台老板、部署方式、功能。nacos是阿里的,eureka是奈飛的;nacos有自己的安裝包,需要獨立部署,eureka僅作為一個服務組件,引入jar包即可;nacos=eureka+config,它既是注冊中心,也是配置中心。

  如何使用nacos?先下載安裝包(下載地址https://github.com/alibaba/nacos/releases),打開下載頁 -> 點擊目前最新版本1.1.3跳到下載頁 -> 點擊壓縮包 -> 下載到本地(E盤download目錄)后解壓 -> 通過命令行啟動nacos:

C:\Users\wulf>e:

E:\>cd download\nacos\bin

E:\download\nacos\bin>startup.cmd -m standalone

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 1.1.3
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 32916
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://10.10.100.110:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2019-08-21 10:44:30,073 INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$209b3f7a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-08-21 10:44:30,213 INFO Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-08-21 10:44:30,225 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@6e9175d8' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-08-21 10:44:30,234 INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$456fe22c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-08-21 10:44:30,241 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-08-21 10:44:32,145 INFO Tomcat initialized with port(s): 8848 (http)

2019-08-21 10:44:35,148 INFO Root WebApplicationContext: initialization completed in 8513 ms

2019-08-21 10:44:40,366 INFO Initializing ExecutorService 'applicationTaskExecutor'

2019-08-21 10:44:40,922 INFO Adding welcome page: class path resource [static/index.html]

2019-08-21 10:44:41,424 INFO Creating filter chain: Ant [pattern='/'], []

2019-08-21 10:44:41,435 INFO Creating filter chain: Ant [pattern='/**/*.css'], []

2019-08-21 10:44:41,435 INFO Creating filter chain: Ant [pattern='/**/*.js'], []

2019-08-21 10:44:41,438 INFO Creating filter chain: Ant [pattern='/**/*.html'], []

2019-08-21 10:44:41,442 INFO Creating filter chain: Ant [pattern='/**/*.map'], []

2019-08-21 10:44:41,442 INFO Creating filter chain: Ant [pattern='/**/*.svg'], []

2019-08-21 10:44:41,444 INFO Creating filter chain: Ant [pattern='/**/*.png'], []

2019-08-21 10:44:41,444 INFO Creating filter chain: Ant [pattern='/**/*.ico'], []

2019-08-21 10:44:41,444 INFO Creating filter chain: Ant [pattern='/console-fe/public/**'], []

2019-08-21 10:44:41,446 INFO Creating filter chain: Ant [pattern='/v1/auth/login'], []

2019-08-21 10:44:41,447 INFO Creating filter chain: Ant [pattern='/v1/console/health/**'], []

2019-08-21 10:44:41,447 INFO Creating filter chain: Ant [pattern='/v1/cs/**'], []

2019-08-21 10:44:41,447 INFO Creating filter chain: Ant [pattern='/v1/ns/**'], []

2019-08-21 10:44:41,447 INFO Creating filter chain: Ant [pattern='/v1/cmdb/**'], []

2019-08-21 10:44:41,450 INFO Creating filter chain: Ant [pattern='/actuator/**'], []

2019-08-21 10:44:41,454 INFO Creating filter chain: Ant [pattern='/v1/console/server/**'], []

2019-08-21 10:44:41,498 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6c8bca63, org.springframework.security.web.context.SecurityContextPersistenceFilter@6d24ffa1, org.springframework.security.web.header.HeaderWriterFilter@74cadd41, org.springframework.security.web.authentication.logout.LogoutFilter@42a9e5d1, com.alibaba.nacos.console.filter.JwtAuthenticationTokenFilter@2364305a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@773f7880, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4c51bb7, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@470a696f, org.springframework.security.web.session.SessionManagementFilter@55e8ec2f, org.springframework.security.web.access.ExceptionTranslationFilter@62fad19, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@5536379e]

2019-08-21 10:44:41,566 INFO Exposing 2 endpoint(s) beneath base path '/actuator'

2019-08-21 10:44:41,616 INFO Initializing ExecutorService 'taskScheduler'

2019-08-21 10:44:41,773 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2019-08-21 10:44:41,778 INFO Nacos Log files: E:\download\nacos/logs/

2019-08-21 10:44:41,783 INFO Nacos Conf files: E:\download\nacos/conf/

2019-08-21 10:44:41,783 INFO Nacos Data files: E:\download\nacos/data/

2019-08-21 10:44:41,784 INFO Nacos started successfully in stand alone mode.

2019-08-21 10:44:44,665 INFO Initializing Servlet 'dispatcherServlet'

2019-08-21 10:44:44,686 INFO Completed initialization in 20 ms

  我們通過localhost:8848/nacos可以訪問到它,默認用戶名密碼都是nacos:

  我們創建個項目來注冊到這個nacos,三板斧再來:

  1、pom引入spring boot、spring cloud、spring cloud alibaba和nacos依賴:

<?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.wlf</groupId>    
    <artifactId>spring-cloud-alibaba-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.9.0.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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <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>

  2、application配置nacos地址:

#端口
server.port=8282
#應用名
spring.application.name=lxytrans-provider

#注冊中心
spring.cloud.nacos.discovery.server-addr=localhost:8848

  3、啟動類加注解@EnableDiscoveryClient:

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@SpringBootApplication
public class TransProviderApplication {

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

    @Slf4j
    @RestController
    static class TestController {

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

        @GetMapping("/hey")
        public String hey() throws InterruptedException {
            Thread.sleep(2000);
            return "hey";
        }

    }
}

  跑起來后我們看下nacos控制台實例:

   

 

  以上是服務提供方注冊到nacos, 消費方的注冊跟服務方沒啥不同,這里就不再列出。nacos更多介紹參見https://nacos.io/zh-cn/docs/what-is-nacos.html。至於消費方如何通過nacos調用提供方服務,參見0.9.0.RELEASE版本的spring cloud alibaba nacos+feign實例

  

 


免責聲明!

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



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