版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/it_lihongmin/article/details/91355548
目录
1)、在启动类中添加@EnableDiscoveryClient注解
3)、使用Spring profiles方式启动三个服务节点
1)、启动类中添加@EnableFeignClients 和@EnableDiscoveryClient注解
在之前有写Spring Cloud Eureka的集群服务(Spring Cloud Eureka Server集群和客户端调用),作为微服务的注册和发现中心。但是都知道Eureka Server已经是不会进行更新了,Spring Cloud还支持其他方式的注册和发现中心(Spring Cloud Zookeeper和Consul、etcd等形式),所以很有必要进行对比和选择。下面是一些大致的对比:
Zookeeper集群的搭建可以参照Zookeeper的Windows集群搭建和Zookeeper的Centos集群搭建,在简历集群的基础上,我们开始搭建基于Zookeeper集群的微服务注册和调用,使用Feign 进行负载均衡调用,架构大致如下:
1、从http://start.spring.io生成项目
在start.spring.io中添加Web、Actuator。生成项目后,在下面添加zookeeper-client-provider和zoopeeper-client-consumer两个项目,pom.xml中的dependencies都添加如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-all</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2、zookeeper-client-provider
1)、在启动类中添加@EnableDiscoveryClient注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ZookeeperClientProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperClientProviderApplication.class, args);
}
}
2)、根目录下添加UserController
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Value("${server.port}")
private int port;
@GetMapping("user")
public String getUserInfo(@RequestParam Long id) {
User user = new User();
user.setId(id);
user.setName("userName-" + id);
return user.toString() + "port:" + port;
}
}
3)、使用Spring profiles方式启动三个服务节点
application-zookeeper-1.properties
server.port=8000 spring.application.name=zookeeper-user-provider spring.cloud.zookeeper.connect-string=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 spring.cloud.zookeeper.discovery.enabled=true spring.cloud.zookeeper.discovery.register=true
application-zookeeper-2.properties
server.port=8001 spring.application.name=zookeeper-user-provider spring.cloud.zookeeper.connect-string=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 spring.cloud.zookeeper.discovery.enabled=true spring.cloud.zookeeper.discovery.register=true
application-zookeeper-3.properties
server.port=8002 spring.application.name=zookeeper-user-provider spring.cloud.zookeeper.connect-string=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 spring.cloud.zookeeper.discovery.enabled=true spring.cloud.zookeeper.discovery.register=true
3、zookeeper-client-consumer
1)、启动类中添加@EnableFeignClients 和@EnableDiscoveryClient注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ZookeeperClientConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperClientConsumerApplication.class, args);
}
}
2)、添加一个Controller和一个Service
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserConsumerController {
@Autowired
private UserService userService;
@GetMapping("getUser/{id}")
public String getUserInfo(@PathVariable Long id) {
return userService.getUserInfo(id);
}
}
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "zookeeper-user-provider", fallback = FeignFallBack.class)
public interface UserService {
/**
* 定义Feign的接口
* 使用{@link RequestParam} 一定要添加value值,否则不能正确的解析
*
* @param id 用户id
* @return 用户信息
*/
@GetMapping("user")
String getUserInfo(@RequestParam(value = "id") Long id);
}
3)、添加一个fallback类
import org.springframework.stereotype.Component;
@Component
public class FeignFallBack implements UserService {
@Override
public String getUserInfo(Long id) {
return null;
}
}