本文經過建立一個簡單是實例,展現spring cloud的應用場景,使用eureka作服務註冊和發現,管理其餘服務,是系統的核心,移除宕機服務不會形成故障導致系統沒法使用。使用zuul作路由轉發和負載均衡,外部訪問統一走網關。內部調用使用feign來註解服務,簡化調用代碼編寫。整個spring cloud的微服務的應用雛形就是這樣,上手仍是很是簡單的。前端
服務就是提供相應功能的代碼、模塊,在系統裏統一註冊到服務中心,其餘服務根據服務名等來調用這個服務,這也是eureka所作的工做。java
部署的服務不少的狀況下,每個服務都有本身的端口,前端或者其餘外部調用若是都要指定ip和端口去訪問,就會很是繁瑣。網關作的工做是統一管理這些服務的路由,根據路徑轉發到不一樣的服務去,而後還作負載均衡這些。git
github地址 spring-cloud-simplegithub
選擇gradle,而後勾選java web
而後一路nextspring
若是根目錄沒有gradle.properties,則新建一個,添加公共版本號api
springboot_version=2.1.3.RELEASE
springcloud_version=Greenwich.SR1
複製代碼
在跟目錄的build.gradle中加入內容瀏覽器
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springboot_version}"
}
}
allprojects {
apply plugin: "java"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
repositories {
mavenCentral()
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springcloud_version}"
}
}
}
複製代碼
點擊項目名稱 -> newspringboot
eureka負責服務發現和服務註冊,是spring cloud的核心,新建一個eureka-server子項目,在子目錄的build.gradle中加入依賴bash
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
}
複製代碼
在src/main/java/包中添加Application
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
複製代碼
在src/main/resources添加配置application.yml
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
複製代碼
點擊idea中運行main,瀏覽器訪問http://localhost:8761便可看到頁面
hello-service是一個eureka client,它把本身的服務註冊到eurake中,其餘client能夠從服務中心獲取到其餘client
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
複製代碼
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
@Value("${server.port}")
private int port;
@RequestMapping("/hi")
public String hello() {
return "hi, my port=" + port;
}
}
複製代碼
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8763
spring:
application:
name: hello-service
複製代碼
訪問htto://localhost:8763/hi便可看到頁面
api網關負責路由轉發和負載均衡,轉發特定路由到特定的服務中,因爲其餘服務都是經過特定的端口來暴露服務,網關負責把路由轉發到特定的端口。
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
複製代碼
@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
複製代碼
server:
port: 8769
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: zuul
zuul:
routes:
hello:
path: /hello/**
serviceId: hello-service
複製代碼
訪問http://localhost:8769/hello/hi可看到hello-service返回的頁面
服務內部相互調用,直接使用feign註解service訪問,feign提供了負載均衡等。
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
}
複製代碼
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@RestController
public class CallServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CallServiceApplication.class, args);
}
@Autowired
private HelloService helloService;
@RequestMapping("/hi")
public String hello() {
return helloService.sayHiFromClientOne();
}
}
複製代碼
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "hello-service")
public interface HelloService {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String sayHiFromClientOne();
}
複製代碼
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: call-service
複製代碼
修改api-gateway配置加入call路由到routes
routes:
hello:
path: /hello/**
serviceId: hello-service
call:
path: /call/**
serviceId: call-service
複製代碼
啓動call-service,重啓api-gateway,訪問http://localhost:8769/call/hi,可看到由call調用hello返回的頁面
進入子項目,運行
../gradlew clean build -x test
複製代碼
打好的jar包在./build/libs/中