springcloud微服務實戰 學習筆記二 服務提供者

zhumeilu發表於2017-12-14
  • 新增依賴

          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.5.4.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <java.version>1.8</java.version>
          </properties>
    
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-eureka-server</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
          </dependencies>
    
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-dependencies</artifactId>
                      <version>Dalston.SR1</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
    
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
    複製程式碼
  • 配置檔案

      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";
          }
      }
    複製程式碼

相關文章