• 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:WindowsSystem32driversetc)

    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 "{
    " +
                    "  "bookName": "Apache Kafka实战",
    " +
                    "  "bookSize": "16开",
    " +
                    "  "pack": "平装",
    " +
                    "  "isbn": "9787121337765",
    " +
                    "  "publisher": "电子工业出版社",
    " +
                    "  "publishTime": "2018-05-01",
    " +
                    "  "service": "book-service:8081"
    " +
                    "}";
        }
    }

    建立服务提供者-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 "{
    " +
                    "  "bookName": "Apache Kafka实战",
    " +
                    "  "bookSize": "16开",
    " +
                    "  "pack": "平装",
    " +
                    "  "isbn": "9787121337765",
    " +
                    "  "publisher": "电子工业出版社",
    " +
                    "  "publishTime": "2018-05-01",
    " +
                    "  "service": "book-service:8082"
    " +
                    "}";
        }
    }

    最后建立消费者-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

  • 相关阅读:
    支付宝开发平台之第三方授权登录与获取用户信息
    Struts2之访问路径
    Struts2之命名空间与Action的三种创建方式
    vs和vim
    linux下的各个目录(待填)
    linux-CentOS初学terminal命令(3)rm、chmod、mkdir、who、w、id、systemctl、
    gcc和vs在c的一些区别
    利用putty在window下控制linux的terminal、ftp传输
    linux-CentOS初学terminal命令(2)vi、gcc、g++、./、mv、cp、ifconfig
    linux初学terminal命令(1)ls、cd、su、man、pwd、useradd、passwd、cat、Ctrl+C、Ctrl+Z、Ctrl+L
  • 原文地址:https://www.cnblogs.com/LUA123/p/9365725.html
Copyright © 2020-2023  润新知