先說說Eureka
服務注冊中心高可用
簡單點說就是啟用多個Eureka Server(一般2個就可以了,我這里用了3個),然后讓3個Server 互相注冊。

創建服務注冊中心
創建一個普通的Spring Boot工程
首先我們需要創建一個普通的Spring Boot工程,命名為eureka-server。
添加Eureka依賴
工程創建成功之后,向pom.xml文件中添加eureka-server的依賴(這里有個javaJDK的坑,1.8版本以上的會有報錯,需要額外添加依賴),添加完依賴之后,pom.xml文件如下所示:
<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>eureka-server</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 必須要引入 springboot parent ,幫我們實現了很多jar包的依賴管理 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <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>
<!-- 用於安全校驗 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- JDK 版本1.8以上的需要額外添加依賴 --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
啟動一個服務注冊中心
啟動一個服務注冊中心的方式很簡單,就是在Spring Boot的入口類上添加一個@EnableEurekaServer
注解,如下:
@EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
配置服務注冊中心
在Spring Boot的配置文件application.yml中(如果3個Eureka Server的端口分別是8761、8762、8763,以8761為例,這里有個坑,開始yml配置里,
serviceUrl沒有用駝峰式的,服務一直注冊不上去),另外這里配置的hostname,記得改系統的host文件,配置對應的ip地址,server 端yml寫法如下:
spring: application: name: eureka-server2 #安全登入用戶設置 security: user: name: skyworth password: skyworth2 server: port: 8762 eureka: instance: hostname: eureka-server2 client: #表示是否將自己注冊到Eureka Server register-with-eureka: false #表示是否從Eureka Server獲取注冊信息 fetch-registry: false serviceUrl: #設置與Eureka Server交互的地址,查詢服務和注冊服務都需要依賴這個地址 defaultZone: http://skyworth:skyworth1@eureka-server:8761/eureka/,http://skyworth:skyworth3@eureka-server3:8763/eureka/
到這里 Server 端就差不多了,下面是Cline端,Cline端的依賴基本是一樣的,主要區別在啟動類和配置文件的區別
client端yml配置如下:
spring: application: name: eureka-client server: port: 8760 eureka: instance: hostname: eureka-client client: serviceUrl: defaultZone: http://skyworth:skyworth1@eureka-server:8761/eureka/,http://skyworth:skyworth2@eureka-server2:8762/eureka/,http://skyworth:skyworth3@eureka-server3:8763/eureka/
client端啟動類:
@SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
測試
項目啟動后 查看eureka-server: http://localhost:8761/,http://localhost:8762/,http://localhost:8763/,發現服務已經注冊進去。
另外提一下,為了安全,client端可以增加至多個,並增加用戶名,密碼的登入驗證。
注:當增加用戶密碼的登入模式后,可能會出現服務注冊不進去的情況,解決方案,在eurka服務中添加一個安全認證類
/** * */ package com.skyworth.config; import org.springframework.context.annotation.Configuration; 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.http.SessionCreationPolicy; /** * Copyright: Copyright (c) 2018 skyworth * * @ClassName: WebSecurityConfig.java * @Description: 該類的功能描述 * * @version: v1.0.0 * @author: Administrator * @date: 2018年8月1日 下午4:14:12 * * Modification History: * Date Author Version Description *---------------------------------------------------------* * 2018年8月1日 Administrator v1.0.0 修改原因 */ @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { /** * 高版本的丟棄了 * * security: * basic: * enabled: true * * 配置,應該使用以下方式開啟 * * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { // Configure HttpSecurity as needed (e.g. enable http basic). http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER); http.csrf().disable(); //注意:為了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic, // 如果是form方式,不能使用url格式登錄 http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); } }