前言
微服務的意義和概念在當下十分火熱,而spring同樣提供了實現微服務的SpringCloud框架。更多的也不多說,希望通過這個系列記錄學習過程的的經驗。
所有版本以SpringBoot2.x和SpringCloud 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。
可以在之前的服務註冊中心看到這個新的服務已註冊:
再點開註冊的控制器
以上便建立了服務與服務註冊中心之間的聯繫。