1 将服务提供者做成集群模式
配置service-hi的端口为8762进行启动,配置service-hi的端口为8763进行启动,
service-hi会在ecureka server上注册两个service-hi的实例,相当于一个小的集群
2 创建服务消费者
2.1 ribbon简介
在微服务框架中,业务被分成一个独立的服务,服务与服务的通讯是基于http restfull
spring cloud有两种调用方式,一种是ribbon+restTemplate,另一种是Feign
ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon
2.2 创建springboot项目,引入jar依赖
<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.beifeng.hadoop</groupId> <artifactId>beifeng-spring-cloud-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>beifeng-spring-cloud-consumer</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 声明为web项目 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 配置eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 配置ribbon 负载均衡客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </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>
2.3 配置eureka server服务器地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #注册服务器地址
server:
port: 8764
spring:
application:
name: cloud-consumer
2.4 在启动类中声明向注册中心注册,在controller中使用restTemplate进行消费相应的服务
spring cloud中discovery service有多中实现(eureka、consul、zookeeper等),
@EnableDiscoveryClient 基于spring-cloud-commons依赖,并且在classpath中实现
@EnableEurekaClient基于spring-cloud-netflix依赖,只能为eureka作用
查看源码 @EnableEurekaClient上有@EnableDiscoveryClient的注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @EnableDiscoveryClient public @interface EnableEurekaClient { }
@SpringBootApplication @EnableDiscoveryClient//向服务中心注册 @RestController public class CloudConsumer { private static Logger logger=LoggerFactory.getLogger(CloudConsumer.class); @Autowired private RestTemplate restTemplate; public static void main(String[] args) { SpringApplication.run(CloudConsumer.class, args); } @Bean @LoadBalanced//使用这个restTemplate开启负载均衡 RestTemplate initRestTemplate(){ return new RestTemplate(); } @RequestMapping("info") public String hiConsumer() { //使用restTemplate调用消费服务提供者的SERVICE-HI的info服务 String response=restTemplate.getForObject("http://SERVICE-HI/info", String.class); logger.info(response); return response; } }
2.5 启动查看结果
多次访问消费者的请求,查看调用服务的情况
2.6 架构分析