Spring Cloud - ribbon实例详解
1. 创建注册中心 cloud-server
创建spring boot项目cloud-server
(1. pom.xml文件依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
(2. application.yml文件配置
#配置服务端的端口号
server:
port: 7771
#eureka本身即是服务端又是客户端,只做服务需要把科户端关掉
eureka:
client:
register-with-eureka: false
fetch-registry: false
#配置注册中心的注册地址
service-url:
defaultZone: http://localhost:7771/eureka
#应用名称
spring:
application:
name: server
(3. 启动类添加注解
@SpringBootApplication
@EnableEurekaServer
public class CloudServerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudServerApplication.class, args);
}
}
(4. 启动项目,浏览器访问 http://localhost:7771/ 看到如下界面
2. 创建服务提供者 cloud-service-provider1、cloud-service-provider2
创建spring boot项目cloud-service-provider1
(1. pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
(2. application.yml配置
#配置端口号
server:
port: 8771
#引入注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:7771/eureka
#配置应用名称
spring:
application:
name: provider
(3. 启动类配置注解
@SpringBootApplication
@EnableEurekaClient
public class CloudServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(CloudServiceProviderApplication.class, args);
}
}
(4. 创建Controller层
package org.lhj.pro.controller;
import javax.servlet.http.HttpServletRequest;
import org.lhj.pro.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@Autowired
private ProviderService aservice;
@RequestMapping("/helloribbon")
public String helloRibbon(HttpServletRequest request) {
String uri = request.getRequestURI();
int port = request.getLocalPort();
String ip = request.getRemoteAddr();
return aservice.helloRibbon() + ip + ":" + port + uri;
}
}
(5. 创建Service层和实现类
service接口
package org.lhj.pro.service;
//这里的服务名不区分大小写
public interface ProviderService {
String helloRibbon();
}
service实现类
package org.lhj.pro.service;
import org.springframework.stereotype.Service;
@Service
public class ProviderServiceImpl implements ProviderService{
@Override
public String helloRibbon() {
return "ribbon跑通了";
}
}
3. 将创建的项目cloud-service-provider1复制一份,修改项目名称为cloud-service-provider2
并且将application.yml的端口号改为8772,应用名称不用改,和provider1项目的一样即可。
启动注册中心cloud-server和两个服务提供者cloud-service-provider1、cloud-service-provider2,会发现 注册中心多了一个应用,并且有两个服务可用8771和8772
4. 创建服务消费者 cloud-consumer-ribbon
(1. pom.xml配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
(2. 添加application.yml配置
#服务端口
server:
port: 9871
#注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:7771/eureka/
#定义应用名称
spring:
application:
name: consumer-ribbon
(3. 启动类添加注解
package org.lhj.pro;
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;
@SpringBootApplication
@EnableDiscoveryClient
public class CloudRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(CloudRibbonApplication.class, args);
}
}
(4. 创建RestTemplate配置
package org.lhj.pro.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class GetRestTemplate {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
(5. 创建RibbonController
package org.lhj.pro.controller;
import org.lhj.pro.service.RibbonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ribbon")
public class RibbonController {
@Autowired
RibbonServiceImpl helloService;
@RequestMapping("/hello")
public String testHello() {
return helloService.testHello();
}
}
(6. 创建RibbonServiceImpl
package org.lhj.pro.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonServiceImpl {
@Autowired
RestTemplate restTemplate;
public String testHello() {
// 1. provider 为提供服务的应用名称
// 2. helloRibbon 为提供服务应用里的接口方法
return restTemplate.getForObject("http://provider/helloribbon", String.class);
}
}
启动服务消费者cloud-consumer-ribbon,发现注册中心有多了一个应用
请求RibbonController的接口 http://localhost:9871/ribbon/hello 效果如下:
到此为止,spring cloud 之ribbon调用成功!