(Thirty-five) spring cloud micro Services Architecture b2b2c e-commerce - center services and high-availability configuration

Please add source e-commerce platform Penguin beg: 1038774626. Introduction of two former clients are directly call server-side configuration center to obtain profile information. So that there is a problem of the coupling is too high, the client and server, if the server end to do a cluster, the client can only be routed through the original way, to change the IP address of the server end of time, the client also needs to modify the configuration, does not comply with the concept of springcloud service governance. springcloud provides such a solution, we only need to register the server side as a service to the eureka, client end to eureka go get server-side configuration center services either.

server-side transformation

1, adding a dependency

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

We need to introduce multi-spring-cloud-starter-eureka package to add support for the eureka.

2, the configuration file

server:
server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git仓库的地址
          search-paths: config-repo                             # git仓库地址下的相对地址,可以配置多个,用,分割。
          username: username                                        # git仓库的账号
          password: password                                    # git仓库的密码
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/   ## 注册中心eurka地址

Add config eureka registry

3, start class

Start class to add support for activation @EnableDiscoveryClient configuration center

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}

Such server-side transformation is complete. First start eureka registry, start the server side, access in the browser: HTTP: // localhost: 8000 / , you will see server-side has been registered to the registry.

For server-side testing services in accordance with Part I of the normal testing procedures.

The client transformation

1, adding a dependency

<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-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

We need to introduce multi-spring-cloud-starter-eureka package to add support for the eureka.

2, the configuration file

spring.application.name=spring-cloud-config-client
server.port=8002
 
spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server
 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

Spring.cloud.config.uri mainly removed directly to the server side address configuration, increasing the final three configurations:

spring.cloud.config.discovery.enabled: open Config service discovery support

spring.cloud.config.discovery.serviceId: Specifies the server-side name, i.e. the value of the server-side spring.application.name

eureka.client.serviceUrl.defaultZone: to the address configuration center

These three configuration files are required to put bootstrap.properties configuration

3, start class

Start class to add support for activation @EnableDiscoveryClient configuration center

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}
}

Start client to access in the browser: HTTP: // localhost: 8000 / will see server-side and client-side have been registered to the registry.

High Availability

In order to simulate the production cluster environment, we change server-side port 8003, and then start to do load a server-side services, providing server-side support for high availability.

We first tested alone server, respectively access: HTTP: // localhost: 8001 / Neo-config / dev, HTTP: // localhost: 8003 / Neo-config / dev return information:

{
    "name": "neo-config", 
    "profiles": [
        "dev"
    ], 
    "label": null, 
    "version": null, 
    "state": null, 
    "propertySources": [
        {
            "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", 
            "source": {
                "neo.hello": "hello im dev"
            }
        }
    ]
}

DESCRIPTION two ends are properly read the server configuration information.

Visit again: HTTP: // localhost: 8002 / the Hello, return: hello im dev update. Description Client has read the contents of the server side, we randomly stopped a server-side service, once again visit http: // localhost: 8002 / hello, returns: hello im dev update, explain achieve the purpose of high availability.

Guess you like

Origin blog.csdn.net/wiyzq/article/details/90764313