Spring Cloud与Consul搭建微服务

2019-06-28  本文已影响0人  太2真人R

概述

概念

Spring Cloud

Consul

实现

应用划分

实现步骤

1、部署好Consul并启动

2、新建Maven Web服务端工程sc_server

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sc.server</groupId>
    <artifactId>sc_server</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>sc_server Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <!-- SpringBoot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot系统健康情况监控工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

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

</project>
server:
  port: 8081

spring:
  application:
    name: sc-server
  cloud:
    consul:
      host: 127.0.0.1
      port: 8500
      enabled:  true
      discovery:
        enabled: true
        serviceName: sc-server

spring.cloud.consul.host需修改为实际的Consul服务地址。
spring.cloud.consul.discovery.serviceName是服务名,在客户端的调用中会用到。

package com.sc.server;

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

@EnableDiscoveryClient
@SpringBootApplication
public class ScServerApp {

    public static void main(String[] args) {
        try {
            SpringApplication.run(ScServerApp.class, args);
            System.out.println("服务端启动成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

PS:注意要加上@EnableDiscoveryClient注解。

package com.sc.server;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoService {

    @RequestMapping(value = "/hello")
    public String hello(@RequestParam("name") String name) {
        System.out.println("服务被调用了");
        return "Hello " + name;
    }

}

至此,服务端工程搭建完成。

3、新建Maven Web客户端工程sc_client

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sc.client</groupId>
    <artifactId>sc_client</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>sc_client Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <!-- SpringBoot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot系统健康情况监控工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

        <!-- SpringCloud Feign 声明式服务调用框架 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>

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

</project>

其中集成了OpenFeign,即采用了微服务架构下服务之间声明式的调用方案。

server:
  port: 8082

spring:
  application:
    name: sc-client
  cloud:
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        register: false

spring.cloud.consul.host需改为实际的Consul服务所在地址。
spring.cloud.consul.discovery.register设置为false,即该服务是客户端,不需要向Consul服务发现中心注册服务。

package com.sc.client;

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

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ScClientApp {

    public static void main(String[] args) {
        try {
            SpringApplication.run(ScClientApp.class, args);
            System.out.println("客户端启动成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

PS:需注意加上@EnableFeignClients注解。

package com.sc.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * FeignClient的value取自服务端的配置spring.cloud.consul.discovery.serviceName
 */
@FeignClient(value = "sc-server")
@RequestMapping("/demo")
public interface DemoServiceClient {

    @RequestMapping(value = "/hello")
    String hello(@RequestParam("name") String name);

}

@FeignClient注解完成对服务提供方的接口绑定,
其中sc-server是服务注册名,即sc_server工程中spring.cloud.consul.discovery.serviceName的配置。

package com.sc.client;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/client")
public class ClientController {

    @Resource
    private DemoServiceClient demoService;

    @RequestMapping(value = "/hello")
    @ResponseBody
    public String hello() {
        String name = "客户端";
        String res = demoService.hello(name);
        System.out.println("客户端成功调用了服务端并得到响应结果:" + res);
        return res;
    }

}

至此,客户端工程搭建完成。

上一篇下一篇

猜你喜欢

热点阅读