Spring Boot进阶 之 水平扩展

本文的示例代码参考MultiServices

目录

  • Nginx

    • Installation

    • Prepare

    • Service1

    • Service2

    • Configuration

  • ZooKeeper

    • Installation

    • Prepare

    • Provider1

    • Provider2

    • Consumer

Nginx

Installation

brew install nginx

sed -i "" 's/8080/80/g' /usr/local/etc/nginx/nginx.conf
sudo brew services start nginx

brew services list
  • 测试
curl localhost



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

Prepare

mkdir Nginx
# cd Nginx
spring init -b 1.5.6.RELEASE -dweb --build gradle Service1

spring init -b 1.5.6.RELEASE -dweb --build gradle Service2

Service1

# cd Service1
vim src/main/java/com/example/Service1/DemoController.java
package com.example.Service1;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @GetMapping("/demo")
    public String demo() {
        return "Service1: " + "demo";
    }
}
echo "server.port=9000" >> src/main/resources/application.properties 
  • 测试
./gradlew bootrun
curl localhost:9000/demo # 返回"Service1: demo"

Service2

# cd Service2
vim src/main/java/com/example/Service2/DemoController.java
package com.example.Service2;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @GetMapping("/demo")
    public String demo() {
        return "Service2: " + "demo";
    }
}
echo "server.port=9001" >> src/main/resources/application.properties 
  • 测试
./gradlew bootrun
curl localhost:9001/demo # 返回"Service2: demo"

Configuration

sudo vim /usr/local/etc/nginx/nginx.conf
# add lines
upstream ms {
    server 127.0.0.1:9000;
    server 127.0.0.1:9001;
}

server {
    listen 80;
    server_name multiservices.test;
    location / {
        proxy_pass http://ms;
    }
}
sudo nginx -s reload
sudo sh -c "echo '127.0.0.1 multiservices.test' >> /etc/hosts"
  • 测试
curl multiservices.test/demo # 返回"Service2: demo"

curl multiservices.test/demo # 返回"Service1: demo"

ZooKeeper

Installation

brew install zookeeper

brew services start zookeeper

brew services list
  • 测试
zkCli
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0] 
WATCHER::

WatchedEvent state:SyncConnected type:None path:null

Prepare

mkdir ZooKeeper
# cd ZooKeeper
spring init -b 1.5.6.RELEASE -dweb --build gradle Provider1

spring init -b 1.5.6.RELEASE -dweb --build gradle Provider2

spring init -b 1.5.6.RELEASE -dweb --build gradle Consumer

Provider1

# cd Provider1
vim build.gradle
# add lines
dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.cloud:spring-cloud-starter-zookeeper-discovery')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Edgware.SR3"
    }
}
sed -i "" '/SpringBootApplication;/a\
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
' src/main/java/com/example/Provider1/DemoApplication.java

sed -i "" '/@SpringBootApplication/a\
@EnableDiscoveryClient
' src/main/java/com/example/Provider1/DemoApplication.java
vim src/main/java/com/example/Provider1/DemoController.java
package com.example.Provider1;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @GetMapping("/demo")
    public String demo() {
        return "Provider1: " + "demo";
    }
}
mv src/main/resources/application.properties src/main/resources/application.yml

tee src/main/resources/application.yml <<-'EOF'
spring:
  application:
    name: Provider
  cloud:
    zookeeper:
      connect-string: localhost:2181
      discovery:
        enabled: true
server:
  port: 9000
EOF
  • 测试
./gradlew bootrun
[localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[localhost:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established to localhost/127.0.0.1:2181, initiating session
[localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x162bc7bf0510009, negotiated timeout = 40000
curl localhost:9000/demo # 返回"Provider1: demo"

Provider2

# cd Provider2
vim build.gradle
# add lines
dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.cloud:spring-cloud-starter-zookeeper-discovery')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Edgware.SR3"
    }
}
sed -i "" '/SpringBootApplication;/a\
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
' src/main/java/com/example/Provider2/DemoApplication.java

sed -i "" '/@SpringBootApplication/a\
@EnableDiscoveryClient
' src/main/java/com/example/Provider2/DemoApplication.java
vim src/main/java/com/example/Provider2/DemoController.java
package com.example.Provider2;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @GetMapping("/demo")
    public String demo() {
        return "Provider2: " + "demo";
    }
}
mv src/main/resources/application.properties src/main/resources/application.yml

tee src/main/resources/application.yml <<-'EOF'
spring:
  application:
    name: Provider
  cloud:
    zookeeper:
      connect-string: localhost:2181
      discovery:
        enabled: true
server:
  port: 9001
EOF
  • 测试
./gradlew bootrun
[localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[localhost:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established to localhost/127.0.0.1:2181, initiating session
[localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x162bc7bf0510009, negotiated timeout = 40000
curl localhost:9001/demo # 返回"Provider2: demo"

Consumer

# cd Consumer
vim build.gradle
# add lines
dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.cloud:spring-cloud-starter-zookeeper-discovery')
    compile('org.springframework.cloud:spring-cloud-starter-openfeign')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Edgware.SR3"
    }
}
sed -i "" '/SpringBootApplication;/a\
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
' src/main/java/com/example/Consumer/DemoApplication.java

sed -i "" '/@SpringBootApplication/a\
@EnableDiscoveryClient
' src/main/java/com/example/Consumer/DemoApplication.java

sed -i "" '/EnableDiscoveryClient;/a\
import org.springframework.cloud.netflix.feign.EnableFeignClients;
' src/main/java/com/example/Consumer/DemoApplication.java

sed -i "" '/@EnableDiscoveryClient/a\
@EnableFeignClients
' src/main/java/com/example/Consumer/DemoApplication.java
vim src/main/java/com/example/Consumer/DemoController.java
package com.example.Consumer;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @GetMapping("/demo")
    public String demo() {
        return "Consumer: " + "demo";
    }
}
  • 测试
./gradlew bootrun
curl localhost:8080/demo # 返回"Consumer: demo"
vim src/main/java/com/example/Consumer/ProviderClient.java
package com.example.Consumer;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient("Provider")
public interface ProviderClient {
    @GetMapping("/demo")
    String demo();
}
vim src/main/java/com/example/Consumer/DemoController.java
// 省略了包名和导入文件

@RestController
public class DemoController {
    @Autowired
    private ProviderClient providerClient;

    @GetMapping("/demo")
    public String demo() {
        return providerClient.demo();
    }
}
  • 测试
./gradlew bootrun
curl localhost:8080/demo # 返回"Provider1: demo"

curl localhost:8080/demo # 返回"Provider2: demo"

参考

  • An Intro to Spring Cloud Zookeeper

  • linux mac下使用sed a\追加文本

  • Spring Cloud Netflix

你可能感兴趣的:(Spring Boot进阶 之 水平扩展)