01 基础环境准备
02 一文读懂Eureka
03 Zookeeper注册中心
04 Consule注册中心
05 Ribbon
06 OpenFegin
07 Hystrix全面解析
08 Gateway全面解析
09 Config配置中心
10 Bus消息总线
1 简介
Zookeper能当Dubbo的注册中心,也能当SpringCloud的注册中心。
2 环境准备
3 创建工程zk-provider
(1) pom
<dependencies>
<!--spring cloud zookeeper-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--排除自帶的zookeeper-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加自己的zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.zrs.springcloud</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(2) application.yml
server:
port: 7002
#服务名
spring:
application:
name: zk-provider
cloud:
zookeeper:
connect-string: 192.168.0.39:2181
(3) 主启动类
/**
* @Description: EnableDiscoveryClient,用于向Zk和consul作为注册中心时注册服务
* @Auther: zhurongsheng
* @Date: 2020/3/10 23:36
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperApplication.class);
}
}
(4) controller
@RestController
public class ZkProviderController {
@Value("${server.port}")
private String serverport;
@RequestMapping("/zk")
public String zk(){
return "springcloud with zookeeper: "+serverport;
}
}
(5) 启动主程序
3 创建服务消费者 zk-customer
(1) pom
<dependencies>
<!--spring cloud zookeeper-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--排除自帶的zookeeper-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加自己的zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.zrs.springcloud</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(2) application.yml
server:
port: 8001
#服务名
spring:
application:
name: zk-customer
cloud:
zookeeper:
connect-string: 192.168.0.39:2181
(3) 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class);
}
}
(4) 配置类
@Configuration
public class ApplicatrionConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
(5) Controller
@RestController
@RequestMapping("/zk")
public class ZookeeperController {
public static final String PROVIDER_URL="http://zk-provider";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/customer")
public String customer(){
ResponseEntity<String> forEntity = restTemplate.getForEntity(PROVIDER_URL+"/zk", String.class);
return forEntity.getBody();
}
}
(6) 启动
(7) 测试
GITHUB
#分支zookeeper-environment-release-v1.0
https://github.com/zhurongsheng666/spring-cloud-hoxton.git
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15148.html