Consul实现服务注册中心—SpringCloud(H版)微服务学习教程(17)

Java技术 潘老师 7个月前 (03-25) 455 ℃ (0) 扫码查看

这一小节我们通过Consul来实现服务注册中心,具体的流程可参考zookeeper的,我们仍然不管持久层等代码,直接配置环境即可。

一、实现服务提供者

第1步:建Module

mscloud下新建cloud-provider-payment8006模块

第2步:改pom

pom中主要将zk的依赖换为consul的依赖,具体如下:

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

    <artifactId>cloud-provider-payment8006</artifactId>

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

        <!--  引入自定义的api通用包,可以使用支付Payment等Entity      -->
        <dependency>
            <groupId>com.panziye.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>

    </dependencies>

</project>

第3步:写yml

resoures下的application.yml配置如下:

server:
  port: 8006

spring:
  application:
    name: consul-payment-service
  cloud:
    #consul服务注册中心配置
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

第4步:主启动类

新建PaymentMain8006 主启动类:

package com.panziye.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

第5步:业务类

我们这里就只新建个PaymentControlelr类:

package com.panziye.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class PaymentControlelr {
    @Value("${server.port}")
    private String serverPort;
    @GetMapping(value = "/payment/consul")
    public String consul(){
        return "payment consul server port:"+serverPort+",随机数:"+ Math.random();
    }
}

第6步:测试

我们启动consul和8006服务,浏览器访问http://localhost:8006/payment/consulhttp://localhost:8500/,,发现服务注册成功:
Consul实现服务注册中心—SpringCloud(H版)微服务学习教程(17)

Consul实现服务注册中心—SpringCloud(H版)微服务学习教程(17)

二、实现服务消费者

服务消费者的实现与zookeeper中的类似,我们步骤如下:

第1步:建Module

mscloud下新建cloud-consumer-consul-order80模块

第2步:改pom

pom中主要将zk的依赖换为consul的依赖,具体如下:

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

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

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

        <!--  引入自定义的api通用包,可以使用支付Payment等Entity      -->
        <dependency>
            <groupId>com.panziye.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>

    </dependencies>

</project>

第3步:写yml

resoures下的application.yml配置如下:

server:
  port: 80

spring:
  application:
    name: cloud-order-consul-service
  cloud:
    #consul服务注册中心配置
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

第4步:主启动类

新建OrderConsulMain80 主启动类:

package com.panziye.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

第5步:业务类

1)在config在新建ApplicationContextCofig

package com.panziye.springcloud.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;

@Configuration
public class ApplicationContextCofig {
    /**
     * 配置RestTemplate
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

2)controller中新建OrderController

package com.panziye.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {
    // 注册中心中服务地址-consul中service名称
    public static final String PAYMENT_URL = "http://consul-payment-service";

    @Resource
    private RestTemplate restTemplate;

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

}

第6步:测试

再启动OrderConsulMain80,访问浏览器测试:
1)http://localhost/consumer/payment/consul
Consul实现服务注册中心—SpringCloud(H版)微服务学习教程(17)
2)http://localhost:8500/
Consul实现服务注册中心—SpringCloud(H版)微服务学习教程(17)


版权声明:本站所有文章,如无特殊说明,均为本站原创。全部下载资源版权归原作者所有。任何个人或组织,若未征得本站同意,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。若需转载请注明文章来源。
本文链接:Consul实现服务注册中心—SpringCloud(H版)微服务学习教程(17)
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】

您也可以 微信登录 来发表评论!