概述:
下面是搭建了父工程,父工程fengmingmen下面搭建了 A服務提供者模塊userService,B服務消費者模塊userConsumer。
B使用redisTemplate調用A的方法,
因爲存在 1.對外暴露自己的地址 2.將來地址出現變更,還需要及時更新 等問題,
而使用 eureka註冊中心解決這一系列問題。
一.創建項目
1.創建核心父工程
2.配置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>
<groupId>fengmingmen</groupId>
<artifactId>fengmingmen</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>user-service</module>
</modules>
<!-- 配置父級 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<!-- 配置全局屬性 -->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<mapper.version>2.1.5</mapper.version>
<mysql.version>5.1.47</mysql.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring-cloud (導入pom文件)
scope: import 只能在<dependencyManagement>元素裏面配置
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 通用mapper啓動器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.version}</version>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置spring-boot的maven插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二.服務提供者
1.創建user模塊
2.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">
<parent>
<artifactId>springcloud-demo</artifactId>
<groupId>cn.itcast</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
3.模塊結構
4.配置文件(application.yml)
配置數據庫信息
建表語句(自己隨便加點數據進去查詢)
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL COMMENT '用戶名',
`password` varchar(32) NOT NULL COMMENT '密碼,加密存儲',
`name` varchar(50) DEFAULT NULL COMMENT '真實姓名',
`age` int(11) NOT NULL COMMENT '年齡',
`sex` int(11) DEFAULT '1' COMMENT '性別,1男,2女',
`birthday` datetime DEFAULT NULL COMMENT '生日',
`created` datetime NOT NULL COMMENT '創建時間',
`updated` datetime NOT NULL COMMENT '修改時間',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='用戶表';
5.啓動類
package user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication//啓動類註解
@MapperScan("user.mapper")//掃描的mapper路徑
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
6.實體類:
package user.entity;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* 實體類
*/
@Table(name = "user")
@Data
public class User {
//useGeneratedKeys = true 允許JDBC支持自動生成主鍵,需要驅動兼容
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
// 賬號
private String username;
// 密碼
private String password;
// 姓名
private String name;
// 年齡
private Integer age;
// 性別
private Integer sex;
// 生日
private Date birthday;
// 創建日期
private Date created;
// 修改日期
private Date updated;
}
7.mapper:
package user.mapper;
import tk.mybatis.mapper.common.Mapper;
import user.entity.User;
/**
* 數據訪問接口
*/
public interface UserMapper extends Mapper<User> {
}
8.service:
package user.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import user.mapper.UserMapper;
import user.entity.User;
/**
* 業務層
*/
@Service
@Transactional
public class UserService {
@Autowired
private UserMapper userMapper;
/**
* 根據主鍵id查詢用戶
*/
public User findOne(Long id) {
return userMapper.selectByPrimaryKey(id);
}
}
9.controller:
package user.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import user.entity.User;
import user.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
//用於檢查數據庫配置賬號密碼是否有誤
@Value("${spring.datasource.username}")
private String userNme;
//密碼的配置如果有特殊符號需要在yaml文件加引號
@Value("${spring.datasource.password}")
private String password;
@Autowired
private UserService userService;
/**
* 根據主鍵id查詢用戶
*/
@GetMapping("/{id}")
public User findOne(@PathVariable("id") Long id) {
return userService.findOne(id);
}
}
10. 啓動測試
啓動項目,訪問接口:http://localhost:9001/user/1
三.服務消費者
1.創建user消費者模塊
2.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">
<parent>
<artifactId>fengmingmen</artifactId>
<groupId>fengmingmen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.模塊結構
4.配置文件(application.yml)
server:
port: 9002
5.啓動類(註冊 RestTemplate)
package consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
6.實體類
package consumer.entity;
import lombok.Data;
import java.util.Date;
@Data
public class User {
private Long id;
// 賬號
private String username;
// 密碼
private String password;
// 姓名
private String name;
// 年齡
private Integer age;
// 性別
private Integer sex;
// 生日
private Date birthday;
// 創建日期
private Date created;
// 修改日期
private Date updated;
}
}
7.controller(user-consumer跨項目調用user-service)
package consumer.controller;
import consumer.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
/**
* 根據主鍵id查詢用戶
*/
@GetMapping("/{id}")
public User findOne(@PathVariable("id") Long id) {
String url = "http://localhost:9001/user/" + id;
return restTemplate.getForObject(url, User.class);
}
}
8.分別啓動userService跟userConsumer測試
四.Eureka(註冊中心)
1.搭建eureka模塊
2.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">
<parent>
<artifactId>fengmingmen</artifactId>
<groupId>fengmingmen</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-service</artifactId>
<dependencies>
<!-- 配置eureka服務端啓動器(集成了web啓動器) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3.模塊結構
4.啓動類
package fengmingmen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer // 聲明當前應用爲eureka服務(啓用eureka服務)
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
5.配置文件
server:
port: 8761 # eureka服務端,默認端口
spring:
application:
name: eureka-server # 應用名稱,會在Eureka中作爲服務的id標識(serviceId)
eureka:
client:
service-url: # Eureka服務的地址,現在是自己的地址,如果集羣,需要寫其它服務的地址。
defaultZone: http://localhost:8761/eureka
fetch-registry: false #不拉取服務(自已拉取自己的服務沒有必要)
register-with-eureka: false # 不註冊自己(自已註冊到自己沒有必要)
6.訪問 localhost:8761
存在問題:
- 在user-consumer中,我們把url地址硬編碼到了代碼中,不方便後期維護
- user-consumer需要記憶user-service的地址,如果出現變更,可能得不到通知,地址將失效
- user-consumer不清楚user-service的狀態,服務宕機也不知道
- user-service只有1臺服務,不具備高可用性
- 即便user-service形成集羣,user-consumer還需自己實現負載均衡
生產模塊,消費模塊,eruka服務搭建完畢,使用eureka
- eureka實現了服務的自動註冊、發現、狀態監控
- Eureka:就是服務註冊中心(可以是一個集羣),對外暴露自己的地址
- 提供者:啓動後向Eureka註冊自己信息(地址,提供什麼服務)
- 消費者:向Eureka訂閱服務,Eureka會將對應服務的所有提供者地址列表發送給消費者,並且定期更新
- 心跳(續約):提供者定期通過http方式向Eureka刷新自己的狀態
- Eureka分兩個部分: Eureka服務端 + Eureka客戶端(服務提供者或服務消費者)
五.eureka客戶端
5.1首先對於user-service模塊,做註冊
1.在user-service模塊中添加eureka客戶端啓動器依賴
pom.xml
<!-- 配置eureka客戶端啓動器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.在啓動類上開啓Eureka客戶端,添加 @EnableDiscoveryClient 來開啓Eureka客戶端
3.修改application.yml,添加eureka客戶端配置
# 配置eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
4.配置指定應用名稱,作爲服務的id使用。
5.重啓user-service,訪問Eureka
發現已經自動註冊進去註冊中心了,而且名字是我們配置中命名的user-service
,端口爲9001
5.2然後對於user-consumer模塊,做服務發現 (通過服務id發現服務)
1.在user-consumer模塊中添加eureka客戶端啓動器依賴
pom.xml
<!-- 配置eureka客戶端啓動器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.在啓動類上開啓Eureka客戶端,添加 @EnableDiscoveryClient 來開啓Eureka客戶端
3.application.yml
server:
port: 9002
spring:
application:
name: user-consumer # 應用名稱
eureka:
client:
service-url: # eurekaServer地址
defaultZone: http://localhost:8761/eureka
4.修改代碼,用DiscoveryClient類的方法,根據服務id,獲取服務實例
package consumer.controller;
import consumer.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
/**
* 注入發現者
*/
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private RestTemplate restTemplate;
/**
* 根據主鍵id查詢用戶
*/
@GetMapping("/{id}")
public User findOne(@PathVariable("id") Long id) {
// 根據服務id獲取該服務的全部服務實例
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
// 獲取第一個服務實例(因爲目前我們只有一個服務實例)
ServiceInstance serviceInstance = instances.get(0);
// 獲取服務實例所在的主機
String host = serviceInstance.getHost();
// 獲取服務實例所在的端口
int port = serviceInstance.getPort();
// 定義服務實例訪問URL
String url = "http://" + host + ":" + port + "/user/" + id;
System.out.println("服務實例訪問URL: " + url);
return restTemplate.getForObject(url, User.class);
}
}
5.Debug重啓user-consumer
6.項目斷點,postman請求
六.eureka服務端(高可用)
Eureka架構中的三個核心角色:
- 服務註冊中心(Eureka服務端)
Eureka的服務端應用,提供服務註冊和發現功能,就是剛剛我們建立的eureka-server- 服務提供者(Eureka客戶端)
提供服務的應用,可以是SpringBoot應用,也可以是其它任意技術實現,只要對外提供的是Rest風格服務即可。本例中就是我們實現的user-service- 服務消費者(Eureka客戶端)
消費應用從註冊中心獲取服務列表,從而得知每個服務方的信息,知道去哪裏調用服務方。本例中就是我們實現的user-consumer
1.高可用(介紹)
- Eureka Server即服務的註冊中心,在剛纔的案例中,我們只有一個EurekaServer,事實上EurekaServer也可以是一個集羣,形成高可用的Eureka註冊中心。
服務同步
多個Eureka Server之間也會互相註冊爲服務,當服務提供者註冊到Eureka Server集羣中的某個節點時,該節點會把服務的信息同步給集羣中的每個節點,從而實現數據同步。因此,無論客戶端訪問到Eureka Server集羣中的任意一個節點,都可以獲取到完整的服務列表信息。
而作爲客戶端,需要把信息註冊到每個Eureka中:
如果有三個Eureka,則每一個EurekaServer都需要註冊到其它幾個Eureka服務中,例如:有三個分別爲8761、8762、8763,則:
- 8761要註冊到8762和8763上
- 8762要註冊到8761和8763上
- 8763要註冊到8761和8762上
2. 高可用配置(實施)
(搭建三個EurekaServer的集羣,端口分別爲:8761和8762,8763)
2.1 修改eureka-server的配置(application.yml)
server:
port: ${port:8761} # eureka服務端,默認端口
spring:
application:
name: eureka-server # 應用名稱,會在Eureka中作爲服務的id標識(serviceId)
eureka:
client:
service-url: # Eureka服務地址;如果是集羣則是其它服務地址,後面要加/eureka
defaultZone: ${defaultZone:http://localhost:8761/eureka}
# 把fetch-registry和register-with-eureka修改爲true或者註釋掉(默認爲true)
fetch-registry: true # 拉取服務(自已拉取對方的服務)
register-with-eureka: true # 註冊自己(自已註冊到對方)
# 在上述配置文件中的${}表示在jvm啓動時候若能找到對應port或者defaultZone參數則使用,若無則使用後面的默認值。
- 所謂的高可用註冊中心,其實就是把EurekaServer自己也作爲一個服務,註冊到其它EurekaServer上,這樣多個EurekaServer之間就能互相發現對方,從而形成集羣。
2.2同一套eureka server 模塊代碼,啓動三個eureka,形成集羣
8761:
8762:
8763:
2.3啓動三個eureka
3.客戶端註冊服務到集羣
因爲EurekaServer不止一個,因此user-service項目註冊服務或者user-consumer獲取服務的時候,service-url參數需要變化:
# 配置eureka
eureka:
client:
service-url: # EurekaServer地址,多個地址以','隔開
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
七.eureka服務提供者的其他配置
服務註冊
服務提供者在啓動時,會檢測配置屬性中的: eureka.client.register-with-eureka=true
參數是否正確,事實上默認就是true。如果值確實爲true,則會向EurekaServer發起一個Rest請求,並攜帶自己的元數據信息,Eureka
Server會把這些信息保存到一個雙層Map結構中。
- 第一層Map的Key就是服務id,一般是配置中的 spring.application.name 屬性
- 第二層Map的key是服務的實例id。一般host+ serviceId + port,例如:locahost:user-service:9001
值則是服務的實例對象,也就是說一個服務,可以同時啓動多個不同實例,形成集羣。
默認註冊時使用的是主機名,如果想用ip進行註冊,可以在user-service中添加配置:
# 配置eureka
eureka:
instance:
ip-address: 127.0.0.1 # ip地址
prefer-ip-address: true # 更傾向於使用ip,而不是host名稱
服務續約
在註冊服務完成以後,服務提供者會維持一個心跳(定時向EurekaServer發起Rest請求),告訴EurekaServer:“我還活着”。這個我們稱爲服務的續約(renew)
有兩個重要參數可以修改服務續約的行爲:
# 配置eureka
#默認情況下每個30秒服務會向註冊中心發送一次心跳,證明自己還活着。如果超過90秒沒有發送心跳,EurekaServer就會認爲該服務宕機,會從服務列表中移除,這兩個值在生產環境不要修改,默認即可。
eureka:
instance:
lease-renewal-interval-in-seconds: 30 # 服務續約(renew)的間隔時間,默認爲30秒
lease-expiration-duration-in-seconds: 90 # 服務失效時間,默認值90秒
八.Eureka客戶端:服務消費者的其他配置
獲取服務列表
當服務消費者啓動時,會檢測 eureka.client.fetch-registry=true 參數的值,如果爲true,則會從Eureka Server服務的列表只讀備份,然後緩存在本地。並且 每隔30秒 會重新獲取並更新數據。可以通過下面的參數來修改:
eureka:
client:
registry-fetch-interval-seconds: 30 # 獲取服務間隔時間(默認30秒)
九.Eureka服務端:失效剔除、自我保護
- 服務下線
當服務進行正常關閉操作時,它會觸發一個服務下線的REST請求給Eureka Server,告訴服務註冊中心:“我要下線了”。服務中心接受到請求之後,將該服務置爲下線狀態。- 失效剔除
有時我們的服務可能由於內存溢出或網絡故障等原因使得服務不能正常的工作,而服務註冊中心並未收到“服務下線”的請求。相對於服務提供者的“服務續約”操作,服務註冊中心在啓動時會創建一個定時任務,默認每隔一段時間(默認爲60秒)將當前清單中超時(默認爲90秒)沒有續約的服務剔除,這個操作被稱爲失效剔除。
可以通過eureka.server.eviction-interval-timer-in-ms參數對其進行修改,單位是毫秒。- 自我保護
我們關停一個服務,就會在Eureka面板看到一條警告
這是觸發了Eureka的自我保護機制。當服務未按時進行心跳續約時,Eureka會統計服務實例最近5分鐘心跳續約的比例是否低於了85%。在生產環境下,因爲網絡延遲等原因,心跳失敗實例的比例很有可能超標,但是此時就把服務剔除列表並不妥當,因爲服務可能沒有宕機。Eureka在這段時間內不會剔除任何服務實例,直到網絡恢復正常。生產環境下這很有效,保證了大多數服務依然可用,不過也有可能獲取到失敗的服務實例,因此服務調用者必須做好服務的失敗容錯。
可以通過下面的配置來關停自我保護:
eureka:
server:
enable-self-preservation: false # 關閉自我保護模式(缺省爲打開)