0
点赞
收藏
分享

微信扫一扫

SpringCloud -- consul

晚熟的猫 2022-01-23 阅读 69

SpringCloud 首先要解决的就是服务注册。

服务注册用例图:

上图分析:

三个角色,服务方,业务提供方,业务消费方

服务方:开启注册服务,暴露服务注册地址和端口

业务提供方:按照服务方提供的ip地址、端口进行服务注册(可以是多个提供方,业务功能一样,这是为了防止单体宕机,使用这个集群,多个提供方他们注册的业务名都一样,具体调用是通过轮询算法进行调用,见下面代码注解的 @LoadBalanced)

业务消费者:按照服务方提供的ip地址、端口进行服务注册,通过提供方在业务注册的注册名进行远程调用(具体调用代码实例使用RestTemplate)

代码:

1. 提供方:6001

导入相对应的 pom 依赖:

<?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</artifactId>
        <groupId>com.lidantao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consul-provider-6001</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <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>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
    </dependencies>

</project>

yml 配置文件

server:
  port: 6001
spring:
  application:
    name: consul-provider
  cloud:
    consul:
      # 注册的地址
      host: localhost
      # 注册的端口
      port: 8500
      discovery:
        # 对外暴露的服务名称
        service-name: ${spring.application.name}

启动类

package com.lidantao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Cola
 * @Date 2022年01月23日 09:53:00
 */
@SpringBootApplication
public class ConsulProviderMain6001 {
    public static void main(String[] args) {
        SpringApplication.run(ConsulProviderMain6001.class, args);
    }
}

controller

package com.lidantao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cola
 * @Date 2022年01月23日 09:56:00
 */
@RestController
public class TestController {

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

    @RequestMapping("/provider/getTest")
    public String getTest(){
        return "consul : " + consul_port;
    }


}

2. 提供方:6002

导入相对应的 pom 依赖:

<?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</artifactId>
        <groupId>com.lidantao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consul-provider-6002</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <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>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
    </dependencies>

</project>

yml 配置文件

server:
  port: 6002
spring:
  application:
    name: consul-provider
  cloud:
    consul:
      # 注册的地址
      host: localhost
      # 注册的端口
      port: 8500
      discovery:
        # 对外暴露的服务名称
        service-name: ${spring.application.name}

启动类

package com.lidantao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Cola
 * @Date 2022年01月23日 09:53:00
 */
@SpringBootApplication
public class ConsulProviderMain6002 {
    public static void main(String[] args) {
        SpringApplication.run(ConsulProviderMain6002.class, args);
    }
}

controller

package com.lidantao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cola
 * @Date 2022年01月23日 09:56:00
 */
@RestController
public class TestController {

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

    @RequestMapping("/provider/getTest")
    public String getTest(){
        return "consul : " + consul_port;
    }


}

 3. 消费方:60

导入相对应的 pom 依赖:

<?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</artifactId>
        <groupId>com.lidantao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consul-consumer-60</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <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>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
    </dependencies>

</project>

yml 配置文件

server:
  port: 60
spring:
  application:
    name: consul-consumer
  cloud:
    consul:
      # 注册的地址
      host: localhost
      # 注册的端口
      port: 8500
      discovery:
        # 对外暴露的服务名称
        service-name: ${spring.application.name}

启动类

package com.lidantao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Cola
 * @Date 2022年01月23日 09:53:00
 */
@SpringBootApplication
public class ConsulConsumerMain60 {
    public static void main(String[] args) {
        SpringApplication.run(ConsulConsumerMain60.class, args);
    }
}

controller

package com.lidantao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author Cola
 * @Date 2022年01月23日 09:56:00
 */
@RestController
public class TestController {


    @Autowired
    private RestTemplate restTemplate;

    private String PROVIDER_URL = "http://consul-provider/provider/getTest";

    @RequestMapping("/consumer/getTest")
    public String getTest(){
        return restTemplate.getForObject(PROVIDER_URL, String.class);
    }


}

configuration

package com.lidantao.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author Cola
 * @Date 2022年01月22日 22:25:00
 */
@Configuration
public class MyConfiguration {

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

4. consul 注册页面

5. 服务请求页面

 

 

 

举报

相关推荐

0 条评论