继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

如何创建“服务注册中心”

qq_IT达人_0
关注TA
已关注
手记 107
粉丝 54
获赞 267

创建一个基础的Spring Boot工程,命名为eureka-server,并在pom.xml中引入需要的依赖内容:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency></dependencies></dependencyManagement>

通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,比如下面的例子:

@EnableEurekaServer
@SpringBootApplicationpublic
 class Application 
 {public static void main(String[] args) 
 {new SpringApplicationBuilder(Application.class).
 web(true).run(args);
 }
 }

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:

spring.application.name=eureka-server  
server.port=1001  
eureka.instance.hostname=localhost  
eureka.client.register-with-eureka=false  
eureka.client.fetch-registry=false

为了与后续要进行注册的服务区分,这里将服务注册中心的端口通过server.port属性设置为1001。

从现在开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,希望可以帮助更多的好学者。大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。


打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP

热门评论

希望可以帮助到你们哦~~

查看全部评论