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的升级迭代版本间变化比较大,所以在升级版本的时候需要多尝试,阅读相关资料,不然很多错误都不知道在哪里;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值