基于Spring Boot 2.x的Spring Cloud服务注册与发现

基于SpringBoot 2.x的Spring Cloud服务注册与发现

  • 创建服务注册中心
    • 创建springboot工程
    • 添加注解
    • 添加配置
    • 启动服务
  • 创建服务提供者
    • 修改配置
    • 启动服务
  • 代码下载

创建服务注册中心

创建springboot工程

  1. 进入https://start.spring.io
  2. 选择Maven Project、Java、2.1.4,添加Eureka server依赖基于Spring Boot 2.x的Spring Cloud服务注册与发现_第1张图片
  3. 点击Generate Project,解压下载的zip压缩包,这时一个简易的Spring Boot工程就创建起来了

添加注解

启动类添加@EnableEurekaServer注解

package com.example.cloudeurekaserver;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

添加配置

  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.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.1.4.RELEASEversion>
		<relativePath/> 
	parent>
	<groupId>com.examplegroupId>
	<artifactId>cloud-eureka-serverartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>cloud-eureka-servername>
	<description>Demo project for Spring Bootdescription>

	<properties>
		<java.version>1.8java.version>
		<spring-cloud.version>Greenwich.SR1spring-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>

project>
  1. 修改application.properties为application.yml,添加以下配置
server:
  # 服务端口
  port: 8001

eureka:
  instance:
    # eureka主机名,会在控制页面中显示
    hostname: localhost
  client:
    # 是否将自己注册到Eureka Server,默认为true
    # 由于当前这个应用就是Eureka Server,故而设为false
    registerWithEureka: false
    # 表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,
    # 不需要同步其他的Eureka Server节点的数据,故而设为false
    fetchRegistry: false
    serviceUrl:
      # eureka注册中心服务器地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动服务

启动Spring Boot项目,在浏览器中输入http://localhost:8001即可进入Eureka主页面。
基于Spring Boot 2.x的Spring Cloud服务注册与发现_第2张图片

创建服务提供者

修改配置

提供者项目创建方式与注册中心服务器基本相同,只需做以下修改:

  1. 修改启动类注解@EnableEurekaServer为@EnableEurekaClient
  2. 修改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>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.1.4.RELEASEversion>
		<relativePath/> 
	parent>
	<groupId>com.examplegroupId>
	<artifactId>cloud-eureka-clientartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>cloud-eureka-clientname>
	<description>Demo project for Spring Bootdescription>

	<properties>
		<java.version>1.8java.version>
		<spring-cloud.version>Greenwich.SR1spring-cloud.version>
	properties>

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

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

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-configartifactId>
		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>

project>
  1. 修改application.yml如下:
spring:
  application:
    #服务名
    name: service-01

server:
  #服务端口
  port: 8002

eureka:
  instance:
    #eureka主机名,会在控制页面中显示
    hostname: localhost
    #eureka服务器页面中status的请求路径
    status-page-url: http://${eureka.instance.hostname}:${server.port}/index
  client:
    serviceUrl:
      #在注册中心中进行注册
      defaultZone: http://localhost:8001/eureka/

启动服务

启动项目成功后,即可在服务中心DS Replicas -> Instances currently registered with Eureka下发现此服务提供者了。
基于Spring Boot 2.x的Spring Cloud服务注册与发现_第3张图片

代码下载

https://github.com/atparting/springcloud_demo

你可能感兴趣的:(Java)