版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ytlviv1985/article/details/78750913
收集各种资料搭建的spring cloud后记录搭建过程
一、eureka server搭建步骤
pom加入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
启动类
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
启动application.yml,注意yml的key value中间有空格,不能用tab键,同一层注意缩进
hosts增加127.0.0.1 peer1 peer2
spring:
profiles: peer1
application:
name: Eureka-Server-1
eureka:
instance:
hostname: peer1
#配置主机名
client:
register-with-eureka: true
#配置服务注册中心是否以自己为客户端进行注册(配置false)
fetch-registry: true
#是否取得注册信息(配置false)
service-url:
defaultZone: http://peer2:7002/eureka/
server:
port: 7001
---
spring:
profiles: peer2
application:
name: Eureka-Server-1
eureka:
instance:
hostname: peer2
#配置主机名
client:
register-with-eureka: true
#配置服务注册中心是否以自己为客户端进行注册(配置false)
fetch-registry: true
#是否取得注册信息(配置false)
service-url:
defaultZone: http://peer1:7001/eureka/
server:
port: 7002
打jar包后启动
java -jar xxxx.jar --spring.profiles.active=peer1
java -jar xxxx.jar --spring.profiles.active=peer2
二、eureka client搭建
pom依赖,附带hystrix熔断器
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext run = run(Application.class, args);
}
}
application.proerties
spring.application.name=mq-service
eureka.client.serviceUrl.defaultZone=http://peer1:7001/eureka/,http://peer2:7002/eureka/
permission-service.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
spring.cloud.bootstrap.enabled=true
management.security.enabled=false
config client配置文件 ,需要安装rabbitmq
bootstrap.yml
spring:
application:
name: mq-service
cloud:
config:
profile: dev
label: master
discovery:
# 通过服务发现组建config server
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://peer1:7001/eureka/, http://peer2:7002/eureka/
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
三、zuul
pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
启动类
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
}
}
application.yml
server:
port: 8888
spring:
application:
name: api-gateway
zuul:
routes:
mq-route:
service-id: mq-service
path: /mq-service/**
eureka:
client:
service-url:
defaultZone: http://peer1:7001/eureka/, http://peer2:7002/eureka/
四、config server
pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!--依赖管理中心-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
application.yml
server:
port: 8092
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: git地址
username:
password:
eureka:
client:
service-url:
defaultZone: http://peer1:7001/eureka/, http://peer2:7002/eureka/