SpringCloud學習筆記---服務的註冊和發現

前言

​ 微服務的意義和概念在當下十分火熱,而spring同樣提供了實現微服務的SpringCloud框架。更多的也不多說,希望通過這個系列記錄學習過程的的經驗。

​ 所有版本以SpringBoot2.xSpringCloud Finchley爲主,構建工具爲gradle,IDE爲基礎的eclipse。如果使用maven構建可以在官網和其他推薦文章中參考構建pom代碼。

​ 第一篇是關於在微服務中服務中心的註冊和服務的發現。服務管理使用NetFlix Eureka。文後使用的Spring Cloud Netflix都是SpringCoud對Netflix公司一系列開源產品的包裝。

正文

創建服務註冊中心

首先創建一個gradle工程,命名eureka-server,在build.gradle中輸入依賴內容:

buildscript {
    ext {
        springBootVersion = '2.0.0.M6'
    }
    repositories {
        mavenCentral()
        mavenLocal()
        maven {
            url 'https://repo.spring.io/libs-milestone'
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
sourceCompatibility = 1.8

repositories {
    maven {
        url 'https://repo.spring.io/libs-milestone'
    }
}

dependencyManagement {
  imports {
    mavenBom ':spring-cloud-dependencies:Finchley.M5'
  }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-config:2.0.0.M1'
    compile 'org.springframework.cloud:spring-cloud-starter-eureka-server:2.0.0.M1'
}

啓動gradle引入依賴後,創建一個服務註冊中心:

@EnableEurekaServer
@SpringBootApplication
public class Application {

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

在默認設置中,服務註冊中心會將自己作爲客戶端註冊自己,因此需要在配置中配置避免這種情況。

spring.application.name=eureka-server
server.port=1001

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

啓動SpringBootApplication,啓動成功後,在設置好的端口可以看到服務註冊中心頁面:http://localhost:1001

服務註冊中心

創建服務客戶端(提供者)

同樣創建gradle工程,以下是gradle文件

buildscript {
    ext {
        springBootVersion = '2.0.0.M6'
    }
    repositories {
        mavenCentral()
        mavenLocal()
        maven {
            url 'https://repo.spring.io/libs-milestone'
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
sourceCompatibility = 1.8

repositories {
    maven {
        mavenLocal()
        url 'https://repo.spring.io/libs-milestone'
    }
}

dependencyManagement {
  imports {
    mavenBom ':spring-cloud-dependencies:Finchley.M5'
  }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-config:2.0.0.M1'
    // https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '2.0.0.M2'

}

爲了在客戶端顯示服務信息,創建一個控制器:

@RestController
public class DcController {

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/dc")
    public String dc(){
        String services = "Services: " + discoveryClient.getServices();
        System.out.println(services);
        return services;
    }
}

客戶端Appliction:


@EnableDiscoveryClient
@SpringBootApplication
@SpringBootConfiguration
@ComponentScan(basePackageClasses=DcController.class)
public class ClientApplication {

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

啓動application。

可以在之前的服務註冊中心看到這個新的服務已註冊:

服務客戶端

再點開註冊的控制器

輸出服務信息

以上便建立了服務與服務註冊中心之間的聯繫。

參考文章:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章