简单springboot及springboot cloud环境搭建

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

<?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。

复制代码
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);
    }
}
复制代码

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!对于搭建Spring BootSpring Cloud项目,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了Java和Maven,并且配置了正确的环境变量。 2. 创建一个新的Spring Boot项目。您可以使用Spring Initializr(https://start.spring.io/)来生成一个基本的Spring Boot项目结构。在这个网站上,您可以选择Spring Boot的版本、项目的元数据以及一些必要的依赖。 3. 导入项目到您的开发工具中。如果您使用的是IntelliJ IDEA,可以选择"Import Project"并选择您刚刚生成的项目文件夹。如果您使用的是Eclipse,可以选择"Import" -> "Existing Maven Projects"。 4. 在项目中添加Spring Cloud依赖。根据您的需求,可以添加Spring Cloud的不同组件,例如Eureka(服务注册与发现)、Ribbon(客户端负载均衡)、Feign(声明式REST客户端)、Zuul(API网关)等。您可以在Maven的pom.xml文件中添加相应的依赖。 5. 配置Spring Cloud组件。根据您选择的组件,您需要在application.properties或application.yml文件中进行相应的配置。例如,如果您使用Eureka进行服务注册与发现,您需要配置Eureka服务器的地址、端口等信息。 6. 编写业务逻辑。根据您的项目需求,编写相应的业务逻辑代码。您可以创建控制器、服务、数据访问层等组件来实现您的功能。 7. 运行项目。您可以在开发工具中运行项目,或者使用Maven命令进行打包和运行。例如,使用"Maven clean package"命令进行打包,然后使用"java -jar"命令运行生成的jar文件。 这些是搭建Spring BootSpring Cloud项目的基本步骤。当然,具体的细节和配置可能会根据您的项目需求有所不同。希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值