服务发现的任务是由Eureka客户端完成,而服务的消费任务由Ribbon完成。Ribbon是一个基于HTTP和TCP的客户端负载据衡器,它可以通过客户端中配置ribbonServerList服务端列表去轮询访问以达到负载均衡的作用。当Ribbon与Eureka联合使用时,Ribbon的服务实例清单RibbonServerList会被DiscoveryEnableNIWSServerList重写,扩展成从Eureka注册中心获取服务端列表。这里不再对Ribbon做过多的介绍。后面会详细的介绍和分析
下面会通过一个示例来看看Eurka的服务治理体系下如何实现服务的发现与消费。
一、创建一个独立的maven项目
二、添加相关的maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
三、application.yml配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
四、写启动类。
package org.hope; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication { public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
} @Bean
RestTemplate restTemplate() {
return new RestTemplate();
} }
五、写service来调用服务提供者
package org.hope.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class HelloService {
@Autowired
RestTemplate restTemplate; public String hiService(String name) {
return restTemplate.getForObject("http://localhost:8762/hello?name=" + name, String.class);
//SERVICE-HI是服务提供者的实例名称,这里用这种形式调用服务也是可以的。
return restTemplate.getForObject("http://SERVICE-HI/hello?name=" + name, String.class);
} }
六、写一个controller来调用service
package org.hope.web; import org.hope.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloControler { @Autowired
HelloService helloService; @RequestMapping(value = "/hello")
public String hi(@RequestParam String name){
return helloService.hiService(name);
} }
七、测试一下看是否成功。
1.先启动Eureka注册中心
2.再运行ServiceApplication.java
3.最后运行ribbon,在浏览器输入http://localhost:8764/hello?name=wali
证明调用成功!!!
https://gitee.com/huayicompany/springcloud-learn/tree/master/lesson1
参考:
[1] 博客,http://blog.****.net/forezp/article/details/70148833
[2] 博客,http://www.cnblogs.com/skyblog/p/5133752.html
[3] 《SpringCloud微服务实战》,电子工业出版社,翟永超