簡單springboot及springboot cloud環境搭建

springboot使用特定的方式,簡化了spring的各類xml配置文件,並經過maven或者gradle,完成所需依賴,使用springboot maven插件,可直接輸出可運行的jar包,省去了tomcat等容器的部署,使得基於http的網絡應用開發更加方便快捷。html

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

springboot基礎應用搭建

首先創建maven工程。web

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

<?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>        
View Code

創建一個啓動類,便可運行。默認端口爲8080。apache

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容器中。tomcat

springboot cloud註冊中心

爲了對多個springboot應用進行發現以及管理,可以使用eureka服務。在啓動類中增長@EnableEurekaServer便可。同時添加配置文件。springboot

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.propertiesapp

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);
    }
}
相關文章
相關標籤/搜索