SpringBoot2.1+SpringCloud(一)——注冊中心搭建(Eureka)


一、版本說明:

SpringBoot:2.1.6.RELEASE

SpringCloud:Greenwich.RELEASE

二、作用及功能說明:

注冊中心:是將多個微服務統計進行管理,主要起注冊及發現的作用,是微服務架構的一個總要環節,是多個微服務之間通訊的保障及支撐;

Eureka:可以做為服務端,同時也可以作為客戶端,支持高可用;

三、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>【項目自定義】</groupId>
 9 <artifactId>【項目自定義】</artifactId>
10 <version>【版本自定義】</version>
11 <name>【名稱自定義】</name>
12 <packaging>jar</packaging>
13 <description>服務注冊中心——獨立啟動項目</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應用 -->
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=error
 6 
 7 spring.application.name=registration-center-independent-service
 8 
 9 #主機名,會在控制頁面中顯示
10 eureka.instance.hostname=【IP】
11 #使用ip替代實例名
12 eureka.instance.prefer-ip-address=false
13 #設置實例的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 # 關閉自我保護
21 eureka.server.enable-self-preservation=true
22 # 清理服務器時間
23 eureka.server.eviction-interval-timer-in-ms=5000
24 #通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server.
25 eureka.client.register-with-eureka=false
26 eureka.client.fetch-registry=false
27 eureka.client.serviceUrl.defaultZone=http://【登錄或鏈接的用戶名】:【登錄或鏈接的密碼】@【IP】:7000/eureka/

注意:上面紅字部分,在服務端,一定要設置為False;

這里科普一下:在后台的代碼處理邏輯中存在名稱規范,例如:

1)eureka.client.register-with-eureka與eureka.client.registerWithEureka等價;

2)eureka.client.fetch-registry與eureka.client.fetchRegistry等價;

五、代碼部分

代碼部分比較簡單,僅需要在啟動入口類上進行處理即可;

 1 /**
 2 * 類描述: 注冊中心啟動入口
 3 * @author : 王雷
 4 * @date : 2019/6/28 0028 下午 4:56
 5 */
 6 @EnableEurekaServer
 7 @SpringBootApplication
 8 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      * 升級有風險;入坑須謹慎;
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 }

 

六、項目開源地址

目前項目再進行一些調整,后期再進行開放

七、結束語

在整個使用過程中,發現SpringBoot的升級迭代版本間變化比較大,所以在升級版本的時候需要多嘗試,閱讀相關資料,不然很多錯誤都不知道在哪里;

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM