Springboot微服务

什么是springboot

springboot是spring组织生产的一个后端全栈框架(不包括页面的技术)

作用:

优点:
	1)提倡零配置, 专注于业务代码, 提高开发效率.(抛弃了大量的配置文件)
	2)不用整合框架结构,解决了版本不兼容问题(简化项目依赖管理)
缺点:
	springboot只适合小项目使用(对性能要求不高).

springboot Demo

1、导入依赖

使用springBoot的项目打包成jar包,而不是war包部署到tomcat上,springboot通过Maven引入tomcat插件,通过启动器调用tomcat插件来启动项目。

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
	<version>1.4.0.RELEASE</version>
  </parent>  
  <dependencies>
  	<!-- web项目springMvc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

2、创建一个引导类

类名Application ,置于controller、service包同层目录下。
类上加上 @SpringBootApplication 注解,将此类作为启动器

/**
 * 这是一个启动类
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        //启动当前项目中的tomcat插件, 运行当前项目
        SpringApplication.run(Application.class,args);
    }
}

3、创建一个Controller

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world " ;
    }
}

无需其他配置文件,直接可以浏览器访问这个方法

4、(可选)创建配置文件 application.properties(名字不能变)

调用tomcat插件后,它会默认到resources中找application.properties(有就加载里面的内容;没有就使用默认配置)

server.port=8088  //更改启动端口号,key名唯一

url=http://www.baidu.com  //自定义属性(key:value)

spring.activemq.broker-url=tcp://192.168.200.128:61616  //引用外部的消息中间件

在Controller中加载配置信息需要注入 Environment 对象

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private Environment env;

    @RequestMapping("/hello")
    public String hello(){
        String url = env.getProperty("url");
        return "hello world " + url;
    }
}

springboot 中整合activeMq

1、导入依赖

  	<!-- 这不是原生的activeMq,是springboot封装的 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

2、编写发送方

@RestController
@RequestMapping("/testJms")
public class TestJms {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/send")
    public void send(String text){
        //发送消息到消息服务器, 第一个参数:指定队列名称, 第二个参数:发送的内容
        jmsMessagingTemplate.convertAndSend("itcast", text);
    }

    @RequestMapping("/sendmap")
    public void sendMap(){
        Map map=new HashMap();
        map.put("mobile", "13900001111");
        map.put("content", "恭喜获得10元代金券");
        jmsMessagingTemplate.convertAndSend("itcast_map",map);
    }
}

3、编写接收方

@Component
public class Consumer {

    /**
     * 定义监听器(从类级变为方法级,一个方法就是一个监听器),
     * 监听从消息服务器发送来的消息,destination是指定从哪个队列中接收消息
     * @param text
     */
    @JmsListener(destination="itcast")
    public void readMessage(String text){
        System.out.println("接收到消息:"+text);
    }

    @JmsListener(destination="itcast_map")
    public void readMap(Map map){
        System.out.println(map);
    }
}

这样使用的是springBoot内嵌ActiveMq服务
如果要使用外部的服务器,需要在application.properties中加入
spring.activemq.broker-url=tcp://192.168.200.128:61616

发布了33 篇原创文章 · 获赞 2 · 访问量 957

猜你喜欢

转载自blog.csdn.net/Rhin0cer0s/article/details/103964775