首先以下所有的东西都是我个人的一些见解,如果有什么不足,欢迎补充
springclould是一个以微服务著名的框架,springclould是把RESTful接口作为服务提供了出去.
这是我在码云上拉下来的一个springclould的demo.
地址:http://git.oschina.net/weiyonghua/spring-clould-action
这是一个整体的结构,下面分开来一个一个介绍.
1.eureka-server 这个是springclould的注册中心
单个项目结构如下
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.0modelVersion> <groupId>com.examplegroupId> <artifactId>eureka-serverartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>eureka-servername> <description>Demo project for Spring Bootdescription> <parent> <groupId>com.examplegroupId> <artifactId>spring-clould-actionartifactId> <version>0.0.1-SNAPSHOTversion> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.7java.version> properties> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eureka-serverartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
application.properties的配置如下
#服务注册中心端口号 server.port=1111 #是否向服务注册中心注册自己 eureka.client.register-with-eureka=false #是否检索服务 eureka.client.fetch-registry=false #服务注册中心的配置内容,指定服务注册中心的位置 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
application-peer1.properties
#springclould的项目标识 spring.application.name=eureka-server server.port=1111 #设置当前实例的主机名称 eureka.instance.hostname=peer1 #服务注册中心的配置内容,指定服务注册中心的位置 eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
application-peer2.properties
#springclould的项目标识 spring.application.name=eureka-server server.port=1112 #设置当前实例的主机名称 eureka.instance.hostname=peer2 #服务注册中心的配置内容,指定服务注册中心的位置 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
上述两个文件的配置是做一个注册中心的小集群,eureka.instance.hostname这个配置需要添加到你的hosts文件中
127.0.0.1 peer1
127.0.0.1 peer2
EurekaServerApplication 启动类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer//注册一个服务中心 @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
2.computer-service 服务提供方
单个项目结构如下
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.0modelVersion> <groupId>com.examplegroupId> <artifactId>computer-serviceartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>computer-servicename> <description>Demo project for Spring Bootdescription> <parent> <groupId>com.examplegroupId> <artifactId>spring-clould-actionartifactId> <version>0.0.1-SNAPSHOTversion> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.7java.version> properties> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eurekaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
application.properties的文件配置
#springclould的项目标识 spring.application.name=computer-service server.port=2222 #服务注册中心的配置内容,指定服务注册中心的位置 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1111/eureka/
注意客户端和服务端必须注册在同一个注册中心,这里的项目标识就是下面客户端调用的依据
ComputeController这个类中是一些简单的方法实现
package com.example.demo.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * Created by weiyonghua on 2017/6/30. */ @RestController public class ComputeController { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private DiscoveryClient client; @RequestMapping(value = "/add" ,method = RequestMethod.GET) public Integer add(@RequestParam Integer a, @RequestParam Integer b) { ServiceInstance instance = client.getLocalServiceInstance(); Integer r = a + b; logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); return r; } }
ServiceProviderApplication 启动类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient//向服务中心注册 public class ServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(ServiceProviderApplication.class, args); } }
3.feign-consumer 客户端
单个项目结构如下
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.0modelVersion> <groupId>com.examplegroupId> <artifactId>feign-consumerartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>feign-consumername> <description>Demo project for Spring Bootdescription> <parent> <groupId>com.examplegroupId> <artifactId>spring-clould-actionartifactId> <version>0.0.1-SNAPSHOTversion> <relativePath/> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.7java.version> properties> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-feignartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eurekaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
ComputeClient 客户端调用类
package com.example.demo.service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * Created by weiyonghua on 2017/6/30. */ @FeignClient(value = "compute-service" , fallback = ComputeClientHystrix.class) public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add") Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); }
@FeignClient 标签中的value/name都可以表示调用那个项目,对应上服务端的spring.application.name且在同一个注册中心上,并且路径和服务端的路径一致的时候就可以完成调用,fallback表示当这个服务调用失败的时候去调用的类,相当于不会在调用时出现服务直接挂掉的情况,给了一个缓冲.
ComputeClientHystrix 类
package com.example.demo.service; import org.springframework.stereotype.Component; /** * Created by weiyonghua on 2017/6/30. */ @Component public class ComputeClientHystrix implements ComputeClient { @Override public Integer add(Integer a, Integer b) { return -9999; } }
application.properties 配置类
spring.application.name=feign-consumer server.port=4444 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
FeignConsumerApplication 启动类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient//向服务中心注册 @EnableFeignClients//开启客户端的功能 public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
ConsumerController 类 正常调用service方法即可
package com.example.demo.controller; import com.example.demo.service.ComputeClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by weiyonghua on 2017/6/30. */ @RestController public class ConsumerController { @Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); } }
至此,springclould的基本调用就结束了,有兴趣深入了解的可以点击下面的链接
点击打开链接