Spring Cloud

Spring最擅长的就是集成,把世界上最好的框架拿过来,集成到自己的项目中。

Netflix

Eureka:注册中心

Zuul:服务网关

Ribbon:负载均衡

Feign:服务调用

Hystrix:熔断器

以上只是其中一部分,架构图:

 

第一个 Spring Boot 项目

1创建一个Maven工程

2在pom.xml加入Spring boot依赖

<parent>    
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-parent</artifactId>    
<version>2.1.3.RELEASE</version></parent><dependencies>    
<dependency>        
<groupId>org.springframework.boot</groupId>        
<artifactId>spring-boot-starter-web</artifactId>    
</dependency>
</dependencies>

3创建应用程序启动类 DemoApplication


@SpringBootApplicationpublic class DemoApplication {  
  public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);   
 }}

4创建一个controller类hello controller

@RestControllerpublic class HelloController {  
  @RequestMapping("hello")   
 String hello() {        return "Hello World!";    }}

5运行启动类中的main方法

 6浏览器访问 localhost:8080/hello

 我们没有写任何配置文件,只需要启动Main方法即可打开Web服务访问到controller里面的内容

yaml/properties 文件

我们知道Spring boot 项目只有一个配置文件,那就是application.yml。在spring boot启动时就会读取application.yml中的配置信息

注解

@SpringBootApplication是mian启动的注解,是@SpringBootConfiguration@EnableAutoConfiguration@ComponentScanpublic三个注解的总和

SpringBootConfiguration 表示 Spring Boot 的配置注解,EnableAutoConfiguration 表示自动配置,ComponentScan 表示 Spring Boot 扫描 Bean 的规则,比如扫描哪些包。

@Bean

这个注解是方法级别上的注解,主要添加在 @Configuration@SpringBootConfiguration 注解的类,有时也可以添加在 @Component 注解的类。它的作用是定义一个Bean。

@Value

通常情况下,我们需要定义一些全局变量,都会想到的方法是定义一个 public static 变量,在需要时调用,是否有其他更好的方案呢?答案是肯定的。

    @Value("${server.port}")    String port;    @RequestMapping("/hello")    public String home(String name) {        return "hi "+name+",i am from port:" +port;    }

其中,server.port 就是我们在 application.yml 里面定义的属性,我们可以自定义任意属性名,通过 @Value 注解就可以将其取出来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值