三步走: 1、添加依赖 2、添加application.yml配置文件,配置服务 3、添加启动类

下面是完整的eureka注册服务使用过程

1、创建父工程 spring-cloud-parent

设置maven 父工程pom.xml添加依赖

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">

4.0.0

com.demo0909

spring-cloud-parent

pom

1.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.11.RELEASE

8

8

Hoxton.SR12

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud-version}

pom

import

2、在父工程上创建一个maven子工程,作为eureka server,提供注册服务

eureka-server pom.xml

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">

spring-cloud-parent

com.demo0909

1.0.0

4.0.0

eureka-server

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

在resources下创建application.yml配置文件

server:

port: 8761

#服务器名

spring:

application:

name: EUREKA-SERVER

# eureka 配置

eureka:

instance:

hostname: localhost # 主机名

prefer-ip-address: true #是否将自己的ip地址注册到eureka中

ip-address: 192.168.1.1 #设置当前实例ip

instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} #设置web控制台显示的实例 id

client:

service-url:

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信

添加主启动类EurekaServerApp.class

package com.springcloud.demo.ApplicationController;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication

@EnableEurekaServer //启动eureka server

public class EurekaServerApp {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApp.class, args);

}

}

启动EurekaServerApp.class 访问:http://localhost:8761 出现以下界面表示成功

3、创建一个maven子工程goods-server作为eureka client

goods-server项目pom.xml引入依赖

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">

spring-cloud-parent

com.demo0909

1.0.0

4.0.0

goods-server

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

resources目录下创建application.yml配置文件

server:

port: 8000

spring:

application:

name: GOODS-SERVER #服务名

eureka:

instance:

hostname: localhost

prefer-ip-address: true #是否将自己的ip地址注册到eureka中

ip-address: 192.168.1.1 #设置当前实例ip

instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} #设置web控制台显示的实例 id

lease-renewal-interval-in-seconds: 30 #每次一eureka client向eureka server发送心跳的时间间隔

lease-expiration-duration-in-seconds: 90 #如果90秒内eureka server没有收到心跳包,注册中心删除client

client:

service-url:

defaultZone: http://localhost:8761/eureka #eureka服务端地址,客户端通过该地址与eureka通信

然后在goods-server在com.springcloud.demo.ApplicationController包下创建GoodsServer的主启动类GoodsServerApp.java

package com.springcloud.demo.ApplicationController;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

@ComponentScan("com.springcloud")

@EnableEurekaClient

public class GoodsServerApp {

public static void main(String[] args) {

SpringApplication.run(GoodsServerApp.class, args);

}

}

在goods-server的com.springcloud.demo.Controller包下创建GoodsController.java

GoodsController.java

package com.springcloud.demo.Controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/goods")

public class GoodsController {

@GetMapping("/{id}")

public int findbyid(@PathVariable("id") int id) {

return id;

}

}

启动GoodsServerApp.java 到此goods-server服务已经注册到eureka注册中心了

测试

输入网址:http://localhost:8000/goods/6 结束!

精彩链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: