eureka是个什么东西呢?它是一个服务注册中心。就拿电商系统的例子来说,如果要查看会员的订单详情,那么就要在会员系统的tomcat里面调用订单系统的tomcat里的方法。那么直接通过接口访问吗?显然这是不安全的。因此我们需要一个统一管理远程RPC调用的注册中心。
搭建一个注册中心服务端
1、 创建一个maven工程命名为xx-cloud-eureka-server(不会创建的参考:http://www.sucai66.com/article/detail/20200605/13.html)
2、 在pom.xml增加依赖
<parent>
<artifactId>bg-cloud</artifactId>
<groupId>com.bg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency><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>
3、 接着创建一个启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication{
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args );
}
}
4、 编写配置文件application.yml
# 本身是注册中心,但是也需要注册,这里可以向自己注册
server:
port: 9000
eureka:
instance:
prefer-ip-address: true
hostname: localhost
client:
service-url:
defaultZone: http://http://${eureka.instance.hostname}:${server.port}/eureka/
# 不在页面上显示当前 服务
register-with-eureka: false
# 为true时,可以启动,但报异常:Cannot execute request on any known server
fetch-registry: false
# 服务名 ,很重要 ,后期 config 配置中心会以 服务名为标识找对应的 服务
spring:
application:
name: eureka-server
# 设置当前项目端口号
5、 最后启动EurekaServerApplication,访问http://localhost:9000/就可以打开管理页面了。
搭建服务提供者
1、 创建一个maven工程命名为xx-cloud-eureka-client(不会创建的参考:http://www.sucai66.com/article/detail/20200605/13.html)
2、 在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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bg-cloud</artifactId>
<groupId>com.bg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bg-cloud-user</artifactId>
<packaging>war</packaging>
<name>bg-cloud-user Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<finalName>bg-cloud-user</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3、 接着创建一个启动类
@SpringBootApplication()
@EnableEurekaClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
4、 编写配置文件application.yml
eureka:
prefer-ip-address: true
instance:
appname: eureka-server
hostname: localhost
client:
registry-fetch-interval-seconds: 10
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:9000/eureka
server:
port: 8000
spring:
application:
name: user-server
5、我们创建一个rest服务
/**
* @ClassName IndexController
* @Description 描述此类用途
* @Author lizhijun
* @Date 2020/6/11 12:30
* @Version V1.0
**/
@RestController
@RequestMapping("/index")
public class IndexController {
@RequestMapping("/test")
public String test(){
System.out.println("经过了");
retun "hello test 8000";
}
}
6、启动,去注册中心查看变化,我们把这个项目复制一份将端口改为8001在注册一遍,这样就有连个user-server微服务了,方便下面我们做负载均衡的实验。
查看原文请进:http://www.sucai66.com/article/detail/20200617/20.html