微服務
實踐“微服務”自然要學習如何做服務註冊與發現
基於SpringBoot來進行微服務的學習,自然選擇了與之息息相關的SpringCloud;當然可以選擇其他的技術進行,比如dubbo
也可以用zookeeper來實現服務註冊與發現,至於zookeeper來實現此功能好還是不好,各家之言都有
SpringCloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems .SpringCloud
SpringCloud 包含了 Distributed/versioned configuration、Distributed/versioned configuration等很多子項目。
- Distributed/versioned configuration
- Service registration and discovery
- Routing
- Service-to-service calls
- Load balancing
- Circuit Breakers
- Global locks
- Leadership election and cluster state
- Distributed messaging
服務註冊與發現
SpringCloud模塊
- spring-cloud-starter-eureka-server
工程module
- 服務註冊中心
- 服務module
服務註冊中心
創建discovery module,並在 build.gradle中引入 spring-cloud-starter-eureka-server依賴
apply plugin: 'org.springframework.boot'
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
}
}
repositories {
mavenCentral()
}
dependencies {
compile ('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
jar {
baseName = 'discovery-bootcwenao'
}
通過註解 @EnableEurekaServer 提供註冊中心服務
/**
* @author cwenao
* @version $Id DiscoveryBootcwenaoApplication.java, v 0.1 2017-01-12 9:56 cwenao Exp $$
*/
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryBootcwenaoApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(DiscoveryBootcwenaoApplication.class).web(true).run(args);
}
}
application.yml 配置eureka屬性
server:
port: 8761
eureka:
instance:
hostname: discovery
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://discovery:${server.port}/eureka/
服務註冊
創建服務module, 在build.gradle中引入 spring-cloud-starter-eureka
apply plugin: 'org.springframework.boot'
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.cloud:spring-cloud-stream')
}
sourceSets {
main {
resources.srcDirs = ['src/main/resources', 'src/main/java']
resources.includes = ['**/*.xml', '**/*.yml']
}
}
jar {
baseName = 'apigateway-bootcwenao'
}
通過註解 @EnableDiscoveryClient 進行服務註冊
@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayBootcwenaoApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args);
}
}
application.yml 配置eureka屬性
server:
port: 10002
spring:
application:
name: apigateway
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
註冊完成後,可以通過 spring.application.name 的配置來訪問該服務
訪問 http://localhost:8761 發現服務已經在註冊中心上註冊
服務註冊中心啓用用戶名密碼
通過配置applicaiton.yml用戶名密碼
security:
basic:
enabled: true
user:
name: aa
password: abcd
配置服務提供方application.yml
eureka:
instance:
hostname: configserver
prefer-ip-address: true
client:
registerWithEureka: true
fetchRegistry: true
service-url:
defaultZone: http://aa:abcd@localhost:8761/eureka/
代碼
代碼請移步 Github參考地址
如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start