SpringCloud搭建微服务注册中心、服务注册及服务调用

根据前一篇文章搭建Spring需要的模块服务,本篇我们学习SpringCloud如何搭建微服务的注册中心并且完成发现与注册功能

由于暂时不需要用到数据库的内容,我们可以搭建时不选择Mysql和Mybatis的服务,选择有关数据库的服务配置文件中要配置与数据库相关的内容

  1. 搭建注册中心
  2. 在src/main/resources/application.properties中配置

    server.port=1111
    eureka.instance.hostname=localhost
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
    
    • server.port=1111 搭建注册中心的端口号为1111
    • eureka.instance.hostname=localhost 访问地址为本地,上传到自己的服务器是要修改为服务器的IP
    • eureka.client.register-with-eureka=false 表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
    • eureka.client.fetch-registry=false 表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
    • eureka.client.service-url.defaultZone=http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/ 设置Eureka的地址
  3. 启动类中添加注解@EnableEurekaServer 意为允许成为注册中心

    package com.springcloud.register;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer     //注册中心
    public class RegisterApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(RegisterApplication.class, args);
    	}
    
    }
    
    
  4. 运行启动类,在地址栏中输入注册中心的IP及端口号http://127.0.0.1:1111

    可以看出暂未注册任何服务

  5. 搭建服务提供端并且进行服务注册
  6. 同样的生成SpringCloud模块,暂不需要与数据库有关的Maven 依赖
    在当前模块的src/main/resources/application.properties中配置

    server.port=2222
    spring.application.name=SERVICE-A
    eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
    
  • server.port=2222 客户端Tomcat端口号为2222
  • spring.application.name=SERVICE-A 注册到注册中心的名字为SERVICE-A !!!注意,尽量不要用下滑线,Ribbon做负载均衡是下划线会不识别找不到服务
  • eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/ 注册中心的注册地址
  1. 配置服务提供端启动类
    package com.springcloud.client;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    @ComponentScan("com.action")
    @EnableEurekaClient
    public class ClientApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(ClientApplication.class, args);
    	}
    
    }
    
    
  2. 添加com.action包及Action类,我们起名为HelloAction
    package com.action;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloAction {
    
    	@Value("${server.port}")
    	private String port;
    	@RequestMapping("/getName")
    	public String hello(){
    		return "你好,中国!我是"+port;
    	}
    }
    

其中${server.port}获取当前端口号

  1. 配置完成,运行服务提供端启动类
    刷新127.0.0.1:1111/ 可以看到服务名与端口号

  2. 搭建客户端调用服务调用注册的方法
  3. 同样的生成SpringCloud模块,暂不需要与数据库有关的Maven 依赖
    在当前模块的src/main/resources/application.properties中配置

server.port=3333
spring.application.name=SERVICE-A
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
  1. 配置启动类
   package com.springcloud.client;

   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;
   import org.springframework.cloud.client.loadbalancer.LoadBalanced;
   import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.ComponentScan;
   import org.springframework.web.client.RestTemplate;

   @SpringBootApplication
   @EnableEurekaClient
   @ComponentScan("com.action")
   public class ClientApplication {

   	@Bean           //在Bean容器中注入此方法,启动模
   	public RestTemplate getRestTemplate(){
   		return new RestTemplate();
   	}

   	public static void main(String[] args) {
   		SpringApplication.run(ClientApplication.class, args);
   	}

   }
  1. 编写com.action/HelloAction

    package com.action;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class HelloAction {
    
    	@Autowired
    	private RestTemplate rt;	//Autowried自动实体化
    
    	@RequestMapping("/hello")
    	public String hello(){
    		String url = "http://SERVICE-B/getName";
    		String result = rt.getForObject(url,String.class);
    		return result;
    	}
    }
    

RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可。

  1. 分别启动注册中心,服务提供端与调用端
    输入127.0.0.1:1111/ 可以看到注册中心中注册的服务

    在浏览器中执行3333端口的hello方法可以看到成功调用了服务端2222的getName方法成功获取到返回值
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值