Below we create the client that provides the service, and register ourselves with the Service registration center. In this article we mainly introduce the registration and discovery of services, so we may try to provide an interface in the service provider to obtain all the current service information.
First, create a basic spring boot app. Named Eureka-client, in Pom.xml, add the following configuration:
<parent><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-parent</artifactid><version>1.5.4.release</version><relativepath/> <!--lookup parent from repository--></parent><dependencies><dependency><groupid> org.springframework.cloud</groupid><artifactid>spring-cloud-starter-eureka</artifactid></ Dependency><dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter-web</artifactid></dependency></dependencies><dependencymanagement> <dependencies><dependency><groupid>org.springframework.cloud</groupid><artifactid >spring-cloud-dependencies</artifactid><version>dalston.sr1</version><type>pom</ Type><scope>import</scope></dependency></dependencies></dependencymanagement>
Second, the implementation of the/DC request processing interface, through the Discoveryclient object, in the log to print out the relevant content of the service instance.
@RestControllerpublic class DcController {@AutowiredDiscoveryClient discoveryClient;@GetMapping("/dc")public String dc() {String services = "Services: " + discoveryClient.getServices();System.out.println(services);return services;}}
Finally, by adding @enablediscoveryclient annotations to the main class, this annotation activates the discoveryclient implementation in the Eureka so that the output of the service information in the controller can be realized.
@EnableDiscoveryClient@SpringBootApplicationpublic class Application {public static void main(String[] args) {new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);}}
After we have completed the implementation of the service content, we will continue to do some configuration work for Application.properties, as follows:
spring.application.name=eureka-clientserver.port=2001eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
With the Spring.application.name property, we can specify that the name of the microservices will follow the call and only use that name to access the service. The Eureka.client.serviceUrl.defaultZone property corresponds to the configuration content of the service registry, specifying the location of the service registry. In order to test differentiated service providers and service registries on this computer, use the Server.port property to set different ports.
Of course, we can also get the current list of services by directly accessing the/DC interface provided by the Eureka-client service
1
Services: [Eureka-client]
Among these, the eureka-client in square brackets is the list of all services that are obtained in the implementation of Eureka through the Discoveryclient interface defined by spring cloud. Since spring cloud has done a very good job of abstraction in the service discovery layer, for the above program, we can seamlessly switch from Eureka's service governance system to Consul's service governance system Central.
From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects.
Spring Cloud builds a microservices architecture-create a service provider