SpringCloud-基于SpringBoot 2.1.3.RELEASE版本从零搭建分布式框架之服务提供者(三)
2019-03-12 16:57
477 查看
SpringCloud-基于SpringBoot 2.1.3.RELEASE版本从零搭建分布式框架之服务提供者(三)
构建model实体模块
这个模块存储数据库实体对象,提供别的模块依赖,线上项目通常是这样的做法。构建model模块 操作不在具体介绍。
模块图如下:
新建实体UserVo:
package com.spring.cloud.model; import lombok.Data; /** * @ClassName UserVo * @Description TODO * @Author cuixhao * @Date 2019/3/7 * @Version 1.0 **/ @Data public class UserVo { private String userId; private String userName; }
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud</artifactId> <groupId>com.spring.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>model</artifactId> <packaging>jar</packaging> </project>
单个服务配置
新建模块 eureka-provider,服务提供者.利用Ribbon实现。
pom.xml
<parent> <artifactId>spring-cloud</artifactId> <groupId>com.spring.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-provider</artifactId> <version>1.0-SNAPSHOT</version> <name>eureka-provider</name> <description>服务端</description> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> 20000 </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.spring.cloud</groupId> <artifactId>model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories>
修改启动类如下
package com.spring.cloud.eureka; 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.web.client.RestTemplate; /** * @ClassName EurekaProviderApplication * @Description TODO * @Author cuixhao * @Date 2019/3/7 * @Version 1.0 **/ @SpringBootApplication @EnableEurekaClient public class EurekaProviderApplication { public static void main(String[] args) { SpringApplication.run(EurekaProviderApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
注:@EnableEurekaClient 和 @EnableDiscoveryClient的区别:
- 这两个都是服务发现的注解,区别是:前者是只针对注册中心是Eureka使用的注解,后者是兼容所有的注册中心,例如:Zookeeper等。
编写一个服务Rest接口
package com.spring.cloud.eureka.controller; import com.spring.cloud.model.UserVo; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @ClassName ClientController * @Description TODO * @Author cuixhao * @Date 2019/3/7 * @Version 1.0 **/ @RestController @Slf4j public class ClientController { @GetMapping("add") public UserVo service(@RequestParam String userId) { UserVo userVo = new UserVo(); userVo.setUserId(userId); userVo.setUserName("杰克"); log.info("执行了"); return userVo; } }
通过输入一个userId参数,返回一个UserVo对象。
编写application.properties文件:
# 服务名字 spring.application.name=PRODUCER-SERVICE # 端口 server.port=7001# 注册中心地址,如果是集群,那就逗号分隔 eureka.client.serviceUrl.defaultZone= \ http://localhost:8001/eureka/
启动项目
先启动Eureka-Server,在启动Eureka-Provider,访问注册中心:http://localhost:8001
出现如图所示,表示服务已经注册成功。
服务集群配置
服务集群配置和上一篇注册中心集群步骤一致。
编写两个服务:
application.properties
# 服务名称 spring.application.name=PRODUCER-SERVICE # 注册中心地址 eureka.client.serviceUrl.defaultZone= \ http://localhost:8001/eureka/
application-service1.properties
server.port=7001
application-service2.properties
server.port=7002
启动命令:
java -jar eureka-provider.jar --spring.profiles.active=service1
java -jar eureka-provider.jar --spring.profiles.active=service2
访问注册中心:http://localhost:8001,就可以看到7001 7002 两个端口的服务注册成功了!
相关文章推荐
- 基于dubbo的分布式项目框架搭建 开发工具idea (springboot+dubbo+zookeeper+redis+rabbitmq+基于Swagger2的restful api) --(四)
- 基于dubbo的分布式项目框架搭建 开发工具idea (springboot+dubbo+zookeeper+redis+rabbitmq+基于Swagger2的restful api) --(一)
- 基于dubbo的分布式项目框架搭建 开发工具idea (springboot+dubbo+zookeeper+redis+rabbitmq+基于Swagger2的restful api) --(二)
- 基于dubbo的分布式项目框架搭建 开发工具idea (springboot+dubbo+zookeeper+redis+rabbitmq+基于Swagger2的restful api) --(五)
- 基于dubbo的分布式项目框架搭建 开发工具idea (springboot+dubbo+zookeeper+redis+rabbitmq+基于Swagger2的restful api) --(三)
- 基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(十七)【权限架构系统(基础框架搭建)】
- SpringBootService,一个基于spring boot搭建的SOA服务框架
- 在IntelliJ IDEA下搭建基于Maven的spring boot框架
- 微服务中基于Spring Boot的maven分布式项目框架的搭建
- 非spring-boot调用基于spring-cloud + zookeeper微服务 - 基础搭建
- 基于springboot搭建dubbo框架(注解和xml配置两种方式)
- 基于SpringBoot 2.0正式版的SpringCloud的微服务实战项目搭建
- 基于springboot的框架搭建(2)统一配置mybatis的增删改方法
- 基于spring boot的企业项目完成基础框架搭建需要的模块
- IDEA基于Spring Cloud Netflix(2.1.0RC3)的Spring Cloud Eureka来实现服务治理的微服务架构搭建以及和SSM框架的整合——实战教程
- 基于spring boot搭建一个spark streaming的实时事件处理框架
- 基于springBoot的zookeeper集群搭建(dubbo2.*版本)
- 基于springboot的框架搭建(3)取消bean对象使用hashmap代替
- [转载]基于 Spring Boot、Spring Cloud、Spring Oauth2 和 Spring Cloud Netflix 等框架构建的微服务项目
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【六】【引入bootstrap前端框架】