springcloud 服務調用的兩種方式


spring-cloud調用服務有兩種方式,一種是Ribbon+RestTemplate, 另外一種是Feign。
Ribbon是一個基於HTTP和TCP客戶端的負載均衡器,其實feign也使用了ribbon, 只要使用@FeignClient時,ribbon就會自動使用。

一、Ribbon

1.1
新建模塊client-a
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">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.feng</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
&lt;artifactId&gt;client-a&lt;/artifactId&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
        &lt;artifactId&gt;spring-cloud-starter-ribbon&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

</project>

復制代碼

新建bootstrap.yml

復制代碼
server:
  port: 8910

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/

spring:
application:
name: client
-a

復制代碼

ClientApplication, 這里我們需要注冊一個RestTemplate,並且使用@LoadBalanced開啟負載功能

復制代碼
/**
 * @author fengzp
 * @date 17/5/9
 * @email fengzp@gzyitop.com
 * @company 廣州易站通計算機科技有限公司
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
    SpringApplication.run(ClientApplication.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">, args);
}

@Bean
@LoadBalanced
RestTemplate restTemplate(){
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> RestTemplate();
}

}

復制代碼

測試用的controller

復制代碼
/**
 * @author fengzp
 * @date 17/5/9
 * @email fengzp@gzyitop.com
 * @company 廣州易站通計算機科技有限公司
 */
@RestController
public class TestController {
@Autowired
RestTemplate restTemplate;

@RequestMapping(</span><span style="color: #800000;">"</span><span style="color: #800000;">/hi</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String hi(@RequestParam String id){
    </span><span style="color: #0000ff;">return</span> restTemplate.getForObject(<span style="color: #800000;">"</span><span style="color: #800000;">http://service-a/hi?id=</span><span style="color: #800000;">"</span>+id, String.<span style="color: #0000ff;">class</span><span style="color: #000000;">);
}

}

復制代碼

1.2
為了測試負載功能,這里要再新建一個模塊service-b, 和上一篇的service-a的代碼基本相同,只把端口修改了就可以。
把client-a和service-b都啟動成功后,打開eureka中心應該看到:

1.3
打開http://localhost:8910/hi?id=123

可以看到服務已經成功調用。

然后刷新頁面

看到端口已經改變,說明負載功能成功實現

二、feign

2.1
新建模塊client-b
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">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.feng</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
&lt;artifactId&gt;client-b&lt;/artifactId&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
        &lt;artifactId&gt;spring-cloud-starter-feign&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

</project>

復制代碼

bootstrap.yml

復制代碼
server:
  port: 8911

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/

spring:
application:
name: client
-b

復制代碼

ClientApplication, 使用@EnableFeignClients開啟feiginClient功能

復制代碼
/**
 * @author fengzp
 * @date 17/5/9
 * @email fengzp@gzyitop.com
 * @company 廣州易站通計算機科技有限公司
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication {
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
    SpringApplication.run(ClientApplication.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">, args);
}

}

復制代碼

這里新建一個ServiceAFeignClient來調用service-a服務

復制代碼
@Component
@FeignClient(value = "service-a") //這里的name對應調用服務的spring.applicatoin.name
public interface ServiceAFeignClient {
@RequestMapping(value </span>= <span style="color: #800000;">"</span><span style="color: #800000;">/hi</span><span style="color: #800000;">"</span><span style="color: #000000;">)
String hi(@RequestParam(</span><span style="color: #800000;">"</span><span style="color: #800000;">id</span><span style="color: #800000;">"</span><span style="color: #000000;">) String id);

}

復制代碼

Controller

復制代碼
@RestController
public class TestController {
@Autowired
ServiceAFeignClient serviceAFeignClient;

@RequestMapping(</span><span style="color: #800000;">"</span><span style="color: #800000;">/hi</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String hi(@RequestParam String id){
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> serviceAFeignClient.hi(id);
}

}

復制代碼

2.2

運行后的結果應該是和ribbon的相同。

個人感覺使用feign比較舒服,代碼比較簡潔。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM