1. 程式人生 > >SpringCloud Finchley基礎教程:2,註冊中心eureka和配置中心config

SpringCloud Finchley基礎教程:2,註冊中心eureka和配置中心config

1. 註冊中心eureka

1.1 eureka server

1,引入pom依賴

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

2,啟動類上加@EnableEurekaServer註解

@SpringCloudApplication
@EnableEurekaServer
//啟動一個服務註冊中心提供給其他應用進行對話 public class Application { public static void main(String[] args) { //new SpringApplicationBuilder(ServiceRegistryApplication.class).web(true).run(args); SpringApplication.run(Application.class, args); } }

3,加上properties配置

spring:
  application:
    name: scfl-eureka-server
#--------------------------------------------------------------------- server: port: 50000 #--------------------------------------------------------------------- eureka: instance: prefer-ip-address: true status-page-url-path: /actuator/info #eureka註冊中心的url link health-check-url-path: /actuator/health #健康檢查的url
hostname: localhost client: register-with-eureka: true #是否註冊到註冊中心 fetch-registry: false #是否從註冊中心拉取註冊服務列表 service-url: defaultZone: http://localhost:50000/eureka/

1.2 eureka client

1,引入pom依賴

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

2,啟動類上加@EnableDiscoveryClient註解

@SpringCloudApplication已經包含@EnableDiscoveryClient註解

@SpringCloudApplication
public class Application {

    public static void main(String[] args) {
        //new SpringApplicationBuilder(ServiceRegistryApplication.class).web(true).run(args);
        SpringApplication.run(Application.class, args);
    }

}

3,加上properties配置

spring:
  application:
    name: scfl-comm-jdbc

server:
  port: 50001
  servlet:
    context-path: /jdbc

eureka:
  instance:
    prefer-ip-address: true
    status-page-url-path: /actuator/info
    health-check-url-path: /actuator/health
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:50000/eureka/

2. 配置中心config

2.1 config server

1,引入pom依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2,啟動類上加@EnableConfigServer註解

@SpringCloudApplication
@EnableConfigServer
public class Application {

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

}

3,加上properties配置,可以從local,git,svn取配置檔案

git

spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringCloud-Learning/
spring.cloud.config.server.git.searchPaths=Chapter1-1-8/config-repo
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

本地

#Config Server會預設從應用的src/main/resource目錄下檢索配置檔案
spring.profiles.active=native
#指定配置檔案的位置
spring.cloud.config.server.native.searchLocations=file:F:/properties/

2.2 config client

1,引入pom依賴

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

2,不需要單獨的啟動註解

3,加上properties配置

通過config server的service id來去配置

spring.cloud.config.name=${spring.application.name},db,redis,rabbitmq
#spring.cloud.config.profile=${spring.profiles.active}

#spring.cloud.config.uri=http://localhost:7001/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

配置檔案載入順序,先載入配置中心的{name}-{profile}.yml,再載入本專案的application.yml,如果遇到重名的key,會以第一個為準。

3. 配置檔案動態重新整理bus

1,引入pom依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

spring-boot-starter-actuator包含重新整理端點
spring-cloud-starter-bus-amqp可以通過rabbitmq通知所有config client重新獲取配置檔案

2,不需要單獨的啟動註解

3,加上properties配置

#開啟端點以及跨域,預設web只開啟了health和info
management:
  endpoints:
    web:
      exposure: 
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

5,bus流程圖

image

4. spring cloud常用的starter

Name Description
spring-cloud-starter-config config客戶端
spring-cloud-config-server @EnableConfigServer
spring-cloud-starter-bus-amqp /bus/refresh
spring-cloud-starter-bus-kafka /bus/refresh
spring-cloud-starter-netflix-eureka-client eureka-client
spring-cloud-starter-netflix-eureka-server @EnableEurekaServer eureka-server
spring-cloud-starter-netflix-hystrix @EnableCircuitBreaker hystrix
spring-cloud-starter-netflix-hystrix-dashboard @EnableHystrixDashboard /hystrix
spring-cloud-starter-netflix-turbine @EnableTurbine
spring-cloud-starter-netflix-ribbon .ribbon.*
spring-cloud-starter-netflix-zuul zuul
spring-cloud-starter-openfeign @EnableFeignClients @FeignClient(“stores”)
spring-cloud-starter-sleuth sleuth
spring-cloud-starter-zipkin zipkin
pring-cloud-starter-gateway gateway