热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

简述springboot及springbootcloud环境搭建

这篇文章主要介绍了简述springboot及springbootcloud环境搭建的方法,包括springboot基础应用环境搭建,需要的朋友可以参考下

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外,都使用此配置)

<&#63;xml version="1.0" encoding="UTF-8"&#63;>

 4.0.0
 com.mahuan
 producer
 0.0.1-SNAPSHOT
 jar
 producer
 Demo project for Spring Boot
 
 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.1.RELEASE
  
 
 
  UTF-8
  UTF-8
  1.8
 
 
  
   org.springframework.boot
   spring-boot-starter-web
  
  
   org.springframework.boot
   spring-boot-starter-test
   test
  
  
   org.springframework.cloud
   spring-cloud-starter-config
  
  
   org.springframework.cloud
   spring-cloud-starter-eureka
  
  
   org.springframework.cloud
   spring-cloud-starter-eureka-server
  
  
   org.springframework.cloud
   spring-cloud-starter-feign
  
    
   org.springframework.boot
   spring-boot-devtools
   true
  
 
 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
    
     true
    
   
  
 
 
  
   
    org.springframework.cloud
    spring-cloud-dependencies
    Camden.SR6
    pom
    import
   
  
 
  

建立一个启动类,即可运行。默认端口为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环境搭建,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


推荐阅读
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 标题: ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
  • SpringMVC工作流程概述
    SpringMVC工作流程概述 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文介绍了Java的集合及其实现类,包括数据结构、抽象类和具体实现类的关系,详细介绍了List接口及其实现类ArrayList的基本操作和特点。文章通过提供相关参考文档和链接,帮助读者更好地理解和使用Java的集合类。 ... [详细]
  • 本文介绍了一种图片处理应用,通过固定容器来实现缩略图的功能。该方法可以实现等比例缩略、扩容填充和裁剪等操作。详细的实现步骤和代码示例在正文中给出。 ... [详细]
  • 本文介绍了Cocos2dx学习笔记中的更新函数scheduleUpdate、进度计时器CCProgressTo和滚动视图CCScrollView的用法。详细介绍了scheduleUpdate函数的作用和使用方法,以及schedule函数的区别。同时,还提供了相关的代码示例。 ... [详细]
  • 本文介绍了在sqoop1.4.*版本中,如何实现自定义分隔符的方法及步骤。通过修改sqoop生成的java文件,并重新编译,可以满足实际开发中对分隔符的需求。具体步骤包括修改java文件中的一行代码,重新编译所需的hadoop包等。详细步骤和编译方法在本文中都有详细说明。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • Jboss的EJB部署描述符standardjaws.xml配置步骤详解
    本文详细介绍了Jboss的EJB部署描述符standardjaws.xml的配置步骤,包括映射CMP实体EJB、数据源连接池的获取以及数据库配置等内容。 ... [详细]
  • 在IDEA中运行CAS服务器的配置方法
    本文介绍了在IDEA中运行CAS服务器的配置方法,包括下载CAS模板Overlay Template、解压并添加项目、配置tomcat、运行CAS服务器等步骤。通过本文的指导,读者可以轻松在IDEA中进行CAS服务器的运行和配置。 ... [详细]
author-avatar
咖喱2502894907
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有