Spring Boot 微服务部署

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.appassembler插件

官网: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

你可能感兴趣的:(Spring,Boot)