简述springboot及springboot cloud环境搭建

 更新时间:2017年07月13日 10:49:03   作者:8blues  
这篇文章主要介绍了简述springboot及springboot cloud环境搭建的方法,包括spring boot 基础应用环境搭建,需要的朋友可以参考下

springboot使用特定的方式,简化了spring的各种xml配置文件,并通过maven或者gradle,完成所需依赖,使用springboot maven插件,可直接输出可运行的jar包,省去了tomcat等容器的部署,使得基于http的网络应用开发更加方便快捷。

spring中配置文件官方文档http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/

springboot基础应用搭建

首先建立maven工程。

pom.xml文件配置如下(每一个maven工程中的,除了自身GAV外,都使用此配置)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mahuan</groupId>
 <artifactId>producer</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>producer</name>
 <description>Demo project for Spring Boot</description>
 <!-- lookup parent from repository -->
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
  <relativePath />
 </parent>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
  </dependency>
    <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
     <fork>true</fork>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR6</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
</project>  

建立一个启动类,即可运行。默认端口为8080。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}

springboot启动时,会自动扫描所有class文件,发现@Service、@RestController等注解的class文件,加载到IOC容器中。

springboot cloud注册中心

为了对多个springboot应用进行发现以及管理,可使用eureka服务。在启动类中增加@EnableEurekaServer即可。同时添加配置文件。

eureka注册中心,会等待应用主动向其进行注册,而eureka注册中心在发现了新的应用后,会持续向应用发送心跳,判断其是否存活,并定时向注册中心发送心跳包,告知其存活情况。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}
application.properties
server.port=1111
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

eureka.client.registerWithEureka表示eureka中心不会自己注册自己。

springboot cloud生产者

如果springboot应用配置了eureka注册中心,并在启动类中增加了@EnableDiscoveryClient注解,应用启动后会注册到指定的注册中心中。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}
application.properties配置
server.port=1112
spring.application.name=compute-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

其中spring.application.name是必须要有的配置,是此springboot应用的标识。实现不同功能的springboot应用,应有不同的name。

eureka.client.serverUrl.defaultZone是注册中心的地址信息,同注册中心配置的地址相同。

此外由于注册中心的存在,我们不必再固定生产者的启动端口,可通过启动程序控制springboot启动时,使用的端口。

当然固定的端口号,会更加方便运维。

注意,此时程序代码对于启动的配置操作,是优先于配置文件配置的。

@SpringBootApplication 
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{ 
 public static void main(String[] args) { 
  SpringApplication.run(Application.class, args); 
 } 
 @Override 
 public void customize(ConfigurableEmbeddedServletContainer container) { 
  ///TODO 获取未被占用的端口
  int port=8080
  container.setPort(port); 
 } 
}

springboot cloud消费者

首先application.properties中要有eureka的配置信息,同上述的配置信息相同。

springboot的消费者有两种形式实现。

RestTemplate

在启动类中增加@Bean

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class App {
 @Bean
 @LoadBalanced
 RestTemplate restTemplate() {
  return new RestTemplate();
 }
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}

建立一个Controller类

package com.mahuan.producer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class FirstContrller2 {
 @Autowired
 RestTemplate restTemplate;
 @RequestMapping(value = "/first")
 @ResponseBody
 public String first() {
  return restTemplate.getForEntity("http://compute-service/first", String.class).getBody();
 }
}

其中标红部分,为需要调用的application的name,后面为调用的path。如果在注册中心中有多个拥有相同application.name的应用,会自动进行负载均衡。

Feign

建立一个interface

package com.mahuan.producer.controller;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "compute-service")
public interface ComputeService {
 @RequestMapping(method = RequestMethod.GET, value = "/first")
 String first();
}

其中@FeignClient说明要调用的application.name,@RequestMapping中说明调用的应用path。

在Controller类中,直接@Autowired此接口即可。

同时启动类中,需要增加@EnableFeignClients注解。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}

以上所述是小编给大家介绍的springboot及springboot cloud环境搭建,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 详解java JDK 动态代理类分析(java.lang.reflect.Proxy)

    详解java JDK 动态代理类分析(java.lang.reflect.Proxy)

    这篇文章主要介绍了详解java JDK 动态代理类分析(java.lang.reflect.Proxy)的相关资料,需要的朋友可以参考下
    2017-06-06
  • SpringBoot文件上传功能的实现方法

    SpringBoot文件上传功能的实现方法

    这篇文章主要介绍了SpringBoot文件上传功能的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • 导出maven项目依赖的jar包(图文教程)

    导出maven项目依赖的jar包(图文教程)

    下面小编就为大家带来一篇导出maven项目依赖的jar包(图文教程)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • 关于Lombok简化编码使用及说明

    关于Lombok简化编码使用及说明

    这篇文章主要介绍了关于Lombok简化编码使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Java BeanMap实现Bean与Map的相互转换

    Java BeanMap实现Bean与Map的相互转换

    这篇文章主要介绍了利用BeanMap进行对象与Map的相互转换,通过net.sf.cglib.beans.BeanMap类中的方法来转换,效率极高,本文给大家分享实现代码,感兴趣的朋友一起看看吧
    2022-11-11
  • 图文详解java内存回收机制

    图文详解java内存回收机制

    这篇文章主要以图文结合的方式为大家详细介绍了java内存回收机制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • 浅谈springboot 属性定义

    浅谈springboot 属性定义

    本篇文章主要介绍了浅谈springboot 属性定义,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • java链式创建json对象的实现

    java链式创建json对象的实现

    本文主要介绍了java中如何通过最简单的方式实现链式创建json对象,解决创建json代码臃肿的问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Java实战之电影在线观看系统的实现

    Java实战之电影在线观看系统的实现

    这篇文章主要介绍了如何利用Java实现电影在线观看系统,文中用到的技术有:JSP、Spring、SpringMVC、MyBatis等,感兴趣的可以了解一下
    2022-04-04
  • Spring中的@RefreshScope注解作用

    Spring中的@RefreshScope注解作用

    这篇文章主要介绍了Spring中的@RefreshScope注解作用详解,@RefreshScope注解是Spring Cloud中的一个重要注解,用于实现动态刷新配置的功能,当我们在应用程序中使用@Value注解获取配置属性时,如果配置发生变化,需要重启应用程序才能生效,需要的朋友可以参考下
    2023-10-10

最新评论