SpringCloud調用服務示例。
SpringCloud简介:
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
SpringCloud与Springboot内部以及集成好了,可以直接使用SpringBoot.
使用SpringCloud的调用需要新建三个maven项目,分别为eureka-server(服务注册中心) server-member(服务提供方) server-order(服务消费方)
(1)maven项目 eureka-server ,服务注册方(类似于dubbo中使用的 zookeeper)
项目目录结构如下: 需要编写的三个文件(EurekaApp.java application.yml pom.xml)
EurekaApp.java SpringBoot的启动类,主要用于启动项目,读取配置文件信息
记得要添加注解 @EnableEurekaServer ,标明这是个服务注册中心
package springcloud.eureka_server.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * springcloud的注册中心 * @author Administrator * 启动后,在浏览器通过 localhost:8888访问 */ @SpringBootApplication @EnableEurekaServer public class EurekaApp { public static void main(String[] args){ SpringApplication.run(EurekaApp.class, args); } }
application.yml SpringCloud的主配置文件,里面定义配置信息
port 访问指定端口号
defaultZone 指定访问路径为 http://localhost:8888
server: port: 8888 # 指定该Eureka实例的端口 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
pom.xml Springboot与SpringCloud的依赖文件
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springcloud</groupId> <artifactId>eureka-server</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>eureka-server Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 引入spring boot的依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动项目
运行EurekaApp.java ,Run as --- >> Java Application
启动成功后,在浏览器访问: http://localhost:8888
访问结果如下图所示:
(2)maven项目server-member ,服务提供方 (类似dubbo的provider)
项目目录结构如下,需要编写的文件有四个(MemberApp.java Member.controller.java application.yml pom.xml)
MemberController.java 服务提供方的前端控制器类
@RestController 定义返回json数据
@RequestMapping 定义请求访问路径为 getMember
package springcloud.server_member.demo.controller; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 会员服务 * @author Administrator * */ @RestController public class MemberController { @RequestMapping("/getMember") public List<String> getMember(){ List<String> list = new ArrayList<String>(); list.add("zhangsan"); list.add("zhangsan1"); list.add("zhangsan2"); list.add("zhangsan3"); return list; } }
MemberApp.java 项目的SpringBoot启动类
记得使用 @EnableEurekaClient 注解 标明这是一个服务Client,需要注册在eureka上的一个服务
package springcloud.server_member.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * 启动会员服务 * @author Administrator * * 服务的调用: reset feign */ @SpringBootApplication @EnableEurekaClient public class MemberApp { public static void main(String[] args){ SpringApplication.run(MemberApp.class, args); } }
application.yml SpringCloud的配置文件
server.port 定义访问的端口
spring.application.name 定义服务名,在服务消费方通过这个服务名调用服务!(很重要哦)
eureka: client: serviceUrl: defaultZone: http://localhost:8888/eureka/ server: port: 8762 spring: application: name: server-member
pom.xml SpringCloud与SpringBoot的包依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springcloud</groupId> <artifactId>server-member</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>server-member Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.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.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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> <dependency> <groupId>org.objenesis</groupId> <artifactId>objenesis</artifactId> <version>1.2</version> </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>
启动项目 (这个项目启动前需要先启动 eureka-server项目)
运行MemberApp.java ,Run as --- >> Java Application
启动成功后,在浏览器访问: http://localhost:8762/getMember
访问结果如下图所示:
同时再次访问服务注册中心 eureka-server项目,会出现如下结果,Instance下厨多出一行服务
(3)maven项目 server-order 服务消费方(类似dubbo的customer)
项目目录结构如下: 需要编写的文件有五个(OrderController.java OrderService.java OrderApp.java application.yml pom.xml)
OrderService.java 服务消费方的业务类,在这里调用服务提供方的服务
RestTemplate 是框架中定义的rest服务调用
restTemplate.getForObject("http://server-member/getMember", List.class); 这是调用服务名为 server-member的服务中的 getMember请求对应的方法 (这里与上一个maven项目server-member项目中的配置相对应)
package springcloud.server_order.demo.service; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class OrderService { @Autowired private RestTemplate restTemplate; /** * http: 这里是写member-server里面的访问路径。 getMember是访问方法 * @return */ public List<String> getAllUser(){ return restTemplate.getForObject("http://server-member/getMember", List.class); } }
OrderController.java 服务消费者的前端控制类。
使用@Autowired注解 注入业务层对象,调用业务层方法
package springcloud.server_order.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import springcloud.server_order.demo.service.OrderService; @RestController public class OrderController { @Autowired private OrderService orderService; @RequestMapping("/getOrderUser") public List<String> getAllUser(){ System.out.println("订单服务正在测试会议服务"); return orderService.getAllUser(); } }
OrderApp.java 项目的SpringBoot启动类
@EnableEurekaClient 标识这是一个服务client,需要注册到 Eureka注册中心
@LoadBalanced 标识服务的调用,需要用到负载均衡
package springcloud.server_order.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class OrderApp { public static void main(String[] args){ SpringApplication.run(OrderApp.class, args); } /** * 表示支持负载均衡 * @return */ @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
application.yml Springcloud的配置文件
eureka.client.serviceUrl.defaultZone 注册中心的地址
server.port 访问服务的端口
spring.application.name 服务名称
eureka: client: serviceUrl: defaultZone: http://localhost:8888/eureka/ server: port: 8764 spring: application: name: server-order
pom.xml 项目的包依赖(SpringBoot与SpringCloud)
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springcloud</groupId> <artifactId>server-order</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>server-order Maven Webapp</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 /> <!-- 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> <dependency> <groupId>org.objenesis</groupId> <artifactId>objenesis</artifactId> <version>1.2</version> </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>
启动项目(启动这个项目之前需要先启动 eureka-server server-member)
运行OrderApp.java ,Run as --- >> Java Application
启动成功后,在浏览器访问: http://localhost:8764/getOrderUser
访问结果如下图所示: