根据前一篇文章搭建Spring需要的模块服务,本篇我们学习SpringCloud如何搭建微服务的注册中心并且完成发现与注册功能
由于暂时不需要用到数据库的内容,我们可以搭建时不选择Mysql和Mybatis的服务,选择有关数据库的服务配置文件中要配置与数据库相关的内容
搭建注册中心
- 在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://{server.port}/eureka/ 设置Eureka的地址
- 启动类中添加注解@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);
}
}
- 运行启动类,在地址栏中输入注册中心的IP及端口号http://127.0.0.1:1111
- 可以看出暂未注册任何服务
搭建服务提供端并且进行服务注册
- 同样的生成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/ 注册中心的注册地址
- 配置服务提供端启动类
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);
}
}
- 添加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}获取当前端口号
- 配置完成,运行服务提供端启动类
刷新127.0.0.1:1111/ 可以看到服务名与端口号
搭建客户端调用服务调用注册的方法
- 同样的生成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/
- 配置启动类
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);
}
}
- 编写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及返回值类型即可。
- 分别启动注册中心,服务提供端与调用端
输入127.0.0.1:1111/ 可以看到注册中心中注册的服务
在浏览器中执行3333端口的hello方法可以看到成功调用了服务端2222的getName方法成功获取到返回值