Spring Boot 마이크로 서비스 배치

43056 단어 SpringBoot
Spring Boot 마이크로 서비스 배치
하나의 큰 시스템 을 작은 모듈 로 나 누고 데이터 베이스 도 각 모듈 에 대응 합 니 다.
등록 센터 는 보통 zookeeper, consul, etcd, redis 가 있 습 니 다.
1. 서비스 제공 자
서비스 제공 자 는 서비스 가 시 작 될 때 서비스의 정보 (ip, port) 를 등록 센터 에 등록 해 야 한다.

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-jdbcartifactId>
dependency>


<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.4.6version>
dependency>


<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>1.3.2version>
dependency>


<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
dependency>

<dependency>
    <groupId>org.apache.curatorgroupId>
    <artifactId>curator-x-discovery-serverartifactId>
    <version>4.0.1version>
dependency>
package vip.fkandy.controller;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
import org.apache.curator.x.discovery.ServiceInstance;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class ServiceRegister implements ApplicationRunner {

	@Value("${zookeeper.address}")
	private String zkAddress;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		CuratorFramework client = CuratorFrameworkFactory.newClient(zkAddress, new RetryOneTime(1000));
		client.start();
		client.blockUntilConnected();
		ServiceInstance<Object> instance = ServiceInstance.builder().name("product").address("192.168.1.100").port(8080).build();
		ServiceDiscovery<Object> serviceDiscovery = ServiceDiscoveryBuilder.builder(Object.class).client(client)
				.basePath("/soa").build();
		serviceDiscovery.registerService(instance);
		serviceDiscovery.start();
		System.out.println("service register ok");
	}

}


2. 서비스 발견 자
서비스 호출 을 할 때 먼저 등록 센터 에서 서비스의 주 소 를 얻 은 다음 에 채널 의 서비스 주소 에 따라 호출 해 야 한다.
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
    <scope>testscope>
dependency>

<dependency>
    <groupId>org.apache.curatorgroupId>
    <artifactId>curator-x-discoveryartifactId>
    <version>4.0.1version>
dependency>

부하 균형
package com.example.demo;

import java.util.List;

public class LoadBalance {
	private int index = 0;
	private List<String> services;

	public LoadBalance(List<String> services) {
		this.services = services;
	}

	public String choose() {
		String service = services.get(index);
		index++;
		if (index >= services.size()) {
			index = 0;
		}
		return service;
	}
}

package com.example.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
import org.apache.curator.x.discovery.ServiceInstance;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Springboot12MicroServiceWebApplication {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(Springboot12MicroServiceWebApplication.class, args);
		
		CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.1.100:2181", new RetryOneTime(1000));
		client.start();
		client.blockUntilConnected();
		
		ServiceDiscovery<Object> serviceDiscovery = ServiceDiscoveryBuilder.builder(Object.class).client(client)
				.basePath("/soa").build();
		
		Collection<ServiceInstance<Object>> list = serviceDiscovery.queryForInstances("product");
		List<String> services = new ArrayList<String>();
		list.forEach((instance)->{
			services.add(instance.getAddress()+":"+instance.getPort());
		});
		LoadBalance lb = new LoadBalance(services);
		RestTemplate rt = new RestTemplate();
		String body = rt.getForObject("http://"+lb.choose()+"/soa/product/1", String.class);
	}
}


3. 응용 패키지 와 배치
mvn clean package
mvn clean package dependency:copy-dependencies
java -Djava.ext.dirs=lib vip.fkandy.Application

4. appssembler 플러그 인
홈 페이지:http://www.mojohaus.org/appassembler/appassembler-maven-plugin/
<build>
		<plugins>
			
			<plugin>
				<groupId>org.codehaus.mojogroupId>
				<artifactId>appassembler-maven-pluginartifactId>
				<version>1.10version>
				<configuration>
					
					<platforms>
						<platform>windowsplatform>
						<platform>unixplatform>
					platforms>
					
					<assembleDirectory>${project.build.directory}/springboot-12-micro-serviceassembleDirectory>
					
					<repositoryName>librepositoryName>
					
					<binFolder>binbinFolder>
					
					<configurationDirectory>confconfigurationDirectory>
					
					<copyConfigurationDirectory>truecopyConfigurationDirectory>
					
					<configurationSourceDirectory>src/main/resourcesconfigurationSourceDirectory>
					
					<repositoryLayout>flatrepositoryLayout>
					<encoding>UTF-8encoding>
					<logsDirectory>logslogsDirectory>
					<tempDirectory>tmptempDirectory>
					<programs>
						<program>
							<id>springboot-12-micro-serviceid>
							<mainClass>vip.fkandy.ApplicationmainClass>
							<jvmSettings>
								<extraArguments>
									<extraArgument>-serverextraArgument>
									<extraArgument>-Xmx1GextraArgument>
									<extraArgument>-Xms1GextraArgument>
								extraArguments>
							jvmSettings>
						program>
					programs>
				configuration>
			plugin>
		plugins>
	build>

포장 명령
mvn clean package appassembler:assemble

좋은 웹페이지 즐겨찾기