如果没有安装过zookeeper,请移步
zookeeper的单机安装 - 详细教程:https://blog.csdn.net/Kevinnsm/article/details/116134397
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloud</groupId>
<artifactId>springcloud_all</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>provider-ZKpayment-module</module>
<module>consumer-Zkpyament-module</module>
<module>provider-ZKpayment-module</module>
</modules>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.13</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>8.0.19</mysql.version>
<druid.version>1.2.4</druid.version>
<mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version>
<druid.spring.boot.version>1.2.4</druid.spring.boot.version>
<springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version>
<springfox-swagger2.version>2.9.2</springfox-swagger2.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud 2020.0.2-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${
mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${
druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${
mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${
druid.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${
springfox-swagger-ui.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${
springfox-swagger2.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_all</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.provider</groupId>
<artifactId>provider-ZKpayment-module</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.common</groupId>
<artifactId>common-module</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_all</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.consumer</groupId>
<artifactId>consumer-Zkpyament-module</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.common</groupId>
<artifactId>common-module</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderZKApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderZKApplication.class,args);
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerZKApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerZKApplication.class, args);
}
}
@EnableDiscoveryClient注解的作用:能够让注册中心能够发现,扫描到微服务。
server:
port: 8004
spring:
application:
name: provider-ZKpayment-module
cloud:
zookeeper:
connect-string: 8.140.37.103:2181
server:
port: 80
spring:
application:
name: consumer-ZKpayment-module
cloud:
zookeeper:
connect-string: 8.140.37.103:2181
@RestController
@Slf4j
public class ProviderController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/provider/test")
public String test() {
return "提供服务模块服务端口号为:" + serverPort + "《--------------》" + UUID.randomUUID().toString();
}
}
@RestController
@Slf4j
public class ConsumerController {
private static final String URL = "http://provider-ZKpayment-module";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/test")
public String getData() {
return restTemplate.getForObject(URL + "/provider/test", String.class);
}
}
RestTemplate是spring封装的一个服务端远程HTTP请求工具,支持常见的Rest请求(get、post等)
简单了说就是用来远程调用的,因为你订单模块是一个服务,消费模块又是一个单独的服务,两个服务端的端口号要进行交互,所以就有了RestTemplate
当然要注入RestTemplate(注意是在消费模块配置,因为是消费服务调用提供服务模块)
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate AutoRestTemplate() {
return new RestTemplate();
}
}
启动提供服务模块和消费服务模块(注意一定要开启zookeeper服务,顺便进入zookeeper客户端)
我们在微服务中首先需要面对的问题是服务之间如何进行通信呢?在单体架构中,我们只要将服务的接口暴露,然后通过http请求即可,微服务当然也可以这样。
问题是我们请求的url是固定的,单体服务为我们还可以很快的修改;一旦有大量的服务那就需要我们手动维护服务接口,配置负载均衡;服务宕机后,如何及时下线呢?
总而言之注册中心必不可少(至少当前时代是这样)。