要在项目中包含Eureka服务器,请使用组org.springframework.cloud和工件id spring-cloud-starter-eureka-server的启动器。
前面说到spring-cloud-config-server配置中心,搭建以及访问。本篇就是使用了。
同理创建子项目spring-cloud-netflix-eureka项目,添加依赖。pom.xml示例:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
创建配置文件application.yml示例:
server:
# 指定端口号
port: 20000
servlet:
context-path: /register
# 无效
#spring:
# application:
# name: spring-cloud-netflix-eureka
# profiles:
# active: dev
# cloud:
# config:
# uri: http://localhost:8888/config/
# label: master
再创建配置文件bootstrap.yml 示例:
spring:
application:
# 名称
name: spring-cloud-netflix-eureka
profiles:
# 开发环境
active: dev
cloud:
config:
# spring-cloud-config-server链接
uri: http://localhost:8888/config/
# git分支
label: master
GIT文件
这里有两点注意的地方:
1:截图圈好的,和上面注释的一致。
2:以上信息必须写在bootstrap.yml文件里面。写在application.yml无效。
修改文件spring-cloud-netflix-eureka-dev.yml添加以下配置:
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
准备好这些后创建启动类:
package bertram.springcloud.study;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>启动类<p>
* @Author Bertram.Wang
* @Date 2019年3月12日
*/
@SpringBootApplication
// 标识Eureka服务器
@EnableEurekaServer
@RestController
public class Application {
@Autowired
Environment environment;
@RequestMapping("/wang")
public String wang() {
String property = environment.getProperty("setting.wang","未定义");
return property;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动访问http://localhost:20000/register/wang
测试字段访问访问http://localhost:20000/register/
Eureka服务器启动成功感谢关注!
网友评论