搭建Eureka,实现服务注册与发现
springboot搭建项目
一.创建springboot项目
1.1. New Project -> Maven -> Next
1.2. 输入GroupId,ArtifactId -> Next -> Finish
1.3. 在pom.xml中加入SpringBoot相关的Jar包
<?xml version="1.0" encoding="UTF-8"?>
<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.sun</groupId>
<artifactId>springbootService</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!-- 添加这个依赖之后就可以创建一个web应用程序。starter poms部分可以引入所有需要在实际项目中使用的依赖。
spring-boot-starter-web依赖包含所有的spring-core, spring-web, spring-webmvc,嵌入的Tomcat server和其他web应用相关的库。 -->
<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.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<!--Spring Cloud Config 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<!--Spring Boot Actuator,感应服务端变化-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.4. 在resources中添加application.properties,这个文件用来进行 项目的相关配置
1.4. 项目目录结构
1.5. controller层代码
注:这边只是简单书写controller层代码,只是没有添加业务功能代码,可自行添加
package sun.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "service")
public class testControoler {
@GetMapping(value = "hello")
public String login() {
return "success";
}
}
1.6. ConsumerClientApplication层代码
注:此类为springboot启动类
package sun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@SpringBootApplication
@ServletComponentScan
@EnableDiscoveryClient
@RefreshScope
public class ConsumerClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerClientApplication.class,args);
}
}
1.7. 启动项目
注:
启动时候如果出现此类错误:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
原因:
在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。
禁止方式如下:一,在application.yml配置文件中增加以下内容
###客户端调用地址
client:
serviceUrl:
defaultZone:
###是否将自己注册到Eureka服务中,因为该应用本身就是注册中心,不需要再注册自己(集群的时候为true)
register-with-eureka: false
###是否从Eureka中获取注册信息,因为自己为注册中心,不会在该应用中的检索服务信息
fetch-registry: false
二,在application.properties配置文件中增加以下内容
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
1.8. 接口请求检验是否请求成功
二.搭建Eureka并使服务发现
注:出错
因为出现版本问题,先将之前pom文件重新进行配置了,重新引入springboot----->springcloud------>Eureka。只需要把之前的pom文件替换一下就好
2.1. 在pom.xml文件中添加配置
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sun</groupId>
<artifactId>springbootService</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--netflix-eureka-server服务端注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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>
<!-- springcloud的版本RELEASE -->
<version>Hoxton.SR9</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>
</project>
2.2. 在ConsumerClientApplication类添加:
package sun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ConsumerClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerClientApplication.class,args);
}
}
2.3 application.properties文件
#端口号
server.port=8761
#应用名称
spring.application.name=eureka-server
eureka.instance.hostname=192.168.137.1
#代表不向注册中心注册自己
eureka.client.register-with-eureka=false
#维护服务实例,不需要检索服务
eureka.client.fetch-registry=false
#eureka监控平台地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
2.4 进行启动
直接访问http://192.168.137.1:8761/。启动成功界面
2.4 将原来创建的springbootService项目配置进行修改,目的是为Eureka能发现此服务
2.5 application.properties配置文件进行修改
注:其中eureka.client.service-url.defaultZone参数设置内容为Eureka项目启动路径
spring.application.name = springbootService
server.port = 8888
eureka.client.service-url.defaultZone=http://192.168.137.1:8761/eureka/
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
2.6 ConsumerClientApplication类进行修改
注:添加EnableDiscoveryClient注解,作用是使得Eureka发现
package sun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@SpringBootApplication
@EnableEurekaClient
public class ConsumerClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerClientApplication.class,args);
}
}
2.7 pom.xml文件添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2.8 打开Eureka界面
注:在Eureka中可以看到springbootService已注册
码云地址:https://gitee.com/dafengcheng/spring
本文地址:https://blog.csdn.net/blueAndSmile/article/details/109596037
下一篇: 设计模式笔记(2)
推荐阅读
-
springcloud使用之服务的注册发现与消费
-
基于 Consul 实现 MagicOnion(GRpc) 服务注册与发现
-
Eureka(服务注册与发现)简单入门
-
Windows环境下实现Consul服务注册和服务发现
-
SpringCloud-微服务的注册与发现Eureka
-
SpringCloud(二):服务的注册与发现(Eureka)
-
SpringCloud:1章 Eureka服务注册与发现
-
微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现
-
10分钟搭建服务器集群——Windows7系统中nginx与IIS服务器搭建集群实现负载均衡
-
SpringCloud第二代实战系列:一文搞定Nacos实现服务注册与发现