springCould中的consul-从小白开始【4】

发布时间:2023年12月24日

目录

1.consul介绍???????

2.安装???????

3.创建8006模块???????

?4.创建80模块??????


1.consul介绍 ??????

Consul 是一种用于服务发现配置分布式一致性的开源软件。它由HashiCorp开发和维护,可用于帮助构建和管理现代化的分布式系统。Consul提供了服务注册与发现、健康检查、分布式键值存储、多数据中心支持等功能,可帮助开发人员和系统管理员更好地管理和监控分布式应用程序和微服务架构。

2.安装???????

官网:https://developer.hashicorp.com/consul/install?product_intent=consul#Windows

小张下载的Windows版的~?

  • 1.窗口下直接cmd
  • 2.使用consul --version 查看版本号
  • 3.使用 consul agent -dev 运行
  • 4.浏览器访问 localhost:8500

  • 访问地址,说明成功

3.创建8006模块???????

1.建模块

在父工程下创建工程

注意:jdk版本号,maven版本

2.导pom

1.springboot依赖

2.mybatis依赖

3.mysql依赖

4.通用配置

5.consul依赖

    <dependencies>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--引入自己的api通用包-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--springboot整合consul客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
    </dependencies>

3.写yml

1.服务端口

2.服务名称

3.datasource配置

4.consul配置

5.mybatis配置

server:
  port: 8006
spring:
  application:
    name: consul-provider-payment
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud
    username: root
    password: 123456
  #consul注册信息
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}


mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.xz.springcloud.entity

4.主启动类

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

5.编写业务

1.引入自己的端口号

2.注入service

3.编写业务

@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private PaymentService paymentService;

    @RequestMapping("/payment/consul")
    public String paymentZk() {
        return "spring cloud with consul:" + serverPort + "\t\t" + UUID.randomUUID().toString();
    }

    @GetMapping("/payment/getById/{id}")
    public CommonResult getById(@PathVariable("id") Integer id) {
        Payment result = paymentService.getById(id);
        if (result != null) {
            return new CommonResult(200, "查询成功,serverPort:" + serverPort, result);
        } else {
            return new CommonResult<Payment>(404,"查询无果,serverPort:"+serverPort,null);
        }
    }
}

6.测试

1.访问浏览器地址,查询结果

2.访问localhost:8500

访问成功~?

创建节点成功~?

?4.创建80模块??????

1.建模块

在父工程下创建模块

注意jdk和maven版本

2.导pom

1.springboot依赖

2.通用配置依赖

3.consul依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
       <!--consul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

    </dependencies>

3.写yml

1.服务端口

2.服务名称

3.consul配置信息

server:
  port: 80

spring:
  application:
    name: consul-payment-order

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

4主启动类

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

5.配置类?

后面调用URL,使用的是服务名,而不是具体的地址,所以加上@LoadBalance表负载均衡

@Configuration
public class ApplicationConfig {

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

6.业务类

1.要访问的url,使用服务名,做负载均衡

2.注入restTemplate

3.调用restTemplate

@RestController
public class OrderController {
    private static final String INVOKE_URL= "http://consul-provider-payment";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/payment/getById/{id}")
    public CommonResult getById(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(INVOKE_URL + "/payment/getById/" + id, CommonResult.class);
    }

    @GetMapping("/payment/consul")
    public String discoveryConsul(){
        return restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
    }
}

7.测试

1.使用80,访问路径

2.访问local:8500

服务80和服务 8006都入住到consul~

?

查询成功~?

?

?

文章来源:https://blog.csdn.net/dfdg345/article/details/135064829
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。