Spring Cloud Alibaba - Nacos服务提供者


一、Nacos服务提供者

Nacos兼具注册中心


二、前期代码准备

后续Spring Cloud Alibaba开发都是基于本套代码上开发

2.1, 构架Maven项目,取名sca-partner

  • pom.xml


    4.0.0
    com.ydw
    sca-partner
    1.0-SNAPSHOT

    
    
        sca-service-8081
    

    
    pom

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.5.RELEASE
    

    
        11
    

    
        
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.1.0.RELEASE
                pom
                import
            
        
        
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    11
                    11
                    utf-8
                
            
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                false
            
        
    



三、Nacos服务提供者开发

3.1,构架SpringBoot项目,取名sca-service-8081(取名建议带上端口号,后续微服务太多方便查询)

  • pom.xml



    
        sca-partner
        com.ydw
        1.0-SNAPSHOT
    
    4.0.0
    sca-service-8081

    
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
    

  • application.yml
server:
  port: 8081  # 端口
spring:
  application:
    name: sca-service # 服务名,同是也会作为Nacos注册中心的服务名
  # nacos配置
  cloud:
    nacos:
      discovery:
        # 集群中各节点信息都配置在这里(集群用,隔开)
        server-addr: 127.0.0.1:8848
  • ScaService8081Application.java启动类
package com.ydw.sca;

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

@SpringBootApplication
@EnableDiscoveryClient //通用注册中心注解
public class ScaService8081Application {
    public static void main(String[] args) {
        SpringApplication.run(ScaService8081Application.class, args);
    }
}
  • 启动成功后,会在注册中心上面看到服务名


    image.png

你可能感兴趣的:(Spring Cloud Alibaba - Nacos服务提供者)