Eureka服务注册与服务发现
一、环境准备
首先创建了一个Maven环境。修改Pom文件,导入SpringCloud的依赖。用Eureka作为服务注册与发现的组件。
<?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.safesoft</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
<name>main</name>
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
<version>${spring-cloud.version}</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>
二、创建服务注册中心 Eureka – Server
2.1 注册中心导入依赖
<?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">
<parent>
<artifactId>main</artifactId>
<groupId>com.safesoft</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
2.2 编写配置文件
eureka server的配置文件application.yml
如果yml不显示树叶图标,请参考
IDEA创建yml文件不显示小树叶创建失败问题的解决方法
#使用8761端口
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
#下面两个false说明自己是一个 Eureka Server
registerWithEureka: false
fetchRegistry: false
#服务中心的地址
serviceUrl:
defaultZone: http://${ eureka.instance.hostname}:${ server.port}/eureka/
#此服务器名称
spring:
application:
name: eurka-server
2.3 编写启动类
@SpringBootApplication表明这个类是SpringBoot启动的入口。
@EnableEurekaServer 表明这是一个注册中心。
package com.safesoft.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ApplicationServer {
public static void main(String[] args) {
SpringApplication.run(ApplicationServer.class, args);
}
}
2.4 启动注册中心
启动后,在浏览器访问http://localhost:8761/,就能看到注册中心
三、服务提供者 Eureka - Client
刚才的注册中心项目不要关。然后再创建一个新的Module,同样右击项目最上级目录。New – Module – 选择Maven ,点击next – 输入提供者名字 client。
3.1 导入服务提供者项目的依赖
<?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">
<parent>
<artifactId>main</artifactId>
<groupId>com.safesoft</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<dependencies>
<!-- 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.2 编写配置文件
为服务提供者 Eureka - Client 编写 application.yml
#当前项目部署的端口
server:
port: 8762
#配置注册中心的位置,并把自己注册进去
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#当前项目名字
spring:
application:
name: service-client
3.3 编写启动类
@SpringBootApplication注解是SpirngBoot项目启动的入口。
@EnableEurekaClient注解说明这是一个服务提供者Eureka–Client。
package com.safesoft.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ApplicationClient {
public static void main(String[] args) {
SpringApplication.run(ApplicationClient.class, args);
}
}
3.4 编写一个Controller
package com.safesoft.client.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Value("${server.port}")
private String port;
@RequestMapping("/hello")
public String hello(String name) {
return "hello " + name + " , This is a " + port;
}
}
3.5 启动服务提供者
启动以后,访问浏览器http://localhost:8761/,就能在注册中心中,找到刚才注册的服务提供者项目了。注意哦,注册中心项目Eureka–Server 与 服务提供者项目 Eureka–Client 这两个项目都要启动哦,先启动注册中心,再启动服务提供者。
访问Controller代码,输入http://localhost:8762/hello?name="zhangsanfeng"
结果如下
四、项目目录结构及代码
4.1项目目录结构
4.2源码下载地址
spring-cloud-01
参考文章
https://www.jianshu.com/p/19b6553eb603
https://blog.csdn.net/yanluandai1985/article/details/86625348
https://zhoutianyu.blog.csdn.net/article/details/89632721
https://blog.csdn.net/xiaojin21cen/article/details/86704954
还没有评论,来说两句吧...