一、搭建环境
创建maven项目
打开eclipse从左上角选择file—new—查询框输入maven—选择maven project—next—勾选create a simple project(跳过骨架),项目配置信息如下图1.1所示,点击finish完成meaven项目的创建。点击window选择show view,Package Explorer可以在包管理器中查看项目。
项目测试准备
点击项目名左侧的折叠栏,在pom.xml中添加配置依赖
双击pom.xml,打开文件,在文件下侧选择pom.xml文件模式,添加依赖如下,并保存。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在src/main/resources下创建配置文件file,命名为application.properties
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
在src/main/java下新建包com.boot.cloud,包下创建Class,命名为Application。
编写测试类
@EnableEurekaServer
@SpringBootApplication
publicclassApplication {
publicstaticvoid main(String[] args) {
newSpringApplicationBuilder(Application.class).web(true).run(args);
}
}
完成后点击开始按钮,进行测试。
测试结果如图
访问界面如下:
二、测试访问数据
1.添加依赖
1)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
2)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.配置application.properties
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
3.HelloController类
@RestController
publicclassHelloController{
privatefinal Logger logger=Logger.getLogger(getClass());
@Autowired
privateDiscoveryClientclient;
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String index(){
ServiceInstanceinstance=client.getLocalServiceInstance();
logger.info("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
return"hello world";
}
}
4.HelloApplication类
@EnableDiscoveryClient
@SpringBootApplication
publicclassHelloApplication{
publicstaticvoid main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
运行界面
访问界面
注意:
由于主函数在com.boot.cloud包下,所以只能扫描到这个包及其子包下面的类,因此要将HelloController类放在可以扫描到的范围内。