搭建一个基础的Spring Cloud项目

项目中用到两台注册中心(RAServer-A、RAServer-B)、一台服务提供者(Service-A)、一台消费者(LBServer-A)。其中Service-A既做服务提供者也作为消费者使用,同样地,LBServer-A既可以作为消费者,也作为服务提供者使用。实现了一个简单的分布式微服务,主要功能有服务的注册与发现,负载均衡,服务熔断降级。所有服务通过打war包方式使用tomcat部署。项目管理采用maven。

《搭建一个基础的Spring Cloud项目》

由于springcloud环境是一个个springboot项目组成,所以直接参照springboot jar包转war包的方式修改。

1.pom.xml文件

2.修改启动类

3.由于要区分不同环境,所以需要把配置文件修改成多个环境的配置文件

【注册中心】

由于springcloud使用的内部tomcat都是缺省应用名,所以修改成war包之后,如果不缺省名称,都需要把application.properties文件中有关的url地址把应用名加上。

#应用名

spring.application.name=eureka-server

#应用端口

server.port=8150

#主机名

eureka.instance.hostname=peer1

#注册中心服务地址,此处需要把应用名加上

eureka.client.serviceUrl.defaultZone=http://peer2:8160/eureka-server/eureka/

 

#日志等级

logging.level.root=info

#清理间隔

eureka.server.evictionIntervalTimerInMs=1000

《搭建一个基础的Spring Cloud项目》

【服务提供者】-只提供api数据服务

项目中a,b文件夹表示两台服务器,内部的配置是不同项目,application.properties是通用配置,dev,test,prd区分不同环境。打包使用哪个服务器下哪个环境的配置文件是由pom.xml决定的。

《搭建一个基础的Spring Cloud项目》《搭建一个基础的Spring Cloud项目》

【消费者】-负责页面的渲染和服务的调用

Demo中消费者通过feign进行负载均衡和服务熔断降级。 Feign自带Hystrix。

《搭建一个基础的Spring Cloud项目》

《搭建一个基础的Spring Cloud项目》

pom.xml

<?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.zbjk.lcs.oap</groupId>
    <artifactId>oap</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <name>oap</name>
    <description>this is scheduler system of llt</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>

    <!-- 缁熶竴jar鍖呯増鏈?-->
    <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>

    <dependencies>
        <!-- spring boot鏁村悎web搴旂敤缁勪欢 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 绉婚櫎宓屽叆寮弔omcat鎻掍欢 涓嶇Щ闄や細鍚姩鎶ラ敊 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <!-- spring boot鏁村悎eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- spring boot鏁村悎feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- spring boot鏁村悎redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.3.RELEASE</version>
        </dependency>
        <!-- 鐑儴缃?-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 鍐呯疆鎺ュ彛 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- spring boot鏁村悎mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--mysql jdbc椹卞姩 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- json 瑙f瀽 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
        <!-- jstl 鏍稿績鏍囩搴?-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- servlet 绉婚櫎鍐呯疆tomcat鍚庡氨闇€瑕佹坊鍔犲繀瑕佺殑servletapi鍖?-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- 浣跨敤Apache HttpClient鏇挎崲Feign鍘熺敓httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>8.18.0</version>
        </dependency>
        <!--娣诲姞aop鏀寔 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>

        </dependency>

        <!--娣诲姞BeanUtils鏀寔锛屾柟渚胯繘琛宒to涓巇omain涔嬮棿鐨勪簰鐩歌浆鎹㈡搷浣?->
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <!--娣诲姞鐨勬敮鎸佸璇ユā鍧楁煇绫绘煇鏂规硶鐨勫畾鏃惰皟搴?->
        <dependency>
            <groupId>com.zbjk.llt</groupId>
            <artifactId>llt</artifactId>
            <version>1.0.7</version>
        </dependency>

        <!--鍒嗛〉 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!-- 瀹氬埗鍖朿3p0 -->
        <dependency>
            <groupId>com.zbjk</groupId>
            <artifactId>c3p0</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>mchange-commons-java</artifactId>
            <version>0.2.8</version>
        </dependency>

        <!-- 骞冲彴鏍稿績鍖?-->
        <dependency>
            <groupId>com.zbjk.platform</groupId>
            <artifactId>base-core</artifactId>
            <version>0.0.5</version>
        </dependency>
        
        <dependency>
			<groupId>com.zbjk.platform</groupId>
			<artifactId>base-client</artifactId>
			<version>0.0.5</version>
		</dependency>

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- redisson -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.3.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        
        <!-- spring cloud 娑堟伅璺熻釜 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        
    </dependencies>

    <!-- 鍖哄垎鐜 -->
    <profiles>
        <profile>
            <!-- 寮€鍙戠幆澧?-->
            <id>dev</id>
            <properties>
                <packageEnvironment>dev</packageEnvironment>
            </properties>
            <!-- 榛樿婵€娲籨ev鐜閰嶇疆 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 娴嬭瘯鐜 -->
            <id>test</id>
            <properties>
                <packageEnvironment>test</packageEnvironment>
            </properties>
        </profile>
        <profile>
            <!-- 鐢熶骇鐜 -->
            <id>prd</id>
            <properties>
                <packageEnvironment>prd</packageEnvironment>
            </properties>
        </profile>
    </profiles>
    <!-- 鎵撳寘鏋勫缓璁剧疆 -->
    <build>
        <!-- 鎵撳寘璧勬簮 渚濇搴忔墽琛?-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 鍦⊿pringBoot椤圭洰涓浛鎹xxx@鍐呭 -->
                <!-- 鍦⊿pringMvc椤圭洰涓浛鎹?{xxx}鍐呭 -->
                <filtering>true</filtering>
                <excludes>
                    <exclude>dev/</exclude>
                    <exclude>test/</exclude>
                    <exclude>prd/</exclude>
                    <exclude>application.properties</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources/${packageEnvironment}</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application-${packageEnvironment}.properties</include>
                    <include>logback.xml</include>
                </includes>
            </resource>
        </resources>

        <!-- 閫夋嫨鏋勫缓鐨勬彃浠?-->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
        </plugins>

        <!-- 鏈€缁堝寘鍚?-->
        <finalName>oap</finalName>
    </build>
</project>

启动类

package com.zbjk;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@EnableDiscoveryClient
@EnableCircuitBreaker
@SpringBootApplication
@MapperScan("com.zbjk.lcs.oap.mapper")
@EnableFeignClients
public class OapApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		new SpringApplicationBuilder(OapApplication.class).web(true).run(args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		// 指向原先用main方法执行的OrdApplication启动类
		return builder.sources(OapApplication.class);
	}


}

application.properties

    原文作者:Spring Cloud
    原文地址: https://blog.csdn.net/mango_yss/article/details/77158392
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞