consul的下载地址:https://www.consul.io/downloads
<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.0modelVersion>
<groupId>com.cloudgroupId>
<artifactId>springcloud_allartifactId>
<version>1.0-SNAPSHOTversion>
<modules>
<module>provider-consul-modulemodule>
<module>consumer-consul-modulemodule>
modules>
<packaging>pompackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.13junit.version>
<log4j.version>1.2.17log4j.version>
<lombok.version>1.16.18lombok.version>
<mysql.version>8.0.19mysql.version>
<druid.version>1.2.4druid.version>
<mybatis.spring.boot.version>2.1.4mybatis.spring.boot.version>
<druid.spring.boot.version>1.2.4druid.spring.boot.version>
<springfox-swagger-ui.version>2.9.2springfox-swagger-ui.version>
<springfox-swagger2.version>2.9.2springfox-swagger2.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.4.3version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2020.0.2version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2021.1version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.spring.boot.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>${druid.spring.boot.version}version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>${springfox-swagger-ui.version}version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>${springfox-swagger2.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
<addResources>trueaddResources>
configuration>
plugin>
plugins>
build>
project>
<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_allartifactId>
<groupId>com.cloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.providergroupId>
<artifactId>provider-consul-moduleartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
<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_allartifactId>
<groupId>com.cloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.consumergroupId>
<artifactId>consumer-consul-moduleartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulProviderApplication.class, args);
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulConsumerApplication.class, args);
}
}
@EnableDiscoveryClient注解的作用:能够让注册中心能够发现,扫描到微服务。
server:
port: 8006
spring:
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
service-name: ${
spring.application.name}
application:
name: provider-consul-module
server:
port: 80
spring:
application:
name: consumer-consul-module
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
service-name: ${
spring.application.name}
@RestController
@Slf4j
public class ProviderConsulController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/provider/test")
public String test() {
return "提供服务模块服务端口号为:" + serverPort + "《--------------》" + UUID.randomUUID().toString();
}
}
@RestController
public class ConsumerConsulController {
private static final String INVOKE_URL = "http://provider-consul-module";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/test")
public String getData() {
return restTemplate.getForObject(INVOKE_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://localhost:8500,你可以看到consul的客户端界面(然后我们启动的两个服务已经成功注册进consul)
接口测试
成功!
我们在微服务中首先需要面对的问题是服务之间如何进行通信呢?在单体架构中,我们只要将服务的接口暴露,然后通过http请求即可,微服务当然也可以这样。
问题是我们请求的url是固定的,单体服务为我们还可以很快的修改;一旦有大量的服务那就需要我们手动维护服务接口,配置负载均衡;服务宕机后,如何及时下线呢?
总而言之注册中心必不可少(至少当前时代是这样)。