使用场景
系统中有多个服务,服务之间使用轻量级通信协议例如http restful接口.但是服务之间的调用存在硬编码,无法动态伸缩.这时候就可以使用服务注册与发现组件
准备工作
- 框架以及版本号
框架 | 版本号 |
---|---|
Spring Boot | 1.4.0.RELEASE |
Spring Cloud | Brixton.SR5 |
- 实例准备
实例 | 端口 | 功能 |
---|---|---|
eureka-server | 8761 | 注册发现服务器(单节点) |
user-server | 9090、9091 | 用户管理服务器(多节点,演示均衡负载) |
eureka-client | 8090 | 服务消费者,调用用户管理接口(演示用本身不提供服务) |
-
实例调用关系图
如何使用
使用Eureka组件
spring boot 的版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
spring cloud 的版本号
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
搭建注册中心eureka-server
- pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
- yml
eureka:
client:
# 服务器自身是否注册到eureka
register-with-eureka: false
# 是否同步节点
fetch-registry: false
# eureka页面url
service-url:
defaultZone: http://${eureka.instance.ip-address}:${server.port}/eureka/
instance:
ip-address: localhost
hostname: discovery-server
server:
port: 8761
spring:
application:
name: discovery-server
- 启动类Application
@SpringBootApplication
@EnableEurekaServer
public class ExampleSpringCloudEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleSpringCloudEurekaServerApplication.class, args);
}
}
- 启动应用 打开页面
http://localhost:8761
从页面中看出Instances currently registered with Eureka
当前还没有节点来注册中心注册
搭建用户管理服务器(user-server)
- pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
- yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
hostname: user-server
server:
port: 8090
spring:
application:
name: user-server
- 暴露的接口
方便演示简单定义一个返回值.不用访问数据库
@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserController {
@GetMapping
private String getUser() {
log.info("getUser ...");
return "userId: 1";
}
}
- 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ExampleSpringUserServerApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleSpringUserServerApplication.class, args);
}
}
- 启动多个应用实例
如何启动多个实例,可以先mvn package 打包成jar.然后使用命令
java -jar user-server.jar --server.port=9090
java -jar user-server.jar --server.port=9091
-
启动多个节点
-
查看注册中心
user-server节点已经被注册到注册中心
搭建服务消费者(eureka-client)
- pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
- yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
hostname: eureka-client
server:
port: 8090
spring:
application:
name: eureka-client
- 启动类Application
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ExampleSpringCloudEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleSpringCloudEurekaClientApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- feignClient
name = "user-server"
这个配置很重要,ribbon
会在注册中心找可用的host-name
.而这个user-server
就是用户管理服务的host-name
@FeignClient(name = "user-server")
public interface UserServerFeign {
@RequestMapping(value = "/user", method = RequestMethod.GET)
String getUser();
}
- 暴露的接口(方便测试使用)
@RestController
public class UserFeignController {
@Autowired
private UserServerFeign userServerFeign;
@GetMapping("/feign/user")
private String getUserByFeign() {
return userServerFeign.getUser();
}
}
-
启动项目,查看注册中心
服务消费者(eureka-client)已经被注册到注册中心
- 消费接口,验证是否正确
多次调用 http://172.16.81.1:8090/feign/user
http://172.16.81.1:8090这个地址是从注册中心获取的,可以点击注册中心的eureka-client,会调转到一个url
-
查看结果
- 可以消费到user
- 用户管理服务节点的接口日志很平均.均衡负载可以正常工作
测试通过
示例代码
https://github.com/gbKidCoding/spring-cloud-example-discovery-eureka.git
参考资料
Spring Cloud与Docker微服务架构实战 周立 著