idea搭建spring cloud服务注册中心

1.先创建一个maven主项目,file->new->project,选择Maven,next
idea搭建spring cloud服务注册中心_第1张图片
idea搭建spring cloud服务注册中心_第2张图片
2.创建Eureka服务端,在创建好的maven项目里面右键项目名,new->module,选择Spring Initialzr,next
idea搭建spring cloud服务注册中心_第3张图片
idea搭建spring cloud服务注册中心_第4张图片
idea搭建spring cloud服务注册中心_第5张图片
然后next再finish,创建完成后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.0modelVersion>

	<groupId>com.testgroupId>
	<artifactId>eureka-serverartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<packaging>jarpackaging>

	<name>eureka-servername>
	<description>Demo project for Spring Bootdescription>

	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.1.0.RELEASEversion>
		<relativePath/> 
	parent>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
		<java.version>1.8java.version>
		<spring-cloud.version>Greenwich.M1spring-cloud.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
		dependency>

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

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloudgroupId>
				<artifactId>spring-cloud-dependenciesartifactId>
				<version>${spring-cloud.version}version>
				<type>pomtype>
				<scope>importscope>
			dependency>
		dependencies>
	dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

	<repositories>
		<repository>
			<id>spring-milestonesid>
			<name>Spring Milestonesname>
			<url>https://repo.spring.io/milestoneurl>
			<snapshots>
				<enabled>falseenabled>
			snapshots>
		repository>
	repositories>
project>

在EurekaServerApplication.java里面添加@EnableEurekaServer

package com.test.eurekaserver;

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

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

application.properties文件添加

server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

然后启动项目,在浏览器地址栏输入:localhost:8761
idea搭建spring cloud服务注册中心_第6张图片
这里显示No instances available是因为还没有服务注册上去
3.创建Eureka客户端,过程与服务端类似。
idea搭建spring cloud服务注册中心_第7张图片
创建好后在EurekaClientApplication.java里面添加@EnableEurekaClient

package com.test.eurekaclient;

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

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}
}

然后在EurekaClientApplication.java所在包或所在包的子包下创建HelloController.java

package com.test.eurekaclient;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Value("${server.port}")
    String port;
        
    @RequestMapping("/hello")
    public String home(@RequestParam String name) {
        return "hello "+name+",i am from port:" +port;
    }
}

最后配置文件application.properties

spring.application.name=service-hi
server.port=8762
#指向注册中心,http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

然后启动项目,发现在localhost:8761上面有了服务注册上去,名称为service-hi
idea搭建spring cloud服务注册中心_第8张图片
然后浏览器访问localhost:8762/hello?name=hello即可调用服务
idea搭建spring cloud服务注册中心_第9张图片

你可能感兴趣的:(spring,cloud)