spring-cloud-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
- 在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>
- 在application.yml文件中配置zipkin的服务端地址
spring:
zipkin:
base-url: http://localhost:9411/
- 打开zipkin页面就可以看到调用链情况了