【前面的話】書接上文,本文的某些知識依賴個人上一篇文章:SpringCloud之Eureka,若是沒有看過能夠先移步去看一下。另外在微服務架構中,業務都會被拆分紅一個個獨立的服務,服務與服務的通信是基於http restful的。Spring cloud有兩種服務調用方式,一種是ribbon+restTemplate,另外一種是feign。在這一篇文章首先講解下基於ribbon+rest。java
IClientConfig ribbonClientConfig: DefaultClientConfigImplgit
IRule ribbonRule: ZoneAvoidanceRulegithub
IPing ribbonPing: NoOpPingweb
ServerList ribbonServerList: ConfigurationBasedServerListspring
ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter安全
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer~~~restful
~~~pom
<artifactId>lovin-ribbon-client</artifactId>
<packaging>jar</packaging>
<name>ribbonclient</name>
<version>0.0.1</version>
<description>ribbon的client</description>複製代碼
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>複製代碼
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
~~~
- 這裏爲了安全,我這裏仍是添加**spring-boot-starter-security**
~~~yaml
server:
port: 8805 # 服務端口號
spring:
application:
name: lovinribbonclient # 服務名稱
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
client:
serviceUrl:
defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 註冊到的eureka服務地址
~~~
- 配置**spring-boot-starter-security**,這裏爲了方便我這裏放開全部請求
~~~java
package com.eelve.lovin.cofig;複製代碼
import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;架構
/**app
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
~~~
- 而後向程序的ioc容器中注入一個bean: restTemplate;並經過@LoadBalanced註解代表這個restRemplate開啓負載均衡的功能。
~~~java
package com.eelve.lovin;複製代碼
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;
/**
public static void main(String[] args) {
SpringApplication.run(LovinRibbonClientApplication.class,args);
}複製代碼
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
~~~
- 而後編寫一個**HelloService**
~~~java
package com.eelve.lovin.service;複製代碼
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;
/**
public String getHello() {
//這裏的**lovineurkaclient**是我上一篇文章新建的eureka客戶端的名稱
return restTemplate.getForObject("http://lovineurkaclient/hello",String.class);
}
}
~~~
- 再編寫一個**HelloController**
~~~java
package com.eelve.lovin.controller;複製代碼
import com.eelve.lovin.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;
/**
@Autowired
HelloService helloService;複製代碼
@RequestMapping("hello")
public String hello(){
return helloService.getHello();
}
}
~~~
# 叄、啓動測試
- 依次啓動eureka的服務端和兩個客戶端,以及新建的lovin-ribbon-client
![咱們能夠看到服務已經所有啓動成功](https://user-gold-cdn.xitu.io/2019/8/29/16cdb47b09a516e5?w=1886&h=935&f=png&s=128533)
咱們能夠看到服務已經所有啓動成功
- 而後訪問http://localhost:8805/hello
![咱們能夠看到已經能夠經過ribbon調到咱們創建的eureka客戶端了](https://user-gold-cdn.xitu.io/2019/8/29/16cdb47b34fcd087?w=1335&h=586&f=png&s=23468)
咱們能夠看到已經能夠經過ribbon調到咱們創建的eureka客戶端了
- 再次請求接口觀察返回
![咱們能夠看到咱們調到了經過ribbon負載的另一個接口](https://user-gold-cdn.xitu.io/2019/8/29/16cdb47b81d4250b?w=1332&h=552&f=png&s=23047)
咱們能夠看到咱們調到了經過ribbon負載的另一個接口了,到這裏咱們就已經弄好了一個簡單的ribbon負載。複製代碼
---