springcloud项目搭建(Finchley.RELEASE版)

spring-cloud-demo

spring-cloud-demo demo 源码

每个module里的README.md有详细的使用指南

如果引入的springboo版本低于2.0.3会造成在导入如下依赖时,启动报错(错误大概意思是DataSource循环依赖)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

项目介绍

项目基于springboot2.0.3和springcloud Finchley

搭建注册中心

1. 在父工程的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.zkane</groupId>
    <artifactId>spring-cloud-demo</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
        <project.reporting.outputencoding>UTF-8</project.reporting.outputencoding>
        <java.version>1.8</java.version>
        <swagger.version>2.6.0</swagger.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <modules>
        <module>eureka-server</module>
        <module>producer-server</module>
        <module>consumer-server</module>
        <module>config-server</module>
        <module>config-client</module>
        <module>zuul-server</module>
        <module>auth-server</module>
        <module>hystrix-dashboard</module>
        <module>turbine</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2. 在注册中心服务的pom.xml导入依赖

    <parent>
        <groupId>com.zkane</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0.0</version>
        <relativePath/>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3. 在application.yml文件中配置

server:
  port: 8000
spring:
  application:
    name: eureka-server
eureka:
  client:
    # 表示是否将自己注册到Eureka Server,默认为true。
    register-with-eureka: false
    # 表示是否从Eureka Server获取注册信息,默认为true。
    fetch-registry: false
    # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用,分隔
    service-url:
      defaultZone: http://localhost:${server.port}/eureka/

4. 在启动类上配置注解

package com.zkane;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

搭建服务提供者

1. 在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.zkane</groupId>
	<artifactId>producer-server</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>producer-server</name>
	<description>Spring Cloud Producer Server</description>

    <parent>
        <groupId>com.zkane</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0.0</version>
        <relativePath/>
    </parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<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>
	</dependencies>

</project>

2. 在application.yml配置文件中配置

server:
  port: 8100
spring:
  application:
    name: producer-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

3. 在启动类上添加注解

package com.zkane;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ProducerServerApplication {

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

4. 编写controller代码,提供restful风格的API访问

package com.zkane.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 594781919@qq.com
 * @date: 2018/5/8
 */
@RestController
public class ProducerController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/get")
    public String getPort() {
        return "Producer Server port: " + port;
    }
}

搭建服务消费者

1. 编写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.zkane</groupId>
    <artifactId>consumer-server</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>consumer-server</name>
    <description>Spring Cloud Consumer Server</description>

    <parent>
        <groupId>com.zkane</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0.0</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2. 编写application.yml配置文件

server:
  port: 8200
spring:
  application:
    name: consumer-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/
feign:
  hystrix:
    enabled: true

3. 在启动类上添加注解

  • @EnableFeignClients注解是使用feign来调用服务提供者的API接口
package com.zkane;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ConsumerServerApplication {

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

4. 使用feign调用服务提供者的API接口

package com.zkane.remote;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author: 594781919@qq.com
 * @date: 2018/5/8
 */
@Component
@FeignClient(value = "producer-server", fallback = ProducerRemoteHystrix.class)
public interface ProducerRemote {
    @GetMapping("/get")
    String getPort();
}

5. 使用hystrix实现服务熔断机制

  • 需要在application.yml中配置feign启用hystrix
feign:
  hystrix:
    enabled: true
  • 在@FeignClient注解上添加fallback属性,指定熔断后调用的本地方法
@FeignClient(value = "producer-server", fallback = ProducerRemoteHystrix.class)
package com.zkane.remote;

import org.springframework.stereotype.Component;

/**
 * @author: 594781919@qq.com
 * @date: 2018/5/8
 */
@Component
public class ProducerRemoteHystrix implements ProducerRemote {
    @Override
    public String getPort() {
        return "Producer Server 的服务调用失败";
    }
}

6. 使用zipkin实现链路跟踪

  • 在控制台输入获取zipkin.jar的地址
    • 因为在springboot2.0以后,官方不再建议自己搭建zipkin的服务端,而是提供现成的jar包,直接运行即可.参考网址:http://www.bubuko.com/infodetail-2596757.html
curl -sSL https://zipkin.io/quickstart.sh | bash -s

在这里插入图片描述

  • 在控制台输入命令,启动zipkin的服务端。在下面截图中,我把zipkin.jar包移动到了我的E:\keluosi\zipkin下面
java -jar zipkin.jar

在这里插入图片描述

  • 在页面输入zipkin服务端的地址查看zipkin的页面,控制台会打印zipkin的端口号,这里默认为9411
http://localhost:9411/zipkin

在这里插入图片描述

  • 在微服务的生产者和消费者配置zipkin
  1. 在pom.xml文件导入依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
  1. 在application.yml文件中配置zipkin的服务端地址
spring:
  zipkin:
    base-url: http://localhost:9411/
  • 打开zipkin页面就可以看到调用链情况了
    在这里插入图片描述
项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值