一、说明
经过不断的努力,一个 spring cloud demo 框架终于成型了,包括了注册中心 eureka server 集群、网关 zuul 集群、配置中心 config server 、服务状态监控组件 spring boot admin server 、断路器 hystrix 、hystrix dashboard、trubine、服务间相互访问 feign、消息总线 bus 、链路追踪 zipkin 和服务提供者等等。
现在基于这个 demo 做更高级的开发,如果团队合作,那么成员只需要针对某一个微服务提供者进行开发即可。
二、服务提供者
1、新建一个 spring boot 工程
2、修改 pom 文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.chinadci</groupId>
<artifactId>microserviceclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microserviceclient</name>
<description>microserviceclient</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring cloud bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- hystrix dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、添加配置文件
application.yml
### 端口号
server:
port: 8763
### 服务名
spring:
application:
name: MicroServiceClient
### rabbitmq
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
### 链路追踪 zipkin
zipkin:
base-url: http://localhost:9411
sender:
type: web
### 消息总线 bus
cloud:
bus:
enabled: true
trace:
enabled: true
### 配置注册中心
eureka:
client:
registryFetchIntervalSeconds: 5
serviceUrl:
### 注册中心集群地址
defaultZone: http://localhost:8761/eureka/,http://localhost:8760/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
### actuator 配置
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
bootstrap.yml
### 配置中心 config server
### 配置中心放在远程 git 仓库
spring:
cloud:
config:
### git 分支
label: master
### 配置文件后缀,如 MicroServiceClient-dev.yml
profile: dev
### 配置中心地址
uri: http://localhost:8181/
discovery:
enabled: true
### 配置中心服务名
service-id: ConfigServer

4、启动类添加注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class Client1Application {
public static void main(String[] args) {
SpringApplication.run(Client1Application.class, args);
}
}
三、相关服务器信息
该服务提供者依赖一下服务,特别是配置中心服务器,必须开着
组件 | 名称 | 地址 |
---|---|---|
注册中心 | EurekaServer | http://xxx.xxx.xxx:0000 |
配置中心 | ConfigServer | http://xxx.xxx.xxx:0000 |
链路追踪服务器 | Zipkin | http://xxx.xxx.xxx:0000 |
网友评论