因为Eureka已经闭源了, 所以可以使用zookeeper替代.
注册中心目的: 服务治理, 服务注册, 服务发现与负载均衡. 当然zookeeper也有同样的功能.
zookeeper
- 是分布式协调工具
- 可以实现注册中心
- 没有服务保护机制
安装zookeeper
- 下载二进制包地址: https://archive.apache.org/dist/zookeeper/zookeeper-3.5.6/apache-zookeeper-3.5.6-bin.tar.gz
- 解压安装zookeeper请参考:https://www.jianshu.com/p/c2025bb7bbe0
编写会员服务并将会员服务注册到zookeeper
- 新建member的springboot项目
- pom.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>lxf</groupId>
<artifactId>member</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>member</name>
<description>Demo project for Spring Boot</description>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- eureka客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<!-- 从指定库中获取依赖
注意:要修改maven的setttings.xml 如下:
<mirrorOf>*,!spring-milestones</mirrorOf>
表示maven除了spring-milestones以外全部从settings配置的仓库中获取依赖-->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 项目启动入口
MemberApplication.java
:
package lxf.member;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
//@EnableDiscoveryClient 作用是使用zookeeper或connsul作为注册中心所用
//向注册中心注册服务
@EnableDiscoveryClient
public class MemberApplication {
public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}
-
application.yml
配置文件:
###服务别名--该服务注册到服务中心的名称
spring:
application:
name: zk-member
profiles:
active: member1
-
application-member1.yml
配置文件:
server:
port: 8101
spring:
cloud:
zookeeper:
###当前会员服务注册到zookeeper注册中心
connect-string: 127.0.0.1:2181
-
application-member2.yml
配置文件:
server:
port: 8102
spring:
cloud:
zookeeper:
###当前会员服务注册到zookeeper注册中心
connect-string: 127.0.0.1:2181
- 创建api接口:
package lxf.member.api; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MemberApiController { @Value("${server.port}") private String serverPort; @GetMapping("/getMember") public String getMember() { return "I am member! server port = "+serverPort; } }
- 启动member后, 从zookeeper可视化工具中就可以看到注册的member服务了.
zookeeper可视化工具 zkui
- 安装使用请参考
- 我的下载解压后的目录是
cd /usr/local/zkui-master mvn clean install //进入到生成jar包目录 cd target //启动服务 java -jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar
- 启动zkui后,浏览器访问:http://localhost:9090,默认登录账号密码如下:
username:admin password:manager
- 启动zookeeper和member服务后访问
zkui
, 出现如下两张图效果代表注册成功。
创建order服务
- pom以来与member完全一致
- 项目入口文件OrderApplication.java内容如下:
package lxf.member;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
//@EnableDiscoveryClient 作用是使用zookeeper或connsul作为注册中心所用
//向注册中心注册服务
@EnableDiscoveryClient
public class MemberApplication {
public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}
}
- application.yml配置文件:
server:
port: 8103
###服务别名--该服务注册到服务中心的名称
spring:
application:
name: zk-order
cloud:
zookeeper:
###当前会员服务注册到zookeeper注册中心
connect-string: 127.0.0.1:2181
- api接口
OrderApiController.java
,通过会员服务在zookeeper中注册的服务别名zk-member
访问会员服务接口
package lxf.order.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderApiController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getOrder")
public String getOrder(){
//两种方式调用member服务接口,一种是直接通过member ipd地址调用,另一种是通过注册中心获取member的ip地址然后调用
//String res = restTemplate.getForObject("http://127.0.0.1:8101/getMember",String.class);
//使用别名的方式请求member服务接口
String res = restTemplate.getForObject("http://zk-member/getMember",String.class);
System.out.println("订单服务调用会员服务getMember结果res="+res);
return res;
}
}
- 浏览器访问测试:http://127.0.0.1:8103/getOrder
详细代码地址:https://github.com/liangxifeng833/springcloud/tree/master/zookeeper
通过DiscoveryClient获取服务中心数据\
- 在
OrderApiController.java
中增加:
@Autowired
private DiscoveryClient discoveryClient
/**
* 可以获取服务中心的服务列表等数据
* @return
*/
@RequestMapping("/discoveryClientMember")
public List<ServiceInstance> discoveryClientMember() {
List<ServiceInstance> instance = discoveryClient.getInstances("zk-member");
for (ServiceInstance serviceInstance : instance) {
System.out.println("zk-member url:"+serviceInstance.getUri());
}
return instance;
}
- 访问地址:http://127.0.0.1:8103/discoveryClientMember
image.png
注册中心产品
- Eureka闭源
- Zookeeper
- Consul 的使用方式除了maven依赖和yml配置不同,其他完全一致。
网友评论