Spring Cloud Eureka的使用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值