Spring cloud eureka 简单例子

来源:互联网 发布:2017双11淘宝交易额 编辑:程序博客网 时间:2024/06/05 14:40

工作流程:

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@EnableEurekaServerpublic 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=eurekaserverserver.port=1001eureka.instance.hostname=localhosteureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

访问 http://localhost:1001/ 可以查看 Eureka 注册中心的运行状态。

 

 

启动 Service provider:

@SpringBootApplication@EnableDiscoveryClientpublic class EurekaApplication {public static void main(String[] args) {System.setProperty("spring.profiles.active", "provider");SpringApplication.run(EurekaApplication.class, args);}}

 另外在Service Provider 上提供一个REST的服务:

@RestControllerpublic class HelloController {@RequestMapping("/hello")public String index() {System.out.println("index is called");return "Hello World";}}

 Service provider 对应的application-provider.properties

spring.application.name=helloserver.port=1201eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

这样会注册自己的服务到Eureka 的注册中心。 

 

最后启动服务的消费者:

@EnableDiscoveryClient@SpringBootApplicationpublic class ConsumerApplication {@Bean@LoadBalancedpublic 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 服务。

@RestControllerpublic class ConsumerCotroller {@Autowiredprivate RestTemplate template;@RequestMapping("/consumer")public String index() {return template.getForEntity("http://HELLO/hello", String.class).getBody();}}

 服务消费者对应的 application-consumer.properties

server.port=9000spring.application.name=hello-consumereureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

 

整个project的结构如图:


 

访问 http://localhost:9000/consumer 会看到 Hello World。

 

源码可以从这里下载 https://github.com/21ca/eureka-demo。

 

在这个源码的例子中,启动了两个服务注册中心,构成一个注册中心集群。然后启动两个服务提供者,分别注册到不同的服务注册中心。服务消费者从注册中心拿到提供者的列表,然后分别调用(客户端负载均衡)服务提供者的服务。

 

原创粉丝点击