一、创建此spring cloud 项目一共有四个模块,包括公用资源模块,eureka注册中心模块,提供者模块和消费者模块 启动效果如下 父工程pom文件 名称为cloud-api-commons,里面包含公用的entity,接口等等 pom文件如下 创建实体类 支付实体类 名称为cloud-eureka-server7001 ) pom文件如下 然后创建在resources目录下创建application.yml 配置代码如下 创建启动类 创建完之后就启动,在浏览器输入localhost:7001 如果能正常看到这个界面说明注册中心配置成功 模块名称: cloud-provider-payment8001) ** 大家注意一定把自己的cloud-api-commons 替换成自己的坐标** 然后就是写yml文件,在resources下创建application.yml文件 创建数据库备用,首先在mysql创建名称为db2020的数据库然后一个简单的表数据如下 接下来写建立主配置类 创建dao层接口 在resources目录下创建mapper文件,并创建PaymentMapper.xml文件,dao层实现 创建service层 接口 创建接口实现类 然后创建Controller层PaymentController类 到此为止就创建好了提供者,然后我们要先运行注册中心启动类,然后再运行提供者启动类 然后再浏览器输入https://localhost:8001/payment/get/1显示如下页面即说明配置成功 pom文件如下 ** 大家注意一定把自己的cloud-api-commons 替换成自己的坐标** 创建启动类 创建配置文件config. ApplicationContextConfig类 最后创建controller.OrderController类 到此所有的代码都已经完成,接下来首先运行注册中心启动类,然后运行提供者启动类,最后运行消费者提供类,将注册者,消费者注册到Eureka注册中心 然后我们打开浏览器输入localhost:7001查看是否已经注册到Eureka注册中心 当我们看到这个页面就说明已经将提供者和消费者注册到注册中心了 如果出现这个页面就说明配置成功
本人也是刚开始学习spring cloud,写了此笔记,大家耐心看一下,我们共同学习,有不足的地方欢迎各位大佬指正
这里是RunDashboard,如果启动过程没有启动,请看这个链接,教大家如何启动
Run Dashboard 设置参考链接1、首先创建一个Spring cloud父工程
<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.18</lombok.version> <log4j.version>1.2.17</log4j.version> <mysql.version>5.1.47</mysql.version> <druid.version>1.1.16</druid.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version> </properties> <!-- 提供作用:锁定版本+子modlue不用写groupId和version--> <dependencyManagement> <dependencies> <!--spring boot 2.2.2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR1--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency> <!-- druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement>
2、新建子Module
然后选中父模块即可
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.0</version> </dependency> </dependencies>
CommonResult类,给前端返回的json@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); } }
@Data @AllArgsConstructor @NoArgsConstructor public class Payment { private Long id; private String serial; }
3、新建子Module 方法同上 , eureka注册中心模块 名称为cloud-eureka-server7001
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.test.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> </dependencies>
server: port: 7001 eureka: instance: hostname: eureka7001.com #eureka服务端的实例名称 client: #false表示不向注册中心注册自己 register-with-eureka: false #false表示自己就是注册中心,我的之策就是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: #设置于eureka server 交互的地址查询服务和注册服务都需要依赖这个地方 #集群就指向其他的eureka,单机模式就指向自己的 #集群 #defaultZone: https://127.0.0.1:7002/eureka/ #单机 defaultZone: http://127.0.0.1:7001/eureka/ #关闭自我保护机制 #server: # enable-self-preservation: false # eviction-interval-timer-in-ms: 2000
@SpringBootApplication //说明是服务端 @EnableEurekaServer public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class, args); } }
到此注册中心就已经配置成功,接下来要写提供者和消费者模块来注册到注册中心4、新建子Module 方法同上 , 提供者模块 模块名称: cloud-provider-payment8001
pom文件如下 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.test.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> </dependencies>
yml配置如下 server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&charcaterEncoding=utf-8&useSSL=false username: #改成自己的数据库用户名 password: #改成自己的数据库密码 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.test.springcloud.entities eureka: client: register-with-eureka: true fetch-registry: true service-url: #defaultZone: https://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ instance: #显示主机名 instance-id: payment8001 #显示ip prefer-ip-address: true #心跳包发送时间间隔(默认是30s) lease-renewal-interval-in-seconds: 1 #eureka在收到最后一次心跳等待时间上限,单位为s(默认是90s),超时则剔除服务 lease-expiration-duration-in-seconds: 2
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for payment -- ---------------------------- DROP TABLE IF EXISTS `payment`; CREATE TABLE `payment` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `serial` varchar(200) DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; INSERT INTO `payment` VALUES ('1', '12');
@EnableEurekaClient @EnableDiscoveryClient @SpringBootApplication public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
@Mapper public interface PaymentDao { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.springcloud.dao.PaymentDao"> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values (#{serial}); </insert> <resultMap id="BaseResultMap" type="com.test.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from payment where id=#{id}; </select> </mapper>
public interface PaymentService { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
@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); } }
@RestController @Slf4j public class PaymentController { @Autowired private PaymentService paymentService; @Value("${server.port}") private String serverPort; @Autowired private DiscoveryClient discoveryClient; @PostMapping(value = "/payment/create") public CommonResult create( @RequestBody Payment payment){ int result = paymentService.create(payment); log.info("****结果"+result); if(result > 0){ return new CommonResult(200,"插入数据成功,serverPort"+serverPort,result); } else { return new CommonResult(444,"插入数据失败",null); } } @GetMapping (value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id){ Payment result = paymentService.getPaymentById(id); log.info("****结果"+result); if(result != null){ return new CommonResult(200,"查询数据成功,serverPort"+serverPort,result); } else { return new CommonResult(444,"没有对应记录,查询ID:"+id,null); } } @GetMapping(value = "/payment/discovery") public Object discovery(){ List<String> services = discoveryClient.getServices(); for(String s :services){ log.info("**********element"+s); } List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); // System.out.println(instances.get(0)+"/t"+instances.get(1)); for (ServiceInstance instance : instances) { log.info(instance.getServiceId()+"/t"+instance.getHost()+"/t"+instance.getPort()+"/t"+instance.getUri()); } return this.discoveryClient; } }
启动之后打开浏览器输入:localhost:7001 可以看到有这个服务显示就说明已经注册到注册中心了
5、新建子Module 方法同上 ,消费者模块 模块名称:cloud-consumer-order80
<dependencies> <!--eureka自带ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.test.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> </dependencies>
然后就是写yml文件,在resources下创建application.yml文件
yml配置代码如下erver: port: 80 spring: application: name: cloud-order-service eureka: client: service-url: defaultZone: http://localhost:7001/eureka #defaultZone: https://eureka7001.com:7001/eureka/,https://eureka7002.com:7002/eureka/ register-with-eureka: true fetch-registry: true
@SpringBootApplication @EnableEurekaClient @RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = CloudRole.class) public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); }
@Configuration public class ApplicationContestConfig { @Bean //开启负载均衡 @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
@Controller @Slf4j public class OrderController { //public static final String PAYMENT_URL="https://localhost:8001"; public static final String PAYMENT_URL="https://CLOUD-PAYMENT-SERVICE"; @Autowired private RestTemplate restTemplate; @ResponseBody @GetMapping("/consumer/payment/create") public CommonResult<Payment> create (Payment payment){ return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class); } @GetMapping(value = "/consumer/payment/get/{id}") @ResponseBody public CommonResult<Payment> getPayment(@PathVariable("id") Long id){ CommonResult forObject = restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class); // System.out.println(forObject); return forObject; } @GetMapping("consumer/payment/getForEntity/{id}") @ResponseBody public CommonResult<Payment> getPayment2(@PathVariable("id") Long id) { ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class); if (entity.getStatusCode().is2xxSuccessful()) { return entity.getBody(); } else { return new CommonResult<>(444, "操作失败"); } } }
接下来利用消费者进行测试是否可以访问,在浏览器输入:https://localhost/consumer/payment/get/1
到此已经全部结束,这里只是一个简单的将服务注册到eureka注册中心的测试,有不足的地方欢迎各位大佬补充,谢谢大家!
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算