工作流程:
1. 服务提供者注册服务到服务注册中心。
2. 服务消费者到注册中心获取提供者列表。
3. 服务消费者使用提供者提供的服务。
首先新建maven项目,添加dependency 在 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>
在这个简单的例子中,注册中心,服务提供者和服务消费者,在一个项目中,所以这里通过profile 来区分不同的设置。
启动Eureka 注册服务器
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { System.setProperty("spring.profiles.active", "server"); SpringApplication.run(EurekaServerApplication.class, args); } }
对应的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/
访问 http://localhost:1001/ 可以查看 Eureka 注册中心的运行状态。
启动 Service provider:
@SpringBootApplication @EnableDiscoveryClient public class EurekaApplication { public static void main(String[] args) { System.setProperty("spring.profiles.active", "provider"); SpringApplication.run(EurekaApplication.class, args); } }
另外在Service Provider 上提供一个REST的服务:
@RestController public class HelloController { @RequestMapping("/hello") public String index() { System.out.println("index is called"); return "Hello World"; } }
Service provider 对应的application-provider.properties
spring.application.name=hello server.port=1201 eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
这样会注册自己的服务到Eureka 的注册中心。
最后启动服务的消费者:
@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); } }
以及服务消费者提供的一个REST Controller,他会调用Provider 的REST 服务。
@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
server.port=9000 spring.application.name=hello-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
整个project的结构如图:
访问 http://localhost:9000/consumer 会看到 Hello World。
源码可以从这里下载 https://github.com/21ca/eureka-demo。
在这个源码的例子中,启动了两个服务注册中心,构成一个注册中心集群。然后启动两个服务提供者,分别注册到不同的服务注册中心。服务消费者从注册中心拿到提供者的列表,然后分别调用(客户端负载均衡)服务提供者的服务。