【SpringCloud】微服务笔记2
技术版本cloudHoxton.SR1boot2.2.2.RELEASEcloud alibaba2.1.0.RELEASEjavajava8Maven3.5及以上Mysql5.7及以上
1. 新建Maven父工程 cloud2020 maven架构选择org.apache.maven.archetypes:maven-archetype-site pom.xml代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 <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.0modelVersion> <groupId > com.atguigu.springcloudgroupId> <artifactId > cloud2020artifactId> <version > 1.0-SNAPSHOTversion> <packaging > pompackaging> <name > Mavenname> <url > http://maven.apache.org/url> <inceptionYear > 2001inceptionYear> <distributionManagement > <site > <id > websiteid> <url > scp://webhost.company.com/www/websiteurl> site> distributionManagement> <properties > <project.build.sourceEncoding > UTF-8project.build.sourceEncoding> <maven.compiler.source > 1.8maven.compiler.source> <maven.compiler.target > 1.8maven.compiler.target> <junit.version > 4.12junit.version> <log4j.version > 1.2.17log4j.version> <lombok.version > 1.16.18lombok.version> <mysql.version > 5.1.47mysql.version> <druid.version > 1.1.16druid.version> <mybatis.spring.boot.version > 1.3.0mybatis.spring.boot.version> properties> <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-dependenciesartifactId> <version > 2.2.2.RELEASEversion> <type > pomtype> <scope > importscope> dependency> <dependency > <groupId > org.springframework.cloudgroupId> <artifactId > spring-cloud-dependenciesartifactId> <version > Hoxton.SR1version> <type > pomtype> <scope > importscope> dependency> <dependency > <groupId > com.alibaba.cloudgroupId> <artifactId > spring-cloud-alibaba-dependenciesartifactId> <version > 2.1.0.RELEASEversion> <type > pomtype> <scope > importscope> dependency> <dependency > <groupId > mysqlgroupId> <artifactId > mysql-connector-javaartifactId> <version > ${mysql.version}version> dependency> <dependency > <groupId > com.alibabagroupId> <artifactId > druidartifactId> <version > ${druid.version}version> dependency> <dependency > <groupId > org.mybatis.spring.bootgroupId> <artifactId > mybatis-spring-boot-starterartifactId> <version > ${mybatis.spring.boot.version}version> dependency> <dependency > <groupId > junitgroupId> <artifactId > junitartifactId> <version > ${junit.version}version> dependency> <dependency > <groupId > org.projectlombokgroupId> <artifactId > lombokartifactId> <version > ${lombok.version}version> <optional > trueoptional> dependency> dependencies> dependencyManagement> <build > <plugins > <plugin > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-maven-pluginartifactId> <configuration > <fork > truefork> <addResources > trueaddResources> configuration> plugin> plugins> build> project>
2. 建立支付module: cloud-provider-payment8001 项目结构:
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 2.1 pom.xml如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 <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 > cloud2020artifactId> <groupId > com.atguigu.springcloudgroupId> <version > 1.0-SNAPSHOTversion> parent> <modelVersion > 4.0.0modelVersion> <artifactId > cloud-provider-payment8001artifactId> <dependencies > <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-webartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-actuatorartifactId> dependency> <dependency > <groupId > org.mybatis.spring.bootgroupId> <artifactId > mybatis-spring-boot-starterartifactId> dependency> <dependency > <groupId > com.alibabagroupId> <artifactId > druid-spring-boot-starterartifactId> <version > 1.1.10version> dependency> <dependency > <groupId > mysqlgroupId> <artifactId > mysql-connector-javaartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-jdbcartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-devtoolsartifactId> <scope > runtimescope> <optional > trueoptional> dependency> <dependency > <groupId > org.projectlombokgroupId> <artifactId > lombokartifactId> <optional > trueoptional> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-testartifactId> <scope > testscope> dependency> dependencies> project>
2.2 application.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 server: port: 8001 spring: application: name: cloud-provider-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding-utr-8&useSSL=false username: root password: root mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.atguigu.springcloud.entities
2.3 主启动类 PaymentMain8001 1 2 3 4 5 6 7 8 9 10 11 12 package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class PaymentMain8001 { public static void main (String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
2.4 数据库 建库:
1 2 3 4 5 CREATE TABLE `payment`( `id` bigint (20 ) NOT NULL AUTO_INCREMENT COMMENT 'ID' , `serial` varchar (200 ) DEFAULT '' , PRIMARY KEY (`id`) ) ENGING= InnoDB AUTO_INCREMENT= 1 DEFAULT CHARSET= utf8
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
2.5 业务类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.atguigu.springcloud.entities;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data @AllArgsConstructor @NoArgsConstructor public class Payment { private Long id; private String serial; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.atguigu.springcloud.entities;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data @AllArgsConstructor @NoArgsConstructor public class CommonResult <T > { private Integer code; private String message; private T data; public CommonResult (Integer code,String message) { this (code,message,null ); } }
1 2 3 4 5 6 7 8 9 10 11 12 package com.atguigu.springcloud.dao;import com.atguigu.springcloud.entities.Payment;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;@Mapper public interface PaymentDao { int create (Payment payment) ; Payment getPaymentById (@Param("id") Long id) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace ="com.atguigu.springcloud.dao.PaymentDao" > <insert id ="create" parameterType ="Payment" useGeneratedKeys ="true" keyProperty ="id" > insert into payment(serial) values(#{serial}) insert> <resultMap id ="BaseResultMap" type ="com.atguigu.springcloud.entities.Payment" > <id column ="id" property ="id" jdbcType ="BIGINT" /> <result column ="serial" property ="serial" jdbcType ="VARCHAR" /> resultMap> <select id ="getPaymentById" parameterType ="Long" resultMap ="BaseResultMap" > select * from payment where id=#{id}; select> mapper>
1 2 3 4 5 6 7 8 9 package com.atguigu.springcloud.service;import com.atguigu.springcloud.entities.Payment;import org.apache.ibatis.annotations.Param;public interface PaymentService { public int create (Payment payment) ; public Payment getPaymentById (@Param("id") Long id) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.atguigu.springcloud.service;import com.atguigu.springcloud.dao.PaymentDao;import com.atguigu.springcloud.entities.Payment;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; public int create (Payment payment) { return paymentDao.create(payment); } public Payment getPaymentById (Long id) { return paymentDao.getPaymentById(id); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package com.atguigu.springcloud.controller;import com.atguigu.springcloud.entities.CommonResult;import com.atguigu.springcloud.entities.Payment;import com.atguigu.springcloud.service.PaymentService;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @PostMapping(value = "/payment/create") public CommonResult create (Payment payment) { int result = paymentService.create(payment); log.info("*****插入结果:" +result); if (result > 0 ){ return new CommonResult(200 ,"插入数据成功" ,result); }else { return new CommonResult(444 ,"插入数据失败" ,null ); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById (@PathVariable("id") Long id) { Payment payment = paymentService.getPaymentById(id); log.info("*****插入结果:" +payment); if (payment != null ){ return new CommonResult(200 ,"查询成功" ,payment); }else { return new CommonResult(444 ,"没有对应记录,查询ID:" +id,null ); } } }
2.6测试 Chrom浏览器可能不支持Post请求,可以使用PostMan工具测试
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
总结: 1. 建module 2. 改pom 3. 写yml 4. 主启动 5. 业务类
; 3. 建立消费者订单module: ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
3.1 pom.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <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 > cloud2020artifactId> <groupId > com.atguigu.springcloudgroupId> <version > 1.0-SNAPSHOTversion> parent> <modelVersion > 4.0.0modelVersion> <artifactId > cloud-consumer-order80artifactId> <dependencies > <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-webartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-actuatorartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-devtoolsartifactId> <scope > runtimescope> <optional > trueoptional> dependency> <dependency > <groupId > org.projectlombokgroupId> <artifactId > lombokartifactId> <optional > trueoptional> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-testartifactId> <scope > testscope> dependency> dependencies> project>
3.2 application.yml
3.3 主启动 1 2 3 4 5 6 7 8 9 package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class OrderMain80 { public static void main (String[] args) { SpringApplication.run(OrderMain80.class,args); } }
3.4业务类 订单也需要Payment、CommonResult实体类,但是不需要操作数据库,没有Service、Dao,只需添加Controller即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.atguigu.springcloud.entities;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data @AllArgsConstructor @NoArgsConstructor public class Payment { private Long id; private String serial; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.atguigu.springcloud.entities;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data @AllArgsConstructor @NoArgsConstructor public class CommonResult <T > { private Integer code; private String message; private T data; public CommonResult (Integer code,String message) { this (code,message,null ); } }
首说RestTemplate: RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集,实现80到8001的远程调用。 官网地址:https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html 使用: 使用restTemplate访问restful接口非常的简单粗暴,(url、requestMap、ResponseBean.class)这三个参数分别代表REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
将RestTemplate对象注册到容器中
1 2 3 4 5 6 7 8 9 10 11 12 package com.atguigu.springcloud.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configuration public class ApplicationContextConfig { @Bean public RestTemplate getRestTemplate () { return new RestTemplate(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.atguigu.springcloud.controller;import com.atguigu.springcloud.entities.CommonResult;import com.atguigu.springcloud.entities.Payment;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@RestController @Slf4j public class OrderController { private static final String PAYMENT_URL="http://localhost:8001" ; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommonResult<Payment> create (Payment payment) { return restTemplate.postForObject(PAYMENT_URL+"/payment/create" ,payment,CommonResult.class); } @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPayment (@PathVariable("id") Long id) { return restTemplate.getForObject(PAYMENT_URL+"/payment/get/" +id,CommonResult.class); } }
3.5启动80、8001服务,测试 80服务调用8001服务,实现效果如下:
查询:,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
添加:)
浏览器并没有返回错误,但是我们来看数据库:)
可以看到数据库只插入主键,并没有插入内容,要在8001的PaymentController加@RequestBody注解。,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
然后就可以插入了,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 4. 工程重构项目中存在相同的代码(entities包下的Payment.class和CommonResult.class),造成代码冗余,可以进行重构。 通过Maven聚合父工程,把相同重复的代码移到公开公用的工程里面,还可以放第三方接口、工具类,统一调配使用。
4.1 建立公共module ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 4.2 pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <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 > cloud2020artifactId> <groupId > com.atguigu.springcloudgroupId> <version > 1.0-SNAPSHOTversion> parent> <modelVersion > 4.0.0modelVersion> <artifactId > cloud-api-commonsartifactId> <dependencies > <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-devtoolsartifactId> <scope > runtimescope> <optional > trueoptional> dependency> <dependency > <groupId > org.projectlombokgroupId> <artifactId > lombokartifactId> <optional > trueoptional> dependency> <dependency > <groupId > cn.hutoolgroupId> <artifactId > hutool-allartifactId> <version > 5.1.0version> dependency> dependencies> project>
4.3 将entities包复制到cloud-api-commons ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 4.4 使用Maven打包发布上传到公用本地库里打开Maven窗口,执行clean测试一下,无误后出现BUILD SUCCESS,然后执行install
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
4.5 删除重复entities,引入maven install的jar包坐标即可使用。 1 2 3 4 5 6 <dependency > <groupId > com.atguigu.springcloudgroupId> <artifactId > cloud-api-commonsartifactId> <version > ${project.version}version> dependency>
5.EurekaServer服务端安装 5.1建module cloud-eureka-server7001 ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 5.2 pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <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 > cloud2020artifactId> <groupId > com.atguigu.springcloudgroupId> <version > 1.0-SNAPSHOTversion> parent> <modelVersion > 4.0.0modelVersion> <artifactId > cloud-eureka-server7001artifactId> <dependencies > <dependency > <groupId > org.springframework.cloudgroupId> <artifactId > spring-cloud-starter-netflix-eureka-serverartifactId> dependency> <dependency > <groupId > com.atguigu.springcloudgroupId> <artifactId > cloud-api-commonsartifactId> <version > ${project.version}version> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-webartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-actuatorartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-devtoolsartifactId> <scope > runtimescope> <optional > trueoptional> dependency> <dependency > <groupId > org.projectlombokgroupId> <artifactId > lombokartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-testartifactId> <scope > testscope> dependency> <dependency > <groupId > junitgroupId> <artifactId > junitartifactId> dependency> dependencies> project>
5.3 application.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 server: port: 7001 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
5.4 主启动类 1 2 3 4 5 6 7 8 9 10 11 12 package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication @EnableEurekaServer public class EurekaMain7001 { public static void main (String[] args) { SpringApplication.run(EurekaMain7001.class,args); } }
这是个服务注册中心,主要干的活就是服务注册,不需要写业务类。 但是注意:Eureka有两个组件,一定要标清楚哪个是Server,哪个是Client。@EnableEurekaServer代表服务注册中心
5.5测试 ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
出现上面图标,表示Eureka 服务端安装成功。No instances available表示当前没有服务注册进来
; 6. 单机Eureka构建:支付微服务8001入驻进eurekaServer6.1将 Eureka-client 依赖引入,便于使用注解@EnableEurekaClient标注这是个Eureka Client端 1 2 3 4 5 <dependency > <groupId > org.springframework.cloudgroupId> <artifactId > spring-cloud-starter-netflix-eureka-clientartifactId> dependency>
6.2 在application.yml添加Eureka相关配置 1 2 3 4 5 6 7 8 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka
6.3 主启动类添加注解@EnableEurekaClient ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 6.4 测试注意: 要先启动EurekaServer
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
这样就注册进来了,入住进Eureka服务器的名称就是8001yml中配置的spring.application.name。红色警告是Eureka的自我保护机制,后面会详细说。
7. 单机Eureka构建:订单微服务入驻进eurekaServer 7.1 在pom添加 Eureka-client依赖 1 2 3 4 5 <dependency > <groupId > org.springframework.cloudgroupId> <artifactId > spring-cloud-starter-netflix-eureka-clientartifactId> dependency>
7.2 在application.yml添加相关配置 1 2 3 4 5 6 7 8 9 10 11 spring: application: name: cloud-order-server eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka
7.3 主启动类添加注解@EnableEurekaClient ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 7.4 测试PS: 先启动EurekaServer,7001服务,再启动服务提供者provider,8001服务
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
cloud-order-server服务以入住,查询功能也可以正常执行
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
8. EurekaServer集群环境构建 8.1 创建module cloud-eureka-server7002 ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 8.2 pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <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 > cloud2020artifactId> <groupId > com.atguigu.springcloudgroupId> <version > 1.0-SNAPSHOTversion> parent> <modelVersion > 4.0.0modelVersion> <artifactId > cloud-eureka-server7002artifactId> <dependencies > <dependency > <groupId > org.springframework.cloudgroupId> <artifactId > spring-cloud-starter-netflix-eureka-serverartifactId> dependency> <dependency > <groupId > com.atguigu.springcloudgroupId> <artifactId > cloud-api-commonsartifactId> <version > ${project.version}version> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-webartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-actuatorartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-devtoolsartifactId> <scope > runtimescope> <optional > trueoptional> dependency> <dependency > <groupId > org.projectlombokgroupId> <artifactId > lombokartifactId> dependency> <dependency > <groupId > org.springframework.bootgroupId> <artifactId > spring-boot-starter-testartifactId> <scope > testscope> dependency> <dependency > <groupId > junitgroupId> <artifactId > junitartifactId> dependency> dependencies> project>
8.3 写yml之前修改映射文件 找到 C:\Windows\System32\drivers\etc路径下的hosts文件
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
修改映射配置添加进hosts文件
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 8.4 修改7001和7002的application.yml ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
8.5 主启动类 1 2 3 4 5 6 7 8 9 10 11 12 package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaServer public class EurekaMain7002 { public static void main (String[] args) { SpringApplication.run(EurekaMain7002.class,args); } }
8.6测试 启动7001、7002
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
使用域名映射:
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
同时看到Eureka图标,且7001指着7002,7002指着7001,说明Eureka集群搭建成功。
; 9. 将两个微服务发布到Eureka集群配置中只需修改application.yml
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
测试 PS: 先启动EurekaServer,7001/7002服务;再启动服务提供者provider,8001;再启动消费者,80
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
现在,就已经把支付服务8001、订单服务80注册进Eureka集群环境,调用也OK。
; 10. 支付提供者8001集群环境搭建10.1 创建module cloud-provider-payment8002 ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 10.2 pom.xml 同8001的 pom.xml 一样10.3 写application.yml,注意改端口 ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 10.4 主启动类和业务类直接从8001拷贝 ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
10.5 修改8001和8002的controller,默认的负载均衡方式是轮询,看执行查询具体调用那台provider ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
; 10.6 测试PS: 启动顺序:7001、7002、8001、8002、80
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
8001和8002也都访问正常,那如果我们用80访问呢?发现怎么刷新都是8001,这是因为我们的源程序地址是写死的:
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
单机版写死是没有问题的,但是现在有8001、8002了,所有不应该再关注具体的IP和端口,而是只认服务名称。代码修改为一下在试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @RestController @Slf4j public class OrderController { private static final String PAYMENT_URL = "http://CLOUD-PROVIDER-SERVICE" ; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommonResult<Payment> create (Payment payment) { return restTemplate.postForObject(PAYMENT_URL+"/payment/create" ,payment,CommonResult.class); } @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPayment (@PathVariable("id") Long id) { return restTemplate.getForObject(PAYMENT_URL+"/payment/get/" +id,CommonResult.class); } }
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
发现报错了,现在对外暴露的不再是地址和端口,只认微服务名称了,可是微服务并不知道下面有几个,找不到这个主机名称,需要使用@LoadBalanced注解开启RestTemplate负载均衡功能。 提前说一下:这个就是后面要介绍的Ribbon负载均衡功能。
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
然后测试,多次刷新,就会发现8001、8002端口交替出现。
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
这样Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号,且该服务还有负载均衡功能了。O(∩_∩)O
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
这个架构是初级篇里面的重点,务必要学会,难的是后面的Alibaba的Nacos,也有服务注册和配置中心,Alibaba Nacos集群就比这个复杂了
,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMjExNjQy,size_16,color_FFFFFF,t_70)
初级篇完结,后续请在博客分类中查找。