springboot + springCloud搭建簡單集羣

源碼:
https://github.com/LM917178900/spring-boot-hello
https://github.com/LM917178900/eureka-server
https://github.com/LM917178900/ribbon-consumer

1 搭建client集羣

1.1新建springboot微服務,spring-boot-hello

1.2 pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

1.3 aplication.properties

client即是服務端也是消費端,將client註冊到兩臺註冊中心。

spring.application.name=hello-service
# 註冊到兩臺註冊中心
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka

1.4 啓動類

package com.didispace.springboothello;

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

/**
 * 能夠被發現爲客戶端
 */
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootHelloApplication {

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

}

1.5 controller

package com.didispace.springboothello.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Logger;

/**
 * @Author: leimin
 * @Description: new class
 * @Date: 2020/6/8 9:08
 * @Version: 1.0
 */
@RestController
public class HelloController {

    /**
     * xx
     */
    private final Logger logger = Logger.getLogger(String.valueOf(getClass()));

    /**
     * xx
     */
    @Autowired
    private DiscoveryClient client;

    /**
     * xx
     * @return xx
     */
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){

        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello,host:" + instance.getHost()+", service_id:" +instance.getServiceId());

        return "hello World !";
    }
}

1.6 啓動

不同shell窗口,同時啓動兩個實例

java -jar ./spring-boot-hello-0.0.1.SANPSHOT.jar --server.port=8081
java -jar ./spring-boot-hello-0.0.1.SANPSHOT.jar --server.port=8082

2 搭建eureka集羣

2.1 新建springboot微服務,eureka-server

2.2 pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.3 application.properties

配置兩個application,相互註冊檢索,實現註冊同步和高可用,分別是:
application-peer1.properties

# 自己是註冊中心,當成客戶端
eureka.client.register-with-eureka=true
# 需要去檢索服務
eureka.client.fetch-registry=true

spring.application.name=eureka-server
server.port=1111

eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/

application-peer2.properties

# 自己是註冊中心,被當成客戶端
eureka.client.register-with-eureka=true
# 需要去檢索服務
eureka.client.fetch-registry=true

spring.application.name=eureka-server
server.port=1112

eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/

2.4 啓動類

package com.huahua.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * 能夠被發現爲服務端
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

2.5 啓動

在不同shell窗口,同時啓動兩個註冊中心。

java -jar ./demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar ./demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

3 ribbon實現消費者負載均衡

3.1 新建springboot微服務,ribbon-consumer

3.2 pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.3 application

spring.application.name=ribbon-consumer
server.port=9000

# 配置一個就行了,負載均衡
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

3.4 啓動類

package com.example.lei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }

}

3.5 controller

package com.example.lei.controller;

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

/**
 * @Author: leimin
 * @Description: new class
 * @Date: 2020/6/8 16:04
 * @Version: 1.0
 */
@RestController
public class ConsumerController {

    /**
     * xx
     */
    @Autowired
    RestTemplate restTemplate;

    /**
     * yyy
     * @return rr
     */
    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    private String helloConsumer(){

        return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
    }
}

3.6 直接啓功

然後就開始使用,通過consumer調用spring-boot-hello中的接口。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章