SpringCloud
搭建SpringCloud服务中心
在pom.xml引入spring-cloud开发包
<!-- springboot-parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> <relativePath/> </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> <!-- springcloud-eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.4.RELEASE</version> </dependency> </dependencies> <!-- springcloud-parent --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR5</version> <type>pom</type> </dependency> </dependencies> </dependencyManagement>
在application.properties配置eureka
#server server.port=2222 #eureka #spring.application.name=eureka_server eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://localhost:2222/eureka
在主启用类追加@EnabledEurekaServer标记
@EnableEurekaServer//启用eureka服务器 @SpringBootApplication public class EurekaServerBootApplication { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(EurekaServerBootApplication.class, args); } }
将REST服务注册到Cloud
在pom.xml中追加spring-cloud-eureka定义
<!-- springcloud-parent --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR5</version> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <!-- springcloud-eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.4.RELEASE</version> </dependency>
在application.properties添加eureka注册参数
#eureka spring.application.name=ovls_user_server eureka.client.serviceUrl.defaultZone=http://localhost:2222/eureka
在主启动类追加@EabledDiscoveryClient
@EnableDiscoveryClient//启用eureka服务查找和注册 @SpringBootApplication @ServletComponentScan @MapperScan(basePackages={"cn.xdl.ovls.user.dao"}) public class UserServiceBootApplication { public static void main(String[] args) { SpringApplication.run(UserServiceBootApplication.class, args); } }
从Cloud查找调用REST服务
使用RestTemplate(ribbon负载)
在pom.xml中定义ribbon开发包
<!-- springcloud-ribbon --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.3.4.RELEASE</version> </dependency>
定义一个RestTemplate配置,启用ribbon负载
@Configuration public class RestTemplateConfig { @Bean @LoadBalanced//启用ribbon负载均衡调用服务 public RestTemplate cteateRestTemplate(){ return new RestTemplate(); } }
注入RestTemplate,调用eureka服务
@Autowired private RestTemplate restTemplate; ResponseResult userResult = restTemplate.getForObject( "http://USERSERVER/user/token?token="+token, ResponseResult.class);
提示:调用的URL地址,格式为http://服务名/uri
feign模式
在pom.xml中定义ribbon、feign开发包
<!-- springcloud-ribbon --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.3.4.RELEASE</version> </dependency> <!-- springcloud-feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.3.4.RELEASE</version> </dependency>
定义一个Fegin远程调用接口
@FeignClient(name="USERSERVER")//对应哪个服务实例 public interface UserServiceRemote { //调用/user/token服务 @RequestMapping(value="/user/token",method=RequestMethod.GET) public ResponseResult checkToken(String token); }
提示:@FeignClient(name="注册中心服务实例名"),@RequestMapping请求定义跟UserController入口定义相同
注入Feign远程接口对象,使用服务
@Autowired private UserServiceRemote userRemote; //利用Remote对象调用服务 ResponseResult userResult = userRemote.checkToken(token);
在主入口启动类前追加@EnableFeignClients标记
@EnableFeignClients//启用Feign接口调用 @EnableDiscoveryClient @SpringBootApplication @ServletComponentScan @MapperScan(basePackages={"cn.xdl.ovls.paper.dao"}) public class PaperServiceBootApplication { public static void main(String[] args) { SpringApplication.run(PaperServiceBootApplication.class, args); } }