Spring Boot环境搭建

一、搭建环境

创建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);

       }

}

完成后点击开始按钮,进行测试。

测试结果如图

并访问网址http://localhost:1111/

访问界面如下:

 

二、测试访问数据

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.classargs);

       }

}


运行界面

访问界面

注意:

由于主函数在com.boot.cloud包下,所以只能扫描到这个包及其子包下面的类,因此要将HelloController类放在可以扫描到的范围内。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值