spring cloud微服務分散式雲架構-config配置自動重新整理

使用者6866800318發表於2019-03-20

本專案採用版本選擇:

springboot的版本是2.1.2.RELEASE

springcloud的版本是Greenwich.SR1

springcloud倉庫使用的是Gitee

1、下載並安裝RabbitMq

下載地址:http:/ /www.rabbitmq.com/download.html

Spring Cloud大型企業分散式微服務雲架構原始碼請加一七九一七四三三八零

2、SpringCloud Config Server端配置

config-server 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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<!--<version>2.1.4.BUILD-SNAPSHOT</version>-->
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.lee.self</groupId>
	<artifactId>config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>config</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</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-netflix-eureka-client</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>${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>

	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</pluginRepository>
	</pluginRepositories>

</project>

複製程式碼

config-server的bootstrap.yml的配置如下:

server:
  port: 9010
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
		  #碼雲的使用者名稱和密碼及地址
          username: **** 
          password: ****
          uri: https://gitee.com/lijiaxi118/blog-repo
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9001/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"

複製程式碼

3、SpringCloud Config Client端配置

config-client 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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.lee.self</groupId>
	<artifactId>user</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>user</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</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.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<!-- 增加swagger ui展示 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.4.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.4.0</version>
		</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>

</project>

複製程式碼

config-client的bootstrap.yml配置:

spring:
  application:
    name: user
  cloud:
    config:
      discovery:
        service-id: config
        enabled: true
      profile: pro

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9001/eureka/

server:
  port: 9005
ribbon:
  eureka:
    enabled: true

複製程式碼

4、測試

啟動註冊中心eureka

啟動config server 訪問localhost:9010/user-dev.yml監測是否啟動成功

啟動config client

在client專案中寫一個測試類如下

測試類如下

@Component
@RefreshScope
@Data
@ConfigurationProperties(prefix = "spring.redis")
public class RedisConfig {
    private String host;
    private String port;
}

@RestController
@RequestMapping("/test")
public class ConfigController {

    @Autowired
    private RedisConfig redisConfig;

    @RequestMapping("/print")
    public String print() {
        return "host:" +"\t" + redisConfig.getHost()+"====Port:"+redisConfig.getPort();
    }
}

複製程式碼

頁面訪問http://localhost:9005/test/print,檢視結果

修改gitee中的user-dev.yml檔案

使用postman的post請求訪問http://localhost:9010/actuator/bus-refresh

繼續瀏覽器訪問http://localhost:9005/test/print檢視結果是否更新

自此config的重新整理基本完成,最後一步在gitee中配置webhooks,每一次push修改之後都會自動的通知到各個客戶端

4、配置倉庫中的webhooks

點選gitee中的管理——》webhooks,填入外網訪問的路徑即可。

相關文章