【Spring Cloud】二、Eureka Client 服务注册中心客户端启动注册服务

Eureka客户端启动,将服务注册到eureka服务端注册中心上

maven工程结构如下


pom文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>eureka2</groupId>
	<artifactId>eureka2</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>

	<name>eureka2</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath />
	</parent>


	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-parent</artifactId>
				<version>Dalston.SR1</version>   <!--官网为Angel.SR4版本,但是我使用的时候总是报错 -->
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<finalName>eureka2</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.chiwei.eureka.Client</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<manifestEntries>
							<Class-Path>./</Class-Path>
						</manifestEntries>
					</archive>
					<excludes>
						<exclude>config/**</exclude>
						<exclude>templates/**</exclude>
						<exclude>static/**</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<configuration>
					<excludes>

					</excludes>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>report</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-source-plugin</artifactId>
				<configuration>
					<attach>true</attach>
				</configuration>
				<executions>
					<execution>
						<phase>compile</phase>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<executions>
					<execution>
						<id>make-tgz</id>
						<!-- 绑定到package生命周期阶段上 -->
						<phase>package</phase>
						<goals>
							<!-- 绑定到package生命周期阶段上 -->
							<goal>single</goal>
						</goals>
						<configuration>
							<descriptors> <!--描述文件路径 -->
								<descriptor>assembly.xml</descriptor>
							</descriptors>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

application中相关配置如下:

spring.application.name=service-demo
eureka.client.service-url.defaultZone=http://localhost:8060/eureka

server.port=8070
这里的spring.application.name就是注册的服务名称,后面消费者就可以通过这个名称来调用服务了
eureka.client.service-url.defaultZone这个是eureka注册中心服务器的地址,客户端和消费者后面都会用到这个地址的

server.port就是服务端口号,表明该客户端对外提供的服务是8070端口

代码如下:

package com.chiwei.eureka;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Type Bootstrap.java
 * @Desc 
 * @author chiwei
 * @date 2017年11月9日 下午5:24:53
 * @version 
 */
/**
 * @author chiwei
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class Client {

    /**
     * 主函数入口
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Client.class);
        // 不启动web服务
        // app.setWebEnvironment(false);
        app.run(args);
    }

}

@RestController
class ServiceRestController {

    @Value("${server.port}")
    private int serverPort;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello, Spring Cloud! My port is " + String.valueOf(serverPort);
    }

}

/**
 * Revision history
 * -------------------------------------------------------------------------
 * 
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2017年11月9日 chiwei create
 */

启动该客户端,然后去前一篇的服务端地址访问查看页面信息































猜你喜欢

转载自blog.csdn.net/chiweitree/article/details/78625851