Previous article describes how to read a file from a service center configuration, how to configure the center reads the configuration file from a remote git, a lot of the time when a service instance,
are read files from the distribution center, then you can consider the distribution center made a micro-services, which cluster to achieve high availability, the organization chart below:
First, the preparations
continue to use the last article of engineering, architecture can understand springcloud add beg: 3536247259 create a eureka-server works as a service registry.
Eureka is introduced in its initial pom.xml file dependent spring-cloud-starter-netflix- eureka-server, 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>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-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-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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
In the configuration file application.yml, designated service port is 8889, with a basic configuration service registry, as follows:
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Inlet categories:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Second, the transformation config-server
in its starting pom.xml file with EurekaClient dependent spring-cloud-starter-netflix- eureka-client, 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>
Profile application.yml, designated service registered address is http: // localhost: 8889 / eureka /, other configurations above article, the full 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 at the start of class Application program plus @EnableEureka comment.
Third, the reform config-client
to register micro register to the service center, as Eureka client, you need to start pom file with dependent spring-cloud-starter-netflix- eureka-client, 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>
Profile bootstrap.properties, note the bootstrap. Plus service registered 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 read files from the distribution center.
servieId spring.cloud.config.discovery.serviceId configuration center, a service name.
Then found, to write no ip address in reading the configuration file, but the service name, then if you configure the service to deploy multiple, load-balancing, high availability so.
In turn starts eureka-servr, config-server, config-client
visit our Web site: HTTP: // localhost: 8889 /
Visit http: // localhost: 8881 / hi, the browser displays:
foo version 3