Spring Cloud + Consul 搭建微服务项目

(一)搭建Consul服务器

  1. 从官网下载对应版本下载地址,解压之后执行 consul agent -dev
  2. 通过docker安装
# 拉取镜像
docker pull consul:1.12.3

# 启动容器
docker run --name consul -p 8500:8500 --restart=always -d consul:1.12.3 agent -server -bootstrap -ui -node consul_one -client='0.0.0.0'

(二)项目总pom文件

1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>con.coffee</groupId>
    <artifactId>consul-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modelVersion>4.0.0</modelVersion>

    <modules>
        <module>consul-provider</module>
        <module>consul-consumer</module>
    </modules>

    <properties>
        <spring-boot.version>2.6.0</spring-boot.version>
        <spring-cloud.version>2021.0.0</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <compilerArgument>-parameters</compilerArgument>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                            <configuration>
                                <mainClass>${main.class}</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

(三)服务提供者(consul-provider)

1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>consul-demo</artifactId>
        <groupId>con.coffee</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consul-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. application.yml 配置

server:
  port: 8810

spring:
  application:
    name: consul-provider
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: ${spring.application.name}

3. Controller 编写

@RestController
public class ProviderController {

  @GetMapping("/sayHello")
  public String sayHello(@RequestParam("name") String name) {
    return "Hello " + name + ", This is Spring Cloud Consul !";
  }
}

4. 主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProviderApplication {

  public static void main(String[] args) {
    SpringApplication.run(ConsulProviderApplication.class, args);
  }
  
}

(四)服务消费者(consul-consumer)

1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>consul-demo</artifactId>
        <groupId>con.coffee</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consul-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. application.yml 配置

server:
  port: 8820

spring:
  application:
    name: consul-consumer
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: ${spring.application.name}

3. Controller 编写

@RestController
public class ConsumerController {

  @Autowired
  private RestTemplate restTemplate;

  @GetMapping("/sayHello")
  public String sayHello(@RequestParam("name") String name) {
    return restTemplate.getForObject("http://consul-provider/sayHello?name={1}", String.class, name);
  }
}

4. 主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulConsumerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConsulConsumerApplication.class, args);
  }

  @LoadBalanced
  @Bean
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }
}

(五)验证

  1. 分别启动consul-provider、consul-consumer
  2. 访问 http://localhost:8820/sayHello?name=consul,返回 “Hello consul, This is Spring Cloud Consul !” 则项目搭建成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建微服务项目一般需要以下步骤: 1. 创建父项目项目根目录下创建一个父项目,作为所有微服务项目的父项目。在父项目的pom.xml文件中,配置Spring Cloud和其他依赖的版本号,如下所示: ``` <dependencyManagement> <dependencies> <!-- Spring Cloud dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 2. 创建微服务项目 在父项目下创建多个微服务项目,每个子项目都是一个独立的服务模块,可以有自己的数据模型、业务逻辑、数据库等。每个子项目都需要配置Spring Boot和Spring Cloud相关依赖,如下所示: ``` <dependencies> <!-- Spring Boot dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> ``` 3. 配置微服务 每个微服务都需要有自己的配置文件,例如application.yml或application.properties。在配置文件中,需要指定微服务的端口号、注册中心地址、数据源等信息。 ``` server: port: 8080 spring: application: name: user-service datasource: url: jdbc:mysql://localhost:3306/user_db username: root password: root eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 4. 注册微服务 微服务需要注册到注册中心,以便其他微服务可以发现它并调用它的接口。可以使用Eureka或Consul等开源组件作为注册中心。在微服务的配置文件中,需要指定注册中心的地址和端口号。 ``` eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 5. 调用微服务 微服务之间可以通过HTTP或RPC调用接口进行通信。可以使用Feign或Ribbon等Spring Cloud组件来实现微服务之间的调用。 例如,在一个微服务中调用另一个微服务的接口,可以使用以下代码: ``` @FeignClient(name = "user-service") public interface UserServiceClient { @GetMapping("/users/{id}") User getUserById(@PathVariable("id") Long id); } ``` 以上就是使用Spring Cloud搭建微服务项目的基本步骤。当然,在实际项目中还需要考虑一些其他因素,例如安全性、性能、可伸缩性等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值