javascript
SpringBoot2.1+SpringCloud:注册中心搭建(Eureka)
一、版本說明:
SpringBoot:2.1.6.RELEASE
SpringCloud:Greenwich.RELEASE
二、作用及功能說明:
注冊中心:是將多個(gè)微服務(wù)統(tǒng)計(jì)進(jìn)行管理,主要起注冊及發(fā)現(xiàn)的作用,是微服務(wù)架構(gòu)的一個(gè)總要環(huán)節(jié),是多個(gè)微服務(wù)之間通訊的保障及支撐;
Eureka:可以做為服務(wù)端,同時(shí)也可以作為客戶端,支持高可用;
三、Maven主要配置說明
1 <modelVersion>4.0.0</modelVersion>2 <parent>3 <groupId>org.springframework.boot</groupId>4 <artifactId>spring-boot-starter-parent</artifactId>5 <version>2.1.6.RELEASE</version>6 <relativePath/> <!-- lookup parent from repository -->7 </parent>8 <groupId>【項(xiàng)目自定義】</groupId>9 <artifactId>【項(xiàng)目自定義】</artifactId> 10 <version>【版本自定義】</version> 11 <name>【名稱自定義】</name> 12 <packaging>jar</packaging> 13 <description>服務(wù)注冊中心——獨(dú)立啟動(dòng)項(xiàng)目</description> 14 15 <properties> 16 <java.version>1.8</java.version> 17 </properties> 18 19 <dependencies> 20 <!-- 注冊中心Jar--> 21 <dependency> 22 <groupId>org.springframework.cloud</groupId> 23 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 24 </dependency> 25 <!-- 提供注冊中心用戶名及密碼的支持 --> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-security</artifactId> 29 </dependency> 30 </dependencies> 31 <!--spring cloud版本--> 32 <dependencyManagement> 33 <dependencies> 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-dependencies</artifactId> 37 <version>Greenwich.RELEASE</version> 38 <type>pom</type> 39 <scope>import</scope> 40 </dependency> 41 </dependencies> 42 </dependencyManagement> 43 44 <!-- 打包spring boot應(yīng)用 --> 45 <build> 46 <finalName>【打包文件名稱】</finalName> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 <configuration> 52 <fork>true</fork> 53 </configuration> 54 </plugin> 55 </plugins> 56 <resources> 57 <resource> 58 <directory>src/main/java</directory> 59 <includes> 60 <include>**/*.xml</include> 61 </includes> 62 <filtering>true</filtering> 63 </resource> 64 </resources> 65 </build>四、配置文件(application.properties)
1 #注冊中心端口2 server.port=【自定義】3 4 #日志級別5 logging.level.root=error6 7 spring.application.name=registration-center-independent-service8 9 #主機(jī)名,會在控制頁面中顯示 10 eureka.instance.hostname=【IP】 11 #使用ip替代實(shí)例名 12 eureka.instance.prefer-ip-address=false 13 #設(shè)置實(shí)例的ID為ip:port 14 eureka.instance.instance-id=${eureka.instance.hostname}:${server.port} 15 16 spring.security.user.name=【登錄或鏈接的用戶名】 17 spring.security.user.password=【登錄或鏈接的密碼】 18 #eureka.datacenter=trmap 19 #eureka.environment=product 20 # 關(guān)閉自我保護(hù) 21 eureka.server.enable-self-preservation=true 22 # 清理服務(wù)器時(shí)間 23 eureka.server.eviction-interval-timer-in-ms=5000 24 #通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個(gè)eureka server. 25 eureka.client.register-with-eureka=false 26 eureka.client.fetch-registry=false 27 eureka.client.serviceUrl.defaultZone=http://【登錄或鏈接的用戶名】:【登錄或鏈接的密碼】@【IP】:7000/eureka/注意:上面紅字部分,在服務(wù)端,一定要設(shè)置為False;
這里科普一下:在后臺的代碼處理邏輯中存在名稱規(guī)范,例如:
1)eureka.client.register-with-eureka與eureka.client.registerWithEureka等價(jià);
2)eureka.client.fetch-registry與eureka.client.fetchRegistry等價(jià);
五、代碼部分
代碼部分比較簡單,僅需要在啟動(dòng)入口類上進(jìn)行處理即可;
1 /**2 * 類描述: 注冊中心啟動(dòng)入口3 * @author : 王雷4 * @date : 2019/6/28 0028 下午 4:565 */6 @EnableEurekaServer7 @SpringBootApplication8 public class RegistrationCenterIndependentServiceApplication {9 10 public static void main(String[] args) { 11 SpringApplication.run(RegistrationCenterIndependentServiceApplication.class, args); 12 } 13 14 /** 15 * 功能描述:最近版本(Boot:2.1.6.RELEASE;Cloud:Greenwich.RELEASE)下,若需要通過用戶名及密碼訪問必須添加下面的處理 16 * 升級有風(fēng)險(xiǎn);入坑須謹(jǐn)慎; 17 * 18 * @author : 王雷 19 * @date : 2019/6/25 0025 上午 11:26 20 */ 21 @EnableWebSecurity 22 static class WebSecurityConfig extends WebSecurityConfigurerAdapter { 23 @Override 24 protected void configure(HttpSecurity http) throws Exception { 25 http.csrf().disable().authorizeRequests() 26 .anyRequest() 27 .authenticated() 28 .and() 29 .httpBasic(); 30 } 31 } 32 }六、項(xiàng)目開源地址
目前項(xiàng)目再進(jìn)行一些調(diào)整,后期再進(jìn)行開放
七、結(jié)束語
在整個(gè)使用過程中,發(fā)現(xiàn)SpringBoot的升級迭代版本間變化比較大,所以在升級版本的時(shí)候需要多嘗試,閱讀相關(guān)資料,不然很多錯(cuò)誤都不知道在哪里;
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2.1+SpringCloud:注册中心搭建(Eureka)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无法连接 服务器/虚拟机中的数据库,报错
- 下一篇: Spring Boot(2.1.2.RE