现在用spring cloud框架搭建微服务已经是比较流行了,毕竟这个是spring提供给我们的一个框架,可以很好配合spring boot使用,很快就可以搭建出一个微服务来,大大的减少了工作量。
这里就谈一下,各个微服务之间要怎么的互相调用。这个就需要一个有服务注册和发现功能的服务,所有的微服务注册到这个服务上面,这样的话,所有的微服务就客户互相的调用了。
服务的注册和发现功能,目前有两种形式:一种是客户端发现(eureka),比较流行。另一种是服务端发现(zookeeper或consul)。
这里主要谈一下使用zookeeper作为服务注册中心与配置中心。
一、服务注册中心
1.安装zookeeper
解压zookeeper:
tar -xvf zookeeper-3.4.10.tar.gz
启动zookeeper:
cd zookeeper-3.4.10
cd conf
cp zoo_sample.cfg zoo.cfg
cd ../bin
sh zkServer.sh start
2.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
3.创建微服务,并以zookeeper作为服务注册中心。
package com.garlic.springcloudzookeeperclientapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* zookeeper作为服务注册中心,应用启动类
* @author llsydn
*/
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudZookeeperClientAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZookeeperClientAppApplication.class, args);
}
}
对应的application.properties
## 配置应用名称
spring.application.name=spring-cloud-zookeeper-client-app
## 配置服务端口
server.port=8080
## 关闭安全控制
management.security.enabled=false
## 配置zookeeper地址
spring.cloud.zookeeper.connect-string=localhost:2181
使用DiscoveryClient获取注册服务列表
package com.garlic.springcloudzookeeperclientapp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* 提供Rest Api,根据实例名称获取注册服务列表
*
* @author llsydn
* @create 2018-5-11 20:47
*/
@RestController
@RequestMapping("/zookeeper")
public class ZookeeperController {
@Value("${spring.application.name}")
private String instanceName;
private final DiscoveryClient discoveryClient;
@Autowired
public ZookeeperController(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@GetMapping
public String hello() {
return "Hello,Zookeeper.";
}
@GetMapping("/services")
public List<String> serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances(instanceName);
List<String> services = new ArrayList<>();
if (list != null && list.size() > 0 ) {
list.forEach(serviceInstance -> {
services.add(serviceInstance.getUri().toString());
});
}
return services;
}
}
注:可以启动不同的实例,此处我启动了端口8080与8081两个实例,然后使用端点可以查询到所注册的服务列表。同样可以通过zookeeper相关命令查询到说注册的服务列表
sh zkCli.sh
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /services
[spring-cloud-zookeeper-client-app]
[zk: localhost:2181(CONNECTED) 3] ls /services/spring-cloud-zookeeper-client-app
[be61af3d-ffc2-4ffc-932c-26bc0f94971c, bcf21ece-e9e1-4a91-b985-8828688370b8]
[zk: localhost:2181(CONNECTED) 4]
二、配置中心
1.使用zkCli创建配置信息
[zk: localhost:2181(CONNECTED) 27] create /config ""
Created /config
[zk: localhost:2181(CONNECTED) 28] create /config ""
Created /config/garlic
[zk: localhost:2181(CONNECTED) 29] create /config/garlic/name "default"
Created /config/garlic/name
[zk: localhost:2181(CONNECTED) 30] set /config/garlic-dev/name "dev"
Node does not exist: /config/garlic-dev/name
[zk: localhost:2181(CONNECTED) 31] create /config/garlic-dev/name "dev"
Created /config/garlic-dev/name
[zk: localhost:2181(CONNECTED) 32] create /config/garlic-test/name "test"
Created /config/garlic-test/name
[zk: localhost:2181(CONNECTED) 33] create /config/garlic-prod/name "prod"
2.使用controller来动态获取zookeeper配置中心的数据
2.1maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
</dependency>
2.2bootstrap.properties
## 启用zookeeper作为配置中心
spring.cloud.zookeeper.config.enabled = true
## 配置根路径
spring.cloud.zookeeper.config.root = config
## 配置默认上下文
spring.cloud.zookeeper.config.defaultContext = garlic
## 配置profile分隔符
spring.cloud.zookeeper.config.profileSeparator = -
spring.cloud.zookeeper.config.root对应zkCli创建的config目录,defaultContext对应创建的garlic或garlic-* 目录,根据profile来确定获取dev还是test或者prod配置
2.3application.properties
## 配置应用名称
spring.application.name=spring-cloud-zookeeper-config-app
## 配置服务端口
server.port=10000
## 关闭安全控制
management.security.enabled=false
spring.profiles.active=dev
2.4使用controller来动态获取zookeeper配置中心的数据
package com.garlic.springcloudzookeeperconfigapp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 提供Rest Api,获取配置在zookeeper中的配置信息
*
* @author llsydn
* @create 2018-5-11 20:50
*/
@RestController
@RequestMapping("/zookeeper")
@RefreshScope // 必须添加,否则不会自动刷新name的值
public class ZookeeperController {
@Autowired
private Environment environment;
@Value("${name}")
private String name;
@GetMapping
public String hello() {
return "Hello, " + name;
}
@GetMapping("/env")
public String test() {
String name = environment.getProperty("name");
System.out.println(name);
return "Hello," + name;
}
}
启动配置实例之后,可以通过zkCli修改garlic下name的值,然后通过访问端点来查看值是否变化。至此,使用zookeeper作为服务注册中心与配置中心就完成了,我们可以通过使用zookeeper作为配置中心,然后使用zuul作为API网关,配置动态路由,为服务提供者,配置数据库连接相关信息。