第一篇讲了服务的注册与发现,微服务项目会把项目的各个业务需求划分成几个模块来单独提供服务,各服务间的调用都是采用Http Restful来实现,但是在SpringClound中服务间的调用有两种方式:一种是ribbon+ restTemplate;另一种是feign;
Ribbon:在SpringClound中是作为一个负载均衡的客户端,控制访问入口,定制访问策略等功能; Feign组件同时也是集成了ribbon的;
一,项目准备;
在第一篇的基础上,我们将服务器问候的配置文件属性注册的端口分别改为8883分之8882;;然后在IDEA把该服务启动两个实例;
把Single instance only去掉然后就能在IDEA中同一个项目启动两个实例;
那样在Eureka中就注册了两个服务;
二,创建带项目;
同样的,创建一个Model的SpringBoot项目(服务器 - 功能区);
POM依赖如下;
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
</dependencies>
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
特性文件配置如下;
#服务端口
server.port=8884
#注册服务中心地址
eureka.client.service-url.defaultZone=http://localhost:8880/eureka/
#注册服务端name
spring.application.name=service-ribbon
#调用连接时间
eureka.client.eureka-server-read-timeout-seconds=6000
#调用连接时间
eureka.client.eureka-server-connect-timeout-seconds=6000
hystrix.metrics.polling-interval-ms=6000
在启动类上贴@EnableDiscoveryClient向服务中心注册,并且国际奥委会注入豆,在豆上贴@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
代码如下:
@EnableDiscoveryClient //通过@EnableDiscoveryClient向服务中心注册
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
/**
* ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
然后我们再来写service和controller
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService() {
return restTemplate.getForObject("http://service-hello/hello",String.class);
}
}
@RestController
public class HelloControler {
@Autowired
HelloService helloService ;
@RequestMapping(value = “/ hello”)
public String hi(){
return helloService .hiService();
}
}
然后我们启动ribbon项目;访问http:// localhost:8884 / hello;分别刷新这个地址你会看到以下结果:
ribbon项目作为负载均衡的消费者,轮询调用了8882/8883这两个端口的服务提供者;
此时的项目结构如下图: