什麼是微服務?
微服務架構風格是一種將一個單一應用程序開發爲一組小型服務的方法,每個服務運行在自己的進程中,服務間通信採用輕量級通信機制。
微服務架構的特徵:
- 一個系統由多個微服務搭建。
- 每個微服務可獨立運行在自己的進程裏。
- 一個微服務關注特定的功能。
- 微服務之間通過輕量級的通信機制進行通信。
- 微服務之間可使用不同的語言與數據庫存儲技術。
- 全自動的部署機制。
優點:
- 易於開發和維護:一個微服務關注特定功能,代碼量較少。
- 單個微服務啓動較快:代碼少,所以啓動快。
- 局部修改容易部署:修改微服務只需重。
- 技術棧不受限:可結合項目業務及團隊的特點,合理地選擇技術棧。
- 按需伸縮:可根據需求,實現細粒度的擴展。
面臨挑戰:
- 運維要求較高:更多的微服務意味着更多的運維投入。
- 分佈式固有的複雜性:系統容錯、網絡延遲、分佈式事務等。
- 接口調整成本高:微服務之間通過接口進行通信。
- 重複勞動:可能就某一個相同功能重複編寫多次。
Spring Cloud簡介
Spring Cloud是在SpringBoot基礎上構建的,用於快速構建分佈式系統的通用模式的工具集,具有開箱即用的生產特徵,大大提高開發效率,爲微服務提供了完整的解決方案。
- 分佈式/版本化配置
- 服務註冊和發現
- 路由
- service - to - service調用
- 負載均衡
- 斷路器
- 分佈式消息傳遞
使用Spring Cloud的構建微服務
在此之間,前來介紹一下Eureka,其作用與Dubbo類似
Eureka是Netflix開源的服務發現組件,本身是一個基於REST的服務。包含Server和Client兩部分。Spring
Cloud將它集成在子項目Spring Cloud Netflix中,從而實現微服務的註冊與發現。
——Spring Cloud與Docker微服務架構實戰
Eureka Server:存儲微服務啓動時的註冊信息,提供服務發現得能力。
Eureka Client:微服務的消費端
開始代碼
1.創建Eureka Server
創建一個Spring Boot工程eureka-server
注意Spring Boot與Spring Cloud版本必須對應,否則會報錯。
[查看版本信息]https://spring.io/projects/spring-cloud
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dfyang</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</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>
</project>
application.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
# 是否將自己註冊到Eureka Server,由於當前應用爲Eureka Service,故爲false
register-with-eureka: false
# 是否從Eureka Server獲取註冊信息,由於是單點,所以爲false
fetch-registry: false
# 與Eureka Server交互的地址,默認是http://localhost:8761/eureka
service-url:
defaultZone: http://localhost:8761/eureka/
我看有的文章使用的是default-zone,我之前也是使用的default-zone,因爲這樣看起來與其他的更匹配一些,但在使用Spring Security時出現了問題,不知道是不是版本的問題,建議使用defaultZone。
爲Eureka Server添加Spring Security認證
在啓動類上加上@EnableEurekaServer註解,表明是一個Eureka Server
package com.dfyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
啓動項目,訪問localhost:8761
2.創建服務提供者service-producer
創建一個Spring Boot工程service-producer
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dfyang</groupId>
<artifactId>service-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-producer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</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>
</project>
application.yml
server:
port: 8080
spring:
application:
name: service-producer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
啓動類上加上@EnableEurekaClient,表明是一個Eureka Client。
每個服務提供者也能作爲服務消費者消費服務
package com.dfyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ServiceProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProducerApplication.class, args);
}
}
編寫接口
package com.dfyang.serviceProducer.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProducerController {
@RequestMapping("/service")
public String service() {
return "服務A";
}
}
啓動服務,輸入localhost:8761
3.創建服務消費者service-consumer(eureka client)
創建一個Spring Boot工程service-consumer
pom.xml
注意這裏多了一個spring-cloud-starter-netflix-ribbon
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dfyang</groupId>
<artifactId>service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</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>
</project>
application.yml
server:
port: 8081
spring:
application:
name: service-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
在啓動類似加上@EnableDiscoveryClient(該註解與@EnableEurekaClient均表明是一個Eureka客戶端,但@EnableEurekaClient只能在Eureka使用)
同時將RestTemplate作爲Bean交給spring管理
@LoadBalanced表示使用輪詢,這裏不加會報java.net.UnknownHostException,不知道是不是版本的問題
package com.dfyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
消費者獲取服務
service-producer是服務提供者的 [spring.application.name]
service是服務提供者提供的服務
package com.dfyang.serviceConsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consume")
public String consume() {
return restTemplate.getForObject("http://service-producer/service", String.class);
}
}
訪問localhost:8081/consume