欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springcloud入门_注册中心 博客分类: java  

程序员文章站 2024-03-24 10:58:58
...
1.启动注册中心实例

    pom.xml

<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>com.chry</groupId>
	<artifactId>springcloud.helloworld.eureka.server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud.helloworld.Eureka.server</name>
	<description>Demo Spring Eureka Server</description>
	
	<!-- 声明SpringBoot-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	
	<!-- 统一项目字符集UTF-8 -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--spring cloud eureka server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- spring boot test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

 

    注册中心EurekaServerApplication启动类 

package springcloud.helloworld.eureka.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/** 服务注册 */
@EnableEurekaServer
/** 启动 */
@SpringBootApplication
/**
 * 
 * @类描述:EurekaServer实例
 * @项目名称:springcloud.helloworld.eureka.server
 * @包名: springcloud.helloworld.eureka.server
 * @类名称:EurekaServerApplication
 * @创建人:wangxuegang
 * @创建时间:2018年12月19日上午9:47:23
 * @mail 15510235102@163.com
 * @version v1.0
 */
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

 

    注册中心application.yml配置文件,运行时,去掉注释

# eureka实例端口
server:
   port: 8761
   
# eureka注册中心服务器配置   
eureka:
   instance:
       # eureka实例IP地址	
       hostname: localhost
   client:
       # eureka实例是否注册自己,默认是 true
       registerWithEureka: false
       # eureka实例发现注册自己,默认是 true
       fetchRegistry: false
       serviceUrl:
       	   # 指定eureka注册中心服务器的地址
           defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/