SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
需要搭建一个lj_ovls_eureka_server搭建2个 lj_ovls_paper_server ,lj_user_server ,这样才可以看到效果源码:https://gitee.com/qq1350048638/lj_ovls_paper_serverhttps://gitee.com/qq1350048638/lj_user_serverhttps://gite...
需要搭建一个lj_ovls_eureka_server
搭建2个 lj_ovls_paper_server ,lj_user_server ,这样才可以看到效果
源码:
https://gitee.com/qq1350048638/lj_ovls_paper_server
https://gitee.com/qq1350048638/lj_user_server
https://gitee.com/qq1350048638/lj_ovls_eureka_server
公共包 :https://gitee.com/qq1350048638/lj_common
另前端界面
https://gitee.com/qq1350048638/lj_study_web/
eclipse各工程结构图
lj_ovls_eureka_server目录结构
pom.xml
<!-- 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>
EurekaServerBootApplication 启动类
package com.lj.ovls;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer//启用eureka服务器
@SpringBootApplication
public class EurekaServerBootApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(EurekaServerBootApplication.class, args);
}
}
lj_ovls_paper_server工程结构
pom.xml
springcloud-ribbon springcloud-feign 2种方式
<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>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.lj</groupId>
<artifactId>lj_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- springcloud-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<!-- 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>
</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>
PaperServiceBootApplication 启动类
@EnableFeignClients
@EnableDiscoveryClient 注入
package com.lj.ovls;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@ServletComponentScan
@MapperScan(basePackages={"com.lj.ovls.paper.dao"})
public class PaperServiceBootApplication {
public static void main(String[] args) {
SpringApplication.run(PaperServiceBootApplication.class, args);
}
}
使用RestTemplate(ribbon负载)
package com.lj.ovls.paper.config;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced//启用ribbon负载均衡调用服务
public RestTemplate cteateRestTemplate(){
return new RestTemplate();
}
}
注入RestTemplate,调用eureka服务
USERSERVER 是application.properties里面设置的,相当于localhost:8881
@Autowired
private RestTemplate restTemplate;
ResponseResult userResult = restTemplate.getForObject(
"http://USERSERVER/user/token?token="+token, ResponseResult.class);
application.properties
里面设置paper_server 服务器的别名
相当于 localhost:8884 通过eureka服务器 对应的 别名 paperserver 即可访问
server.port=8884
#eureka
spring.application.name=paperserver
eureka.client.serviceUrl.defaultZone=http://localhost:2222/eureka
feign模式
pom.xml 前面已经设置了
定义一个Fegin远程调用接口
package com.lj.ovls.paper.remote;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.lj.ovls.common.entity.ResponseResult;
@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标记 前面已经设置
更多推荐
所有评论(0)