SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

先建立父工程

..

..一路next

搭建注册中心(需要建立三个工程,端口不一样)

..

..

..

修改入口类

package com.cloud.eurekaserver1111;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer1111Application {

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

 

修改属性文件(一共建立三个Eureka-Server服务,端口分别为1111,2222,3333)

server.port=1111

eureka.instance.hostname=server.one.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

 ..

server.port=2222

eureka.instance.hostname=server.two.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.three.com:3333/eureka

 

..

server.port=3333

eureka.instance.hostname=server.three.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka

 

..

 

 

修改hosts文件(C:\Windows\System32\drivers\etc)

127.0.0.1 server.one.com
127.0.0.1 server.two.com
127.0.0.1 server.three.com

 

修改pom文件,把parent改成父工程

    <parent>
        <groupId>com.cloud</groupId>
        <artifactId>cloud-parent-two</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

 建立服务提供者-8081

 ..

 ..

 ..

修改pom

    <parent>
        <groupId>com.cloud</groupId>
        <artifactId>cloud-parent-two</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

 

修改入口类:

package com.cloud.bookservice8081;

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

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class BookService8081Application {

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

 

修改属性文件

server.port=8081
# 服务名
spring.application.name=BookService

# 注册地址
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
# 注册名
eureka.instance.instance-id=book-service:8081
eureka.instance.prefer-ip-address=true

 

 新建controller

package com.cloud.bookservice8081.controller;

import lombok.extern.slf4j.Slf4j;
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.RestController;

import java.util.List;


@Slf4j
@RestController
public class BookController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/book")
    public String index(){
        List<String> services = discoveryClient.getServices();
        services.forEach(e -> log.info("book-service:8081:" + e));
        List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
        list.forEach(e -> {
            log.info("book-service:8081:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
        });
        return "{\n" +
                "  \"bookName\": \"Apache Kafka实战\",\n" +
                "  \"bookSize\": \"16开\",\n" +
                "  \"pack\": \"平装\",\n" +
                "  \"isbn\": \"9787121337765\",\n" +
                "  \"publisher\": \"电子工业出版社\",\n" +
                "  \"publishTime\": \"2018-05-01\",\n" +
                "  \"service\": \"book-service:8081\"\n" +
                "}";
    }
}

 

建立服务提供者-8082

..

..

修改pom

    <parent>
        <groupId>com.cloud</groupId>
        <artifactId>cloud-parent-two</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

 

修改入口类

package com.cloud.bookservice8082;

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

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class BookService8082Application {

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

 

修改属性文件

server.port=8082
# 服务名
spring.application.name=BookService


# 注册地址
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
# 注册名
eureka.instance.instance-id=book-service:8082
eureka.instance.prefer-ip-address=true

 

增加controller

package com.cloud.bookservice8082.controller;

import lombok.extern.slf4j.Slf4j;
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.RestController;

import java.util.List;


@Slf4j
@RestController
public class BookController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/book")
    public String index(){
        List<String> services = discoveryClient.getServices();
        services.forEach(e -> log.info("book-service:8082:" + e));
        List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
        list.forEach(e -> {
            log.info("book-service:8082:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
        });
        return "{\n" +
                "  \"bookName\": \"Apache Kafka实战\",\n" +
                "  \"bookSize\": \"16开\",\n" +
                "  \"pack\": \"平装\",\n" +
                "  \"isbn\": \"9787121337765\",\n" +
                "  \"publisher\": \"电子工业出版社\",\n" +
                "  \"publishTime\": \"2018-05-01\",\n" +
                "  \"service\": \"book-service:8082\"\n" +
                "}";
    }
}

 

最后建立消费者-8080

..

..

修改pom

    <parent>
        <groupId>com.cloud</groupId>
        <artifactId>cloud-parent-two</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

 

修改入口类(配置了@LoadBalanced注解的Bean

package com.cloud.bookconsumer8080;

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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class BookConsumer8080Application {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

 

属性文件

server.port=8080

eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

 

controller

package com.cloud.bookconsumer8080.controller;

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


@RestController
public class BookController {

    private static final String PREFIX = "http://BOOKSERVICE";  // 微服务名字

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("consumeBook")
    public String index(){
        return restTemplate.getForEntity(PREFIX + "/book",String.class).getBody();
    }
}

 

与上一节不同,这次指向的是服务名

目录结构

 

 下面开始运行

访问 http://server.one.com:1111/ 出现

可以看见当前Eureka连接另外两个Eureka,证明注册中心高可用集群搭建成功。

再看下面的服务,有两个,证明服务已经注册进来了

下面访问消费者 http://localhost:8080/consumeBook 

刷新

证明负载均衡也成功了

 

 GitHub

 

转载于:https://www.cnblogs.com/LUA123/p/9365725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值