搭建注冊中心
導(dǎo)入pom依賴
<?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>sakura</artifactId>
<groupId>com.warm-sun</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
?
<artifactId>registry-center</artifactId>
<packaging>jar</packaging>
<name>registry-center</name>
<description>微服務(wù)注冊中心</description>
<!-- 引入spring boot的依賴 -->
?
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
?
<dependencies>
<!--注冊中心選擇eureka 也可以使用 zookeeper consul 這里自己擴展-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--security 框架-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- 注冊中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--web 框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除 tomcat容器-->
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器 基于內(nèi)存 性能秒殺tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
</dependencies>
?
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<imageName>${registry.url}/${project.name}:0.0.1</imageName>
<dockerHost>${docker.url}</dockerHost>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
</plugins>
</build>
</project>
編寫bootstrap.yml拜姿,配置eureka注冊信息
#配置端口號
server:
port: 2000
?
# 配置eureka 安全驗證賬戶
spring:
security:
user:
name: sakura
password: Sakura*&^999
application:
name: registry-center
cloud:
config:
enabled: false
?
# eureka 配置
eureka:
instance:
hostname: localhost
prefer-ip-address: false
?
client:
#只有一個eureka注冊中心 不需要自己向自己注冊 當(dāng)服務(wù)需要搭建集群的時候 registerwitheurka設(shè)置為true
registerWithEureka: false
# 表示從其他的配置中心同步數(shù)據(jù)福压,默認為true;當(dāng)前只有一個配置中心嫩舟,所以設(shè)置為false
fetchRegistry: false
serviceUrl:
# 多個地址用,隔開 并相互注冊
defaultZone: http://sakura:Sakura*&^999@${eureka.instance.hostname}:${server.port}/eureka/
#配置eureka心跳等
server:
eviction-interval-timer-in-ms: 4000
enable-self-preservation: false
renewal-percent-threshold: 0.9</pre>
配置啟動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
?
/** 注冊中心服務(wù)端
* @author tuxiaolei
* @date 2018/11/23
*/
@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryApplication.class,args);
}
}
配置安全登錄信息
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
?
/**
* @author tuxiaolei
* @date 2018/11/23
*/
@EnableWebSecurity
public class RegistrySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers().permitAll()
.anyRequest()
//開啟表單驗證
.authenticated().and().httpBasic();
}
}
這樣我們的Eureka配置中心就完成了
Eureka 架構(gòu)
首先我們要明白幾個概念:
Application Client(Service Consumer) : 服務(wù)消費者、應(yīng)用客戶端,在微服務(wù)架構(gòu)中很多微服務(wù)既是服務(wù)的提供者也是服務(wù)的消費者术唬,這種情況下,微服務(wù)是需要往注冊中心注冊的滚澜。從注冊中心獲取到服務(wù)提供者列表粗仓。
Application Service(Service Provider): 服務(wù)的提供者,需要向注冊中心注冊设捐、續(xù)約借浊、下線 ,往注冊中心發(fā)送心跳
Eureka Server: 注冊中心服務(wù)端挡育,接收客戶端的心跳巴碗,默認90秒沒接收客戶端的心跳下線這個客戶端。
Eureka Server: 注冊中心客戶端即寒,注冊客戶端信息橡淆,方便服務(wù)消費者獲取到提供者的地址、端口等的母赵。
要理解eureka 首先我們得明白 eureka解決了什么:
我們知道:現(xiàn)在開發(fā)已經(jīng)有傳統(tǒng)的單系統(tǒng)開發(fā)轉(zhuǎn)變成了實現(xiàn)流行的分布式開發(fā)和微服務(wù)開發(fā)逸爵,不同的業(yè)務(wù)被劃分成不同的模塊,部署到不同的服務(wù)器實例上凹嘲,對于并發(fā)量大的模塊(比如:訂單模塊师倔、團購模塊)往往來說要搭建集群,那么我們在服務(wù)調(diào)用的時候怎么知道我部署的服務(wù)在哪個服務(wù)器上周蹭,同個服務(wù)的多個實例我應(yīng)該調(diào)取哪個趋艘,哪個服務(wù)是健康的,哪個服務(wù)宕機了凶朗,我們的eureka就很好的解決了這些問題瓷胧。
Eureka 就像一個智能版的微信,我想跟A聊天的時候棚愤,我就去找A的通訊信息搓萧,新結(jié)識了B,將B的信息存到通訊錄里宛畦,當(dāng)B改了頭像瘸洛,那么第一時間我也能知道。只不過我們的Eureka更為強大次和!
根據(jù)上續(xù)場景反肋,我們來考慮一下如何實現(xiàn):
1.服務(wù)注冊后如何及時發(fā)現(xiàn)注冊信息
2.服務(wù)宕機后怎,如何及時下線服務(wù)
3.服務(wù)如何做水平擴展斯够,解決性能瓶頸
4服務(wù)注冊發(fā)現(xiàn)后囚玫,調(diào)用微服務(wù)怎么做路由
5.服務(wù)發(fā)生異常怎么做降級處理
6.注冊中心如何實現(xiàn)自身高可用
這里我們先留下懸念喧锦,我會在后續(xù)的博客中读规,針對eureka的源碼進行解析抓督。