菜单

因为zookeeper为报大旨搭建spring cloud环境

2018年12月20日 - jQuery

spring cloud连忙搭建

  **Spring Cloud是一个微服务框架,它遵照spring
boot, Spring Cloud提供的成套的分布式系统解决方案*manbetx2.0手机版,*。 

  首先我们采纳gradle来创立:

manbetx2.0手机版 1

挑选JDK以及勾选Java,然后下一致步

manbetx2.0手机版 2

打包名已经项目名为,下一样步:

manbetx2.0手机版 3

择我们地方的gradle包,一贯下一样步,点击build.gradle并累加大家的因:

group 'com.gaofei'
version '1.0-SNAPSHOT'

//gradle使用的插件
apply plugin: 'java'
//gradle使用spring-boot打包更方便
apply plugin: 'spring-boot'


//jdk的版本号
sourceCompatibility = 1.8


//本项目的
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

//由于本次创建gradle未出现src,由以下代码来解决
task "create-dirs" << {
    sourceSets*.java.srcDirs*.each {
        it.mkdirs()
    }
    sourcScts*.resources.srcDirs*.each{
        it.midirs()
    }
}


//编译构建时的配置
buildscript {
    ext{
        springBootVersion='1.5.10.RELEASE' //springBootVersion是自己定义的变量 里面写的是springboot插件的版本
    }
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        jcenter()
        mavenCentral()
        maven{ url "http://repo.spring.io/snapshot" }
        maven{ url "http://repo.spring.io/milestone" }
        maven{ url "http://repo.spring.io/release" }
        maven{ url 'http://repo.spring.io/plugins-snapshot' }
    }
    dependencies{
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")//指的是springboot的一个插件
    }
}



//统一所有项目的配置 就是对所有的模块进行统一配置 所有以后的模块都不用再配置
allprojects {

    group 'com.gaofei' //分组
    version '1.0-SNAPSHOT' //版本号

    ext{
        springCloudVersion='Edgware.SR2'
    }
    //所有项目都会引用的阿里云里的maven
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        jcenter()
        mavenCentral()
        maven{ url "http://repo.spring.io/snapshot" }
        maven{ url "http://repo.spring.io/milestone" }
        maven{ url "http://repo.spring.io/release" }
        maven{ url 'http://repo.spring.io/plugins-snapshot' }
    }
}

//统一所有子项目的配置
subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'spring-boot'

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web'){
            //使用undertow来代替tomacat
            exclude module:"spring-boot-starter-tomcat"
        }
        //替代tomcat
        compile 'org.springframework.boot:spring-boot-starter-undertow'
        //健康检查
        compile 'org.springframework.boot:spring-boot-starter-actuator'
        dependencies {
            testCompile group: 'junit', name: 'junit', version: '4.12'
        }
    }
    //版本控制插件
    dependencyManagement{
        imports{
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }

}

由此注释可以见见各类代码块的企图,这里大家是故阿里云的库

连片下大家初始建eureka注册中央,通过new->Module再筑gradle项目来创设

manbetx2.0手机版 4

在build中添加eureka-server依赖

//表示自己是一个服务器
    compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'

接下当application.yml中配置

server:
  port: 8000
spring:
  application:
    name: register-center #起个名字
eureka:
  client:
    register-with-eureka: false #启动时不注册表明自己是一个注册中心
    fetch-registry: false

启动类

@SpringBootApplication
@EnableEurekaServer//表明自己是注册中心
public class RegisterCenterProvider {
    public static void main(String[] args) {

        SpringApplication.run(RegisterCenterProvider.class,args);
    }
}

启动:

manbetx2.0手机版 5

即虽象征注册中央启动成功

下边创建服务注册及劳动主题

创设一个gradle module 项目 

manbetx2.0手机版 6

在build.gradle中上加thymeleaf组件,eureka客户端组件的凭

   //thymeleaf组件
    compile  'org.springframework.boot:spring-boot-starter-thymeleaf'
    //eureka客户端组件
    compile 'org.springframework.cloud:spring-cloud-starter-eureka'

在application.yml中配置:

server:
  port: 8001 
spring:
  application:
    name: project-shopping-mall #注册在注册中心的名字,它会进行键值对映射url
  thymeleaf:
    cache: false #关闭缓存
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/ #注册到注册中心
  instance:
    prefer-ip-address: true #用两种方式进行注册,一种是使用主机名注册,一种是使用ip地址进行注册,这里使用ip地址进行注册

启动类:

@SpringBootApplication
@EnableDiscoveryClient //表示eureka客户端
public class ShoppingMallProvider {
    public static void main(String[] args) {
        SpringApplication.run(ShoppingMallProvider.class,args);
    }
}

启动:

manbetx2.0手机版 7

成功!

生几首会说一下spring cloud中使的零部件

于spring
cloud系列受到,有多伎俩实现挂号中央,本例中接纳zookeeper作为注册中心的角色。服务提供者向zookeeper注册,服务消费者从zookeeper中发觉服务提供者的连锁信息,从而远程调用服务提供方。

 

spring
cloud与zookeeper的合并紧要靠spring-cloud-starter-zookeeper-discovery模块,上面是实例:

1、服务提供者zktest

引入相关依赖pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dxz.zktestclient</groupId>
    <artifactId>zktestclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>   <!--配合spring cloud版本 -->
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <!--设置字符编码及java版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!--用于测试的,本例可省略 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--依赖管理,用于管理spring-cloud的依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR3</version>   <!--官网为Angel.SR4版本,但是我使用的时候总是报错 -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--使用该插件打包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

启动类

package com.dxz.zktest;

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

@EnableDiscoveryClient
@SpringBootApplication
public class ZktestApplication {

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

布信息

1、application.properties

server.port=8822
spring.application.name=zktest

2、bootstrap.properties

spring.cloud.zookeeper.connectString=192.168.5.14:2181
spring.cloud.zookeeper.discovery.instanceHost=192.168.5.3
spring.cloud.zookeeper.discovery.instancePort=${server.port}

2、服务消费者zktestClient

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dxz.zktestclient</groupId>
    <artifactId>zktestclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>   <!--配合spring cloud版本 -->
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <!--设置字符编码及java版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!--用于测试的,本例可省略 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--依赖管理,用于管理spring-cloud的依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR3</version>   <!--官网为Angel.SR4版本,但是我使用的时候总是报错 -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--使用该插件打包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

启动类

package com.dxz.zktestclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ZktestClientApplication {

    @Autowired
    private LoadBalancerClient loadBalancer;

    @Autowired
    private DiscoveryClient discovery;

    @RequestMapping("/discovery")
    public Object discovery() {
        System.out.println(loadBalancer.choose("zktest"));
        return "discovery";
    }

    @RequestMapping("/all")
    public Object all() {
        System.out.println(discovery.getServices());
        return "all";
    }

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

布局信息

1、application.properties

server.port=8844
spring.application.name=zktestclient

2、bootstrap.properties

spring.cloud.zookeeper.connectString=192.168.5.14:2181
spring.cloud.zookeeper.discovery.register=false

3、zookeeper环境搭建

3.1、zookeeper安装

http://mirrors.shuosc.org/apache/zookeeper/zookeeper-3.4.11/

见《zk 03之
linux上的zookeeper单机与伪集群安装

4、测试

起步zookeeper后, 先启动zktest项目,日志如下

manbetx2.0手机版 8

翻开zookeeper的节点情状,

在zookeeper所在bin目录下, 执行如下命令

[root@localhost bin]# ./zkCli.sh -server
localhost,登录成功会面体现如下:

manbetx2.0手机版 9

查看zookeeper上的”/services/zktest”目录

[zk: localhost(CONNECTED) 1] ls /services/zktest
[22aa5d6a-f8a9-4053-bdc4-1b926ddc10b4]
[zk: localhost(CONNECTED) 2] 

启动zktestClient项目

 manbetx2.0手机版 10

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图