微服务构建与部署 - Spring Boot实践

来源:互联网 发布:centos查看文件夹大小 编辑:程序博客网 时间:2024/05/16 08:10

当前服务器架构不再是结构复杂的单一系统,取而代之的是目前较火的微服务架构及容器化部署。而在Java系Spring生态中新出的Spring Boot框架,致力于简化Spring样版化配置,快速、敏捷开发Rest风格的服务或搭建嵌入式Web应用程序等,是构建微服务的利器。


Spring Boot特点

1、创建可独立运行的Spring应用

2、直接嵌入Tomcat或Jetty服务器,不需要部署war文件

3、尽可能根据项目以来来自动配置Spring框架

4、文档、社区支持较少,版本迭代速度太快。


基于Spring Boot搭建rest微服务

项目创建:

Spring Boot项目搭建有两种方式:1、通过访问https://strat.spring.io填写项目信息,生成源码并下载;2、Maven手工构建,如下示例,新建一个普通的maven项目,在pom.xml中添加Web依赖、单元测试依赖即编译插件等,作如下配置:

<dependencies>  <dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>3.8.1</version>    <scope>test</scope>  </dependency>  <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.boot</groupId>    <artifactId>spring-boot-devtools</artifactId>    <optional>true</optional>  </dependency></dependencies><dependencyManagement>  <dependencies>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-dependencies</artifactId>      <version>1.3.3.RELEASE</version>      <type>pom</type>      <scope>import</scope>    </dependency>  </dependencies></dependencyManagement>

Spring Boot只是简化了Spring的配置与启动,具体业务逻辑还需要开发人员自己实现,所以我们添加额外的配置类与启动代码:

@Servicepublic class RandomService {    public String getRandom(intlength) {        StringBuilder sb = new StringBuilder();        for (int i = 0;i < length;i++) {            sb.append("t");        }        return sb.toString();    }}

@SpringBootApplication@RestController@RequestMapping("/login")public class LoginController {    @Autowired    RandomService randomService;    @RequestMapping("/getRandom")    public Map<String, Object> getRandom(@RequestParam String random) {        String serverRandom = randomService.getRandom(8);        Map<String, Object> retMap = new HashMap<String, Object>();        retMap.put("secRan", random.substring(0,8) + serverRandom);        retMap.put("linRan", random);        return retMap;    }    public static void main(String[] args) {        SpringApplication.run(LoginController.class, args);    }}

直接运行该LoginService.main()方法,服务即可发布。

其中的@SpringBootApplication会添加以下几个注解:

  • @Configuration 将标记的类作为配置类,它是application Context的bean定义源,类中@Bean标记的方法可以为spring装配bean
  • @EnableAutoConfiguration 通知Spring Boot根据classpath设置与属性值添加bean。
  • @EnableWebMvc 将应用标记为web应用,它会配置springMVC项目相关的设置,比如DispatcherServlet。当spring boot在类路径下发现spring-webmvc依赖时会自动添加,所以不是必需的。
  • @ComponentScan component自动扫描。

测试访问:http://localhost:8088/login/getRandom?random=abcdefgh


安全

Spring Boot通过Spring Security实现安全控制,包括权限访问、跨站攻击防护等。

在pom.xml中添加对Spring Security依赖:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-security</artifactId></dependency>

启动应用,Boot会提供一个默认的用户账号user和默认角色USER,并在控制台上输出随机生成的密码,以保护服务只能提供给特定角色的调用者。


监控

Spring boot提供的一些开箱即用的应用非常容易使用,比如监控,你只需要在pom文件中引入Spring-boot-actuator模块:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-actuator</artifactId></dependency>

引入之后,spring boot是默认开启监控的,可以使用http、jmx、ssh、telnet等管理和监控应用,健康(health)、数据采集(metrics gathering)等会自动加入到应用中。

Health:运行应用你可以在浏览器中输入:http://localhost:8080/health,就可以看到默认的监控信息了: 

{"status":"UP","diskSpace":{"status":"UP","total":68350373888,"free":39228350464,"threshold":10485760}}

信息包括程序执行状态以及基本的磁盘信息。


Beans

显示一个应用中所有Spring Beans完整列表:

[{"context":"application:8088","parent":null,"beans":[{"bean":"loginController","scope":"singleton","type":"com.cmbc.firefly.rest.LoginController$$EnhancerBySpringCGLIB$$6fcb6ad7","resource":"null","dependencies":["randomService"]},{"bean":"randomService","scope":"singleton","type":"com.cmbc.firefly.rest.RandomService","resource":"file [C:/工作/源码/testDemo/target/classes/com/cmbc/firefly/rest/RandomService.class]","dependencies":[]},…]

Dump:执行一次线程转储。

Metrics:显示当前应用的指标信息。

Trace:显示Trace信息(默认为最新的一些Http请求)。


单元测试

工程添加对测试模块的依赖,如下:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope></dependency>

在创建的java类中添加注解:

@RunWith(SpringJUnit4ClassRunner.class)// SpringJUnit支持,由此引入Spring-Test框架支持! @SpringApplicationConfiguration(classes= SpringBootApplication.class)// 指定我们SpringBoot工程的Application启动类@WebAppConfiguration // 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。

接下来直接编写测试方法并使用@Test注解标注即可。测试类像平常开发业务逻辑一样,直接@Autowired来注入我们要测试的类实例即可。




0 0
原创粉丝点击