spring cloud搭建eureka+zuul+hystrix+config
阿新 • • 發佈:2019-02-10
收集各種資料搭建的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/