work process:
1. The service provider registers the service to the service registry.
2. The service consumer goes to the registry to obtain a list of providers.
3. The service consumer uses the service provided by the provider.
First create a new maven project, add dependency in pom.xml
<parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>1.5.2.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> </dependencies>
In this simple example, the registry, the service provider and the service consumer are in one project, so the profiles are used to distinguish different settings.
Start the Eureka registry server
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { System.setProperty("spring.profiles.active", "server"); SpringApplication.run(EurekaServerApplication.class, args); } }
Corresponding application-server.properties
spring.application.name=eurekaserver server.port=1001 eureka.instance.hostname=localhost eureka.client.fetch-registry=false eureka.client.register-with-eureka=false eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
Visit http://localhost:1001/ to view the running status of the Eureka registry.
Start the service provider:
@SpringBootApplication @EnableDiscoveryClient public class EurekaApplication { public static void main(String[] args) { System.setProperty("spring.profiles.active", "provider"); SpringApplication.run(EurekaApplication.class, args); } }
In addition, a REST service is provided on the Service Provider:
@RestController public class HelloController { @RequestMapping("/hello") public String index() { System.out.println("index is called"); return "Hello World"; } }
Application-provider.properties corresponding to Service provider
spring.application.name=hello server.port=1201 eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
This will register your service to the Eureka registry.
The consumer that finally starts the service:
@EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate (); } public static void main(String[] args) { System.setProperty("spring.profiles.active", "consumer"); SpringApplication.run(ConsumerApplication.class, args); } }
And a REST Controller provided by the service consumer, which will call the Provider's REST service.
@RestController public class ConsumerCotroller { @Autowired private RestTemplate template; @RequestMapping("/consumer") public String index() { return template.getForEntity("http://HELLO/hello", String.class).getBody(); } }
application-consumer.properties corresponding to service consumers
server.port=9000 spring.application.name=hello-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
The structure of the entire project is shown in the figure:
Visit http://localhost:9000/consumer and you will see Hello World.
The source code can be downloaded from https://github.com/21ca/eureka-demo .
In this source code example, two service registries are started to form a registry cluster. Then start two service providers and register with different service registries respectively. The service consumer gets the list of providers from the registry, and then calls (client-side load balancing) the services of the service provider respectively.