1,eclipse创建名为cloudsd的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>cloudsd</groupId>
  <artifactId>cloudsd</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>cloudsd</name>
  <description>Spring Cloud project</description>
  <url>http://maven.apache.org</url>

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

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

		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<scope>test</scope>
			</dependency>

			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-eureka-server</artifactId>
			</dependency>
		
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-security</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-security</artifactId>
			</dependency>
		</dependencies>

		<dependencyManagement>
			<dependencies>
				<dependency>
					<groupId>org.springframework.cloud</groupId>
					<artifactId>spring-cloud-dependencies</artifactId>
					<version>Brixton.RELEASE</version>
					<type>pom</type>
					<scope>import</scope>
				</dependency>
			</dependencies>
		</dependencyManagement>

		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>
  
</project>

src/main/resources下创建application.properties,内容如下:

server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#eureka.client.registerWithEureka=false
#eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
security.basic.enabled=false
server.port=8000  配置端口为8000

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

配置表明自己为eureka service而不是eureka client

eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

同上配置一样(去掉)

eureka.client.serviceUrl.defaultZone  注册中心地址


创建启动类

package com.ldcn.cloudsd;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Hello world!
 *
 */
@EnableEurekaServer
@SpringBootApplication
public class ServiceApplication 
{
    public static void main( String[] args ){
    	
    	 SpringApplication.run(ServiceApplication.class, args);
    }
}


@EnableEurekaServer  表明是eureka service

@SpringBootApplication  springboot应用


运行启动类ServiceApplication

访问localhost:8000 可以看到



服务注册中心创建完成,下面创建服务提供者(eureka client)


创建cloudsd.clientA的maven项目,pom同上

resources下创建application.properties,内容如下:

server.port=8010
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
security.basic.enabled=false
spring.application.name=service-hi
eureka.client.serviceUrl.defaultZone 为上面服务注册中心地址

spring.application.name  为服务名称(非常重要,服务与服务之间调用需要用到)


创建clientA的application启动类

package com.ldcn.cloudsd;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
 * Hello world!
 *
 */
@EnableEurekaClient
@SpringBootApplication
public class ClientAApplication 
{
    public static void main( String[] args ){
    	
    	 SpringApplication.run(ClientAApplication.class, args);
    }
}

@EnableEurekaClient  表明为service client

运行clientA的application启动类


打开之前注册中心的地址:localhost:8000

可以看到



注册中心 出现了名为:SERVICE-HI的服务

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐