nacos-服务注册发现-01

nacos 服务注册与发现代替Eureka。

  • nacos-server

下载,安装,启动可以参考官方文档。本文章使用单机模式

nacos 替换Eureka 做服务注册中心

  • pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.example
    user
    0.0.1-SNAPSHOT
    user
    Demo project for Spring Boot

    
        1.8
        2.2.0.RELEASE
        Hoxton.SR6
    

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







        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

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

    
        
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                ${spring-cloud-alibaba.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

  • application.yml
server:
  port: 8888

spring:
  application:
    name: user-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 # 注册中心地址
  • UserApplication
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient // 使用该注解,开始服务注册
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

}

  • nacos 管理界面


注意:虽然在这里使用的 nacos 作为注册中心,其他spring cloud Netflix 组件一样可以使用

  • feign 调用
@FeignClient(value = "role-service")// 和以前一样
public interface RoleService {

    @GetMapping
    String role();

}

总结:我们简单的使用nacos 作为注册中心,算是入门操作。使用非常简单,其他更详细的使用可以参考官方文档。后面会开始分析nacos 作为注册的实现原理。

你可能感兴趣的:(nacos-服务注册发现-01)