Eureka微服务注册实践
1. 新建Eureka注册中心
1.2 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.7</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2 application.properties
#Eureka注册中心名
spring.application.name=eureka-server
#服务地址
eureka.instance.hostname=localhost
#服务端口
server.port=1111
#是否将自己注册到Eureka-Server中
eureka.client.registerWithEureka=false
#是否从Eureka-Server中获取服务注册信息
eureka.client.fetchRegistry=false
#注册中心URL
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka
1.3 启动类
package com.hezy.springbootEurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
// EurekaServer声明
@EnableEurekaServer
@SpringBootApplication
public class SpringbootEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootEurekaServerApplication.class, args);
}
}
1.4 启动
java -jar ./springbootEurekaServer/target/springbootEurekaServer-0.0.1-SNAPSHOT.jar


查看注册中心内容

2.新建springboot微服务,springbootHello
2.1 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2 aplication.properties
#微服务名
spring.application.name=HelloServices
#注册中心URL
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
2.3 启动类
package com.hezy.springbootHello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
// EurekaClient声明
@EnableEurekaClient
@SpringBootApplication
public class SpringbootHelloApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHelloApplication.class, args);
}
}
2.4 controller
package com.hezy.springbootHello.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("hello")
public class HelloController {
@RequestMapping("")
public String hello() {
return "Hello World!";
}
}
2.5 启动
java -jar ./springbootHello/target/springbootHello-0.0.1-SNAPSHOT.jar --server.port=8081

查看注册中心内容,已发现微服务

本文介绍了如何使用Spring Boot创建Eureka注册中心,并通过实例展示了如何配置微服务(如SpringbootHello)实现服务注册与发现。步骤包括配置Eureka Server、微服务应用及控制器,详细讲解了启动流程和查看注册中心的内容。
1412

被折叠的 条评论
为什么被折叠?



