栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud学习笔记01——注册中心Eureka

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud学习笔记01——注册中心Eureka

从今天开始来记录学习SpringCloud的学习笔记。

今天来看一下注册中心怎么创建,概念性的东西我就不多说了一搜一大堆,而且比我解释的都好。

先看一下我的目录结构,每个模块都是单独的一个服务,即微服务。

 

直接上代码。

1.POM依赖

父模块的pom依赖,用来定义版本。



    4.0.0
    pom
    
        springCloud-Eureka
        springCloud-Login
        springCloud-Order
        springCloud-Zuul
        springCloud-Gateway
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
        
    

    org.example
    test-springCloud
    1.0-SNAPSHOT

    
        1.8
        Greenwich.SR1
    


    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    


注册中心的依赖,你没看错!就这一个依赖,没别的了。

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
2.Eureka配置文件
#eureka的端口号
server:
  port: 8888
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.启动类

这里用到一个注解,来启动Eureka服务

@EnableEurekaServer
package com.youyou.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaMainApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(EurekaMainApplication.class, args);
    }
 
}
4.测试

浏览器输入ip和端口号,搞定!

没错!注册中心这就搞定了! 

5.Login模块pom依赖

为了省事,也引用这个,而没有引用client,这并不影响使用。

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
6.写一个接口
package com.youyou.login.controller;

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

@RestController
public class LoginController {

    @RequestMapping("login")
    public String login() {
//        int i = 1/0;
        return "登录成功";
    }
}
7.配置文件
eureka:
  client:
    serviceUrl:
      #      eureka的注册中心地址
      defaultZone: http://localhost:8888/eureka/
server:
  #  此项目端口号
  port: 8890
  servlet:
    context-path: /login
spring:
  application:
    #    注册进eureka的名字
    name: login-server
8.复制一个启动类

这里用到一个注解,用来进行服务注册。

@EnableEurekaClient
package com.youyou.login;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class LoginApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(LoginApplication.class, args);
    }
 
}
9.再启动测试一下

已经注册进来了 

这样我们就实现了服务的注册!很简单,今天就写到这,办公室有点冷先不写了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号