上篇文章讲到了如何利用SpringBoot快速搭建一个SpringCloud的
Eureka Server集群。
这篇来讲一下如何创建并向Eureka Server集群注册一个或多个服务提供者、服务消费者。
服务消费端
这是我们开发环境中的目录结构
我们需要的maven的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我们创建两个服务消费端,两个
服务消费端的启动配置文件分别是application-cus1.yml和application-cus2.yml。由于
Eureka Server集群的服务同步机制,我们只需要向集群中的任意一台注册中心注册服务即可。
application-cus1.yml:
## application-cus1.yml
eureka:
client:
serviceUrl:
defaultZone: http://peer1:1111/eureka/
## 如果是向一个注册中心的集群注册,
## 那我们只需要向其中的任意一台注册中心注册即可,
## 该注册中心会把请求转发集群内的其他注册中心(服务同步) http://peer2:1112/eureka/,http://peer3:1113/eureka/
server:
port: 3111
spring:
application:
name: service-ribbon
application-cus2.yml:
## application-cus2.yml
eureka:
client:
serviceUrl:
defaultZone: http://peer1:1111/eureka/
## 如果是向一个注册中心的集群注册,
## 那我们只需要向其中的任意一台注册中心注册即可,
## 该注册中心会把请求转发集群内的其他注册中心(服务同步) http://peer2:1112/eureka/,http://peer3:1113/eureka/
server:
port: 3112
spring:
application:
name: service-ribbon
AlphaController
创建
服务消费者的一个简单的请求控制器
/**
* Created by liaosheng on 2018/4/3.
*/
@RestController
public class AlphaController {
@Autowired
AlphaService alphaService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return alphaService.hiService(name);
}
@RequestMapping(value = "/logging")
public String logging(@RequestParam String name) {
return alphaService.logService(name);
}
}
AlphaService
在下面这个方法中是以HTTP请求的方式调用
服务提供者的方法。
/**
* Created by liaosheng on 2018/4/3.
*/
@Service
public class AlphaService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
public String logService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/root/log?name="+name,String.class);
}
public void aspectTest(){
}
}
CustomerApplication
springBoot启动类。通过@EnableDiscoverClient注解让该应用注册为Eureka客户端应用,以获得服务发现的能力。同时,在该类中创建RestTemplate的Spring Bean实例,
通过@LoadBalanced注解开启客户端负载均衡。
/**
* @author liaosheng
*/
@SpringBootApplication
@EnableDiscoveryClient
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
服务提供端
配置文件
配置了两台服务提供者,都向eureka集群注册自己。
## application-pro1.yml
eureka:
client:
serviceUrl:
defaultZone: http://peer1:1111/eureka/
## 如果是向一个注册中心的集群注册,
## 那我们只需要向其中的任意一台注册中心注册即可,
## 该注册中心会把请求转发集群内的其他注册中心(服务同步) http://peer2:1112/eureka/,http://peer3:1113/eureka/
server:
port: 2111
spring:
application:
name: service-hi
application-pro2.yml
## application-cli2.yml
eureka:
client:
serviceUrl:
defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/,http://peer3:1113/eureka/
server:
port: 2112
spring:
application:
name: service-hi
服务提供者基于HTTP暴露的服务
/**
* Created by liaosheng on 2018/4/4.
*/
@RestController
@RequestMapping("/root")
public class RootController {
@Value("${server.port}")
String port;
@RequestMapping("/log")
public String logAspect(@RequestParam String name){
return "hi "+name+",i am from port:" +port;
}
}
RestAspect类
记录从服务消费端调用各个方法的详细信息
/**
* Created by liaosheng on 2018/4/4.
*/
@Aspect
@Component
public class RestAspect {
@Pointcut("execution(public * com.alpha.provider.web.*.*(..))")
public void point(){};
@Before("point()")
public void doBefore(JoinPoint joinPoint){
System.out.println("方法前-------doBefore-------------------");
ServletRequestAttributes sra=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();
System.out.println("url:"+request.getRequestURI());
System.out.println("ip:"+request.getRemoteHost());
System.out.println("method:"+request.getMethod());
System.out.println("class_method:"+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
System.out.println("args:"+joinPoint.getArgs());
}
@After("point()")
public void doAfter(JoinPoint joinPoint){
System.out.println("方法执行后---------doAfter------------------");
}
@AfterReturning(returning="result",pointcut="point()")
public void doAfterReturning(Object result){
System.out.println("执行返回值:"+result);
}
}
ProviderApplication
服务提供者SpringBoot启动类,通过@EnableDiscoveryClient注解向EureKa注册成为服务。
/**
* @author liaosheng
*/
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}
好,然后我们启动这两个服务提供者和服务消费者
如果不知道在如何在idea中根据SpringBoot配置文件启动两个不同的服务的同学,可以看我的另一篇文章有讲到。点击打开链接
全部的服务都启动后,我们访问我们Eureka Server的管理页面http://localhost:1111
Ribbon提供的负载均衡
然后我们来访问我们其中一个的服务消费者,可以看到是端口号为2111的服务提供者响应了服务调用请求,
紧接着马上第二次访问(注意是短时间内,一段时间没有调用请求的话,又会是2111端口响应。)
第二次就轮到端口号为2112的服务提供者响应服务调用了。