1)简介:
微服务中,服务之间是基于http restful通信的。Springcloud提供了两种调用方式:一种是ribbon+restTemplate,另一种是feign;本文叙述是ribbon+restTemplate方式调用。
2)依赖组件:注册中心与服务发现,服务提供者(eureka client注册到注册中心中)
3)创建服务消费者
3.1)添加pom.xml配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
3.2)配置YML
server:
port: 8015
spring:
cloud:
client:
ipAddress: 192.168.3.30
application:
name: service-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://192.168.3.30:8005/eureka/
instance:
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
prefer-ip-address: true
status-page-url-path: /index.html #配置在注册中心界面服务列表的Status列点击跳转到项目用
3.3)配置类
启动类,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
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;
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
UserService,通过ioc容器的restTemplate来消费PANDA-FRAME服务的“/findAll”接口,用程序名替代具体的url地址,ribbon会根据服务名来选择具体的服务实例,然后根据服务实例在请求的时候会用具体的url替换掉服务名
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class UserService {
@Autowired
RestTemplate restTemplate;
public Map findAll() {
return restTemplate.getForObject("http://PANDA-FRAME/user/findAll",Map.class);
}
}
UserController,用于调用UserService 接口
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.panda.ribbon.service.UserService;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(value = "/user/findAll")
public Map findAll() {
return userService.findAll();
}
}
访问结果:
这样,我们只需要知道调用的是什么服务(服务名),而不需要指定具体的地址访问接口,导致服务机切换导致需要修改配置,这些都让注册中心帮我们管理好,我们只需要专注开发自身的服务即可。
github代码:
注册中心:https://github.com/April-D-phil/panda-eureka