spring cloud 项目搭建 :1、eureka 服务治理 心已赠人 2023-10-08 12:49 1阅读 0赞 #### 文章目录 #### * 写在前面 * * 1、建库 * 2、先搭建几个基本的模块 * * 2.1、主项目clouddemo * 2.2、子项目-公共 springcloud-api * 2.3、子项目-服务提供者 springcloud-provider-dept-8001 * * 2.3.1、配置 * 2.3.2、启动类 * 2.3.3、dao --> service --> Controller * 2.4、子项目-服务消费者 springcloud-consumer-dept-80 * * 2.4.1、配置 * 2.4.2、启动类 * 2.4.3、dao --> service --> Controller * 2.5、测试访问 * 3、eureka 服务治理 * * 3.1、注册中心(集群) * 3.2、把服务提供者往注册中心注册 * 3.3、查看eureka状态 ## 写在前面 ## 注意spring boot 与spring cloud版本对应问题 ![在这里插入图片描述][20210514153715924.png] 开发时使用的版本对应 ![在这里插入图片描述][20210514153814908.png] [spring cloud 中文文档][spring cloud]:https://www.springcloud.cc/spring-cloud-dalston.html\#\_cloud\_native\_applications 微服务结构: ![在这里插入图片描述][20210604172136950.png] ### 1、建库 ### CREATE TABLE `dept` ( `deptno` bigint(20) NOT NULL AUTO_INCREMENT, `dname` varchar(60) CHARACTER SET utf8mb4 DEFAULT NULL, `db_source` varchar(60) CHARACTER SET utf8mb4 DEFAULT NULL, PRIMARY KEY (`deptno`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COMMENT='部门表'; INSERT INTO `dept` VALUES ('1', '开发部', database()); INSERT INTO `dept` VALUES ('2', '财务部', database()); INSERT INTO `dept` VALUES ('3', '市场部', database()); INSERT INTO `dept` VALUES ('4', '人事部', database()); INSERT INTO `dept` VALUES ('5', '运维部', database()); INSERT INTO `dept` VALUES ('6', '销售部', database()); ### 2、先搭建几个基本的模块 ### 先新建一个主项目+3个子项目(maven) ![在这里插入图片描述][20210514144359735.png] #### 2.1、主项目clouddemo #### pom.xml > 里面写了些公共的依赖spring cloud、mysql、druid、mybatis、junit、log4j、junit、logback <?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"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>clouddemo</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>springcloud-api</module> <module>springcloud-provider-dept-8001</module> <module>springcloud-consumer-dept-80</module> </modules> <!-- 打包方式 --> <packaging>pom</packaging> <!-- 版本号管理 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <lombok.version>1.16.10</lombok.version> </properties> <!-- 依赖管理 --> <dependencyManagement> <dependencies> <!-- spring cloud依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- 数据库连接池,mybatis里面也内置得有一个 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!-- 日志log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> </dependencyManagement> </project> #### 2.2、子项目-公共 springcloud-api #### ![在这里插入图片描述][2021051414551619.png] 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>clouddemo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-api</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project> 实体类 Dept.java package com.demo.bean; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; @Data @NoArgsConstructor @Accessors(chain = true) // 链式写法 dept.setDname("研发部").setDb_source("test") public class Dept { // 主键 private Long deptno; // 部门名字 private String dname; // 数据表名字,一般一个微服务对应一个 数据库 private String db_source; public Dept(String dname){ this.dname = dname; } } #### 2.3、子项目-服务提供者 springcloud-provider-dept-8001 #### > springboot项目 ![在这里插入图片描述][20210514150112416.png] ##### 2.3.1、配置 ##### 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>clouddemo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-provider-dept-8001</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <!-- 你们会发现springboot的依赖都没引入,直接引入mybatis的stater,springboot的依赖就自动引进来了--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- jetty,和tomcat一样是一个web容器,Jetty相比与Tomcat是轻量级的--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <!-- 热部署工具:写完代码等待一会就部署上去了,不用手动重启 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- 导入我们自己的模块 --> <dependency> <groupId>org.example</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <!-- eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- actuator完善监控信息 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project> application.yml server.port: 8001 mybatis: # type-aliases-package: org.example.bean # config-location: classpath:mybatis-config.xml mapper-locations: classpath:mapper/*.xml spring: application: name: springcloud-provider-dept # 不使用mybatis的默认数据源, datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 username: root password: root # 开启mybatis sql语句打印 logging.level.org.exampl.dao: debug mybatis-config.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 其实mybatis-config里面的所有配置都有可以写在yaml里 --> <configuration> <settings> <!-- 开启二级缓存 --> <setting name="cacheEnable" value="true" /> </settings> </configuration> ##### 2.3.2、启动类 ##### DeptProviderApplication.java package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DeptProviderApplication { public static void main(String[] args) { SpringApplication.run(DeptProviderApplication.class,args); } } ##### 2.3.3、dao --> service --> Controller ##### DeptDao.java package org.example.dao; import org.apache.ibatis.annotations.Mapper; import com.demo.bean.Dept; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface DeptDao { public boolean addDept(Dept dept); public Dept queryById(Long id); public List<Dept> queryList(); } DeptMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.example.dao.DeptDao"> <insert id="addDept" parameterType="com.demo.bean.Dept"> insert into dept(dname,db_source) values (#{dname},database()) </insert> <select id="queryById" resultType="com.demo.bean.Dept" parameterType="Long"> select * from dept where deptno=#{id}; </select> <select id="queryList" resultType="com.demo.bean.Dept"> select * from dept; </select> </mapper> DeptService.java package org.example.service; import com.demo.bean.Dept; import java.util.List; public interface DeptService { public boolean addDept(Dept dept); public Dept queryById(Long id); public List<Dept> queryList(); } DeptServiceImpl.java package org.example.service.impl; import com.demo.bean.Dept; import org.example.dao.DeptDao; import org.example.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DeptServiceImpl implements DeptService { @Autowired private DeptDao deptDao; @Override public boolean addDept(Dept dept) { return deptDao.addDept(dept); } @Override public Dept queryById(Long id) { return deptDao.queryById(id); } @Override public List<Dept> queryList() { return deptDao.queryList(); } } DeptController.java package org.example.controller; import com.demo.bean.Dept; import org.example.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/dept") public class DeptController { @Autowired private DeptService deptService; @Autowired private DiscoveryClient client; @RequestMapping("/add") public boolean addDept(Dept dept){ return deptService.addDept(dept); } @RequestMapping("/get/{id}") public Dept queryById(@PathVariable("id") Long id){ return deptService.queryById(id); } @RequestMapping("/get") public List<Dept> queryList(){ return deptService.queryList(); } // 获取微服务的信息 @GetMapping("/discovery") public Object discovery(){ List<String> services = client.getServices(); System.out.println("11111111:"+services); // 通过application name 拿到一个微服务的信息 List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept"); for (ServiceInstance ins:instances) { System.out.println( "主机名称:"+ins.getHost()+"\t"+ "端口号:"+ins.getPort()+"\t"+ "uri地址:"+ins.getUri()+"\t"+ "id:"+ins.getServiceId() ); } return this.client; } } #### 2.4、子项目-服务消费者 springcloud-consumer-dept-80 #### ![在这里插入图片描述][20210514152011672.png] ##### 2.4.1、配置 ##### 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>clouddemo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-consumer-dept-80</artifactId> <dependencies> <!-- 导入我们自己的模块 --> <dependency> <groupId>org.example</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project> application.yml server.port: 80 ##### 2.4.2、启动类 ##### ConsumerApplication.java package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } } ##### 2.4.3、dao --> service --> Controller ##### ConfigBean.java package org.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } } DeptConsumerController.java package org.example.controller; import com.demo.bean.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class DeptConsumerController { // 消费者:没有service层 // RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具 @Autowired private RestTemplate restTemplate; private static final String URL_PREFIX = "http://localhost:8001/"; @RequestMapping("consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id){ return restTemplate.getForObject(URL_PREFIX+"/dept/get"+id,Dept.class); } @RequestMapping("consumer/dept/add") public Boolean add(Dept dept){ return restTemplate.postForObject(URL_PREFIX+"/dept/add",dept,Boolean.class); } @RequestMapping("consumer/dept/get") public List<Dept> getList(Dept dept){ return restTemplate.getForObject(URL_PREFIX+"/dept/get",List.class); } } #### 2.5、测试访问 #### 启动服务提供者和服务消费者 > http://127.0.0.1/consumer/dept/add?dname=运营部 > http://127.0.0.1/consumer/dept/get > http://127.0.0.1/consumer/dept/get/1 ### 3、eureka 服务治理 ### 这我是在同一个机器上搭建三个注册中心,所以需要改一下系统的hosts文件 hosts文件位置:C:\\Windows\\System32\\drivers\\etc 添加内容: 127.0.0.1 127.0.0.1 127.0.0.1 127.0.0.2 127.0.0.1 127.0.0.3 #### 3.1、注册中心(集群) #### 先新建三个模块(maven)用做服务注册中心(集群) ![在这里插入图片描述][20210514154133727.png] 目录结构 ![在这里插入图片描述][2021051416244567.png] pom.xml(除了artifactId,其他内容三个模块都一样) <?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>clouddemo</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-eureka-7001</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- 热部署工具:写完代码等待一会就部署上去了,不用手动重启 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project> 启动类(除了类名,其他内容三个模块都一样) package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer // 开启 eureka 注册中心 public class EurekaServer7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer7001.class,args); } } application.yml(注意port、hostname、hostname这几个地方三个文件不一样) server.port: 7001 # eureka配置 eureka: instance: hostname: 127.0.0.1 client: register-with-eureka: false # 表示是否向eureka注册自己 fetch-registry: false # 表示自己是否为注册中心,false 表示是 service-url: # 监控页面地址 # 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 集群:defaultZone: http://eureka7002.com/eureka/,http://eureka7003.com/eureka/ defaultZone: http://127.0.0.2:7002,http://127.0.0.3:7003 port分别是:7001、7002、7003 hostname分别是:127.0.0.1、127.0.0.2、127.0.0.3 defaultZone分别是: http://127.0.0.2:7002,http://127.0.0.3:7003 http://127.0.0.2:7001,http://127.0.0.3:7003 http://127.0.0.2:7001,http://127.0.0.3:7002 #### 3.2、把服务提供者往注册中心注册 #### application.yml添加配置 # eureka配置 eureka: client: service-url: defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.2:7002/eureka/,http://127.0.0.3:7003/eureka/ instance: # 修改eureka网页上的默认描述信息 instance-id: provider-dept:8001 info: app.name: 我的微服务1 company.name: yayaya 启动类添加注解 @EnableEurekaClient @EnableDiscoveryClient ![在这里插入图片描述][2021051416111565.png] #### 3.3、查看eureka状态 #### 然后启动三个注册中心和服务提供者 访问:http://127.0.0.1:7001/ ![在这里插入图片描述][2021051416175221.png] 访问info信息: http://localhost:8001/actuator/info ![在这里插入图片描述][20210514162052242.png] 查看服务提供者这个微服务的信息: http://localhost:8001/dept/discovery ![在这里插入图片描述][20210514162206962.png] [20210514153715924.png]: https://img-blog.csdnimg.cn/20210514153715924.png [20210514153814908.png]: https://img-blog.csdnimg.cn/20210514153814908.png [spring cloud]: https://www.springcloud.cc/spring-cloud-dalston.html#_cloud_native_applications [20210604172136950.png]: https://img-blog.csdnimg.cn/20210604172136950.png [20210514144359735.png]: https://img-blog.csdnimg.cn/20210514144359735.png [2021051414551619.png]: https://img-blog.csdnimg.cn/2021051414551619.png [20210514150112416.png]: https://img-blog.csdnimg.cn/20210514150112416.png [20210514152011672.png]: https://img-blog.csdnimg.cn/20210514152011672.png [20210514154133727.png]: https://img-blog.csdnimg.cn/20210514154133727.png [2021051416244567.png]: https://img-blog.csdnimg.cn/2021051416244567.png [2021051416111565.png]: https://img-blog.csdnimg.cn/2021051416111565.png [2021051416175221.png]: https://img-blog.csdnimg.cn/2021051416175221.png [20210514162052242.png]: https://img-blog.csdnimg.cn/20210514162052242.png [20210514162206962.png]: https://img-blog.csdnimg.cn/20210514162206962.png
还没有评论,来说两句吧...