Spring Cloud Getting Started tutorial (Four): In a distributed environment automatically discovers configuration services Spring Cloud Start Tutorial (a): Registration Service

The previous chapter, we Hello world application services, available through the configuration server Config Server hello to the information we configure the "hello world" but its own configuration file must be configured in the URL config server. (Http: // localhost: 8888), If the config server move to another on a separate IP, then as a client of the hello world application must modify the URL address their bootstrap.yml in the config server. This is clearly not enough convenient.

Since the config server has been registered to a eureka service center, auto service center can help make hello world application config server it needs to find it? The answer is yes. Our hello world application only needs to provide the configuration in the config server where it takes the name on it, in the previous example, the configuration name of the service is "config-server". That we now put before services and applications with minor modifications, to achieve automatic discovery service program. The figure is the service discovery mechanism Spring Cloud provides. Config-server is one of the Service Provider, Config-client is a Service Consumer, which are registered with the service center Eureka Server.

1. config-server registered to the service center

config-server itself is a Spring Boot application, can refer directly to Spring Cloud Start Tutorial (a): service registration , the config-server registered in the eureka server. Visit http: // localhost: 8761, you can see our config-server has been registered.

2. Modify the hello world application configuration

1). Also, you need to make your application Hello I registered to eureka service center configuration as before, not repeat them.

2) Modify the configuration file, the URL hard-coded into the config-server mechanism, auto-discovery mechanism by name through the service center, modify bootstrap.yml

Copy the code
 1 eureka:
 2   client:
 3     serviceUrl:
 4       defaultZone: http://localhost:8761/eureka/
 5 spring:
 6   application:
 7     name: config-client
 8   cloud:
 9     config:
10       label: master
11       profile: dev
12 #      uri: http://localhost:8888/
13       discovery:
14          enabled: true
15          serviceId: config-server
16 management:
17   security:
18     enabled: false
19 server:
20   port: 8881
Copy the code

We commented the URL configuration hardcoded config-server, and replace it with a service registry of address http: // localhost: 8761 / eureka / configuration services as well as the name of "config-server", at the same time turn on the automatic discovery mechanism discovery. enable = true. we run the hello world application can be found, GIT can still access the contents inside. At this point our hello world application has been fully configured not know the address of the service, we do not know the contents of the configuration, all of which are automatically discovered by a service registry.

3. When a lot of services, from the distribution center will need to read the file, then we can consider to make a micro-distribution center service, and its cluster to achieve high availability, the organization chart below:

Guess you like

Origin www.cnblogs.com/7788IT/p/11324242.html