springcloud微服务实战 学习笔记二 服务提供者

  • 添加依赖

          
              org.springframework.boot
              spring-boot-starter-parent
              1.5.4.RELEASE
               
          
          
              UTF-8
              UTF-8
              1.8
          
    
          
              
                  org.springframework.cloud
                  spring-cloud-starter-eureka-server
              
              
                  org.springframework.boot
                  spring-boot-starter-web
              
          
    
          
              
                  
                      org.springframework.cloud
                      spring-cloud-dependencies
                      Dalston.SR1
                      pom
                      import
                  
              
          
    
          
              
                  
                      org.springframework.boot
                      spring-boot-maven-plugin
                  
              
          
    
  • 配置文件

      spring.application.name=eureka-client
      server.port=2222
      eureka.instance.hostname=localhost
      #服务注册中心
      eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
    
  • Application.java

      @EnableDiscoveryClient
      @SpringBootApplication
      public class Application {
      
          public static void main(String[] args) {
              new SpringApplicationBuilder(Application.class).web(true).run(args);
          }
      }
    

@EnableDiscoveryClient开启了服务发现

  • controller

      @RestController
      public class DemoController {
      
          Logger logger = LoggerFactory.getLogger(this.getClass());
          @Autowired
          DiscoveryClient  discoveryClient;
          @GetMapping("/hello")
          public String hello(){
      
              logger.info("service:"+ discoveryClient.getServices());
              return "hello world";
          }
      }

你可能感兴趣的:(springcloud微服务实战 学习笔记二 服务提供者)