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中的接口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值