(Turn) The simplest Spring Cloud tutorial in history | Part 7: Highly Available Distributed Configuration Center (Spring Cloud Config)

For the latest Finchley version, please visit:
https://www.fangzhipeng.com/springcloud/2018/08/07/sc-f7-config.html

Or
http://blog.csdn.net/forezp/article/details/81041045

The previous article described how a service reads files from the configuration center, and how the configuration center reads configuration files from remote git. When there are many service instances, all files are read from the configuration center. At this time, you can consider making the configuration center into A microservice is clustered to achieve high availability. The architecture diagram is as follows:

å¨è¿éæå ¥ å¾çæè¿ °

 

1. Preparation

Continue to use the project of the previous article to create an eureka-server project to be used as a service registry.

The initial introduction of Eureka in its pom.xml file depends on spring-cloud-starter-eureka-server, the code is as follows:

<?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>

	<groupId>com.forezp</groupId>
	<artifactId>eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-server</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.forezp</groupId>
		<artifactId>sc-f-chapter7</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

In the configuration file application.yml, specify the service port as 8889, plus the basic configuration as the service registry, the code is as follows:

server:
  port: 8889
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Entry class:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

2. Transform config-server

In its pom.xml file plus the start-up dependency of EurekaClient on spring-cloud-starter-eureka, the code is as follows:

<dependencies>
	<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-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
</dependencies>

The configuration file application.yml, the specified service registration address is http://localhost:8889/eureka/, the other configuration is the same as the previous article, the complete configuration is as follows:

spring.application.name=config-server
server.port=8888

spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username= your username
spring.cloud.config.server.git.password= your password
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/

Finally, the @EnableEureka annotation needs to be added to the startup class Application of the program.

Three, transform config-client

Micro-register it to the service registry. As an Eureka client, you need a pom file plus the initial dependency on spring-cloud-starter-eureka. The code is as follows:

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

The configuration file bootstrap.properties, pay attention to bootstrap. Plus the service registration address is http://localhost:8889/eureka/

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/

eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8881
  • spring.cloud.config.discovery.enabled is to read the file from the configuration center.
  • spring.cloud.config.discovery.serviceId The servieId of the configuration center, which is the service name.

At this time, it is found that the ip address is no longer written when reading the configuration file, but the service name. At this time, if the configuration service is deployed in multiple copies, load balancing is used to achieve high availability.

Start eureka-servr, config-server, config-client in turn to
access the URL: http://localhost:8889/

Visit http://localhost:8881/hi, the browser displays:

foo version 3

Download the source code of this article:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter7

Four, reference materials

spring_cloud_config

Read more

Summary of the simplest SpringCloud tutorials in history

SpringBoot tutorial summary

Summary of Java Interview Questions Series

 

 

 

Guess you like

Origin blog.csdn.net/u014225733/article/details/100582871