@TOC
問題背景
從零開始學(xué)springcloud微服務(wù)項(xiàng)目
注意事項(xiàng):
- 約定 > 配置 > 編碼
- IDEA版本2021.1
- 這個(gè)項(xiàng)目罚随,我分了很多篇章,每篇文章一個(gè)操作步驟,目的是顯得更簡(jiǎn)單明了
- controller調(diào)service绵疲,service調(diào)dao
- 項(xiàng)目源碼以及sentinel安裝包
cloud-payment-service8004搭建
1 創(chuàng)建module
2 選擇jdk1.8
3 輸入服務(wù)名:cloud-provider-payment8004
4 復(fù)制cloud-provider-payment8001到cloud-provider-payment8004,更改啟動(dòng)類
package com.yg.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author suolong
* @Date 2022/6/14 20:31
* @Version 2.0
*/
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class);
}
}
更改PaymentController
package com.yg.springcloud.controller;
import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import com.yg.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
/**
* @Author suolong
* @Date 2022/6/14 21:36
* @Version 2.0
*/
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@Resource
private PaymentService paymentService;
@Resource
private DiscoveryClient discoveryClient;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("result哈哈哈: {}", result);
if (result > 0) {
return new CommonResult(200, "插入成功", result);
}
return new CommonResult(500, "插入失敗", null);
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("查詢結(jié)果哈哈哈hahah:{}", payment);
if (payment != null) {
return new CommonResult(200, "查詢成功哈: " + serverPort, payment);
} else {
return new CommonResult(500, "沒有對(duì)應(yīng)記錄,查詢ID: " + id, null);
}
}
@GetMapping(value = "/payment/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
for (String element : services) {
System.out.println(element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance element : instances) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return this.discoveryClient;
}
@GetMapping(value = "/payment/zk")
public String paymentzk() {
return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
5 更改application
server:
port: 8004
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 當(dāng)前數(shù)據(jù)源操作類型
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驅(qū)動(dòng)包
url: jdbc:mysql://localhost:3306/mysqltest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
cloud:
zookeeper:
connect-string: 192.168.207.128:2181
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.yg.springcloud.entities # 所有Entity別名類所在包
6 默認(rèn)已安裝zookeeper臣疑,啟動(dòng)zookeeper盔憨,啟動(dòng)程序,打開postman:http://localhost:8004/payment/zk
7 登錄zookeeper客戶端讯沈,創(chuàng)建的是臨時(shí)節(jié)點(diǎn)
sh zkCli.sh
ls /services/cloud-payment-service
get /services/cloud-payment-service/fd829187-2dfa-4e05-b3bd-76b14a3b263b
使用在線json查看json串:
{
"name": "cloud-payment-service",
"id": "fd829187-2dfa-4e05-b3bd-76b14a3b263b",
"address": "B-YUAN-G.com",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-payment-service",
"metadata": {}
},
"registrationTimeUTC": 1655518957100,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [{
"value": "scheme",
"variable": true
}, {
"value": "://",
"variable": false
}, {
"value": "address",
"variable": true
}, {
"value": ":",
"variable": false
}, {
"value": "port",
"variable": true
}]
}
}
cloud-consumer-order81搭建
12
3 輸入服務(wù)名:cloud-consumer-order81
4 復(fù)制cloud-consumer-order80到cloud-consumer-order81郁岩,更改啟動(dòng)類
package com.yg.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author suolong
* @Date 2022/6/15 15:10
* @Version 2.0
*/
@SpringBootApplication
public class MainApp81 {
public static void main(String[] args) {
SpringApplication.run(MainApp81.class);
}
}
引入pom依賴
<?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>springcloud2022</artifactId>
<groupId>com.yg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-order81</artifactId>
<dependencies>
<!-- SpringBoot整合zookeeper客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency><!-- 引入自己定義的api通用包,可以使用Payment支付Entity -->
<groupId>com.yg</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
</dependencies>
</project>
修改application文件
server:
port: 81
spring:
application:
name: cloud-consumer-order
cloud:
#注冊(cè)到zookeeper地址
zookeeper:
connect-string: 10.10.195.193:2181
修改controller,zookeeper的服務(wù)名是區(qū)分大小寫的
package com.yg.springcloud.controller;
import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Author suolong
* @Date 2022/6/15 20:54
* @Version 2.0
*/
@RestController
public class OrderController {
//public static final String PaymentSrv_URL = "http://localhost:8001";
public static final String PaymentSrv_URL = "http://cloud-payment-service";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create") //客戶端用瀏覽器是get請(qǐng)求驯用,但是底層實(shí)質(zhì)發(fā)送post調(diào)用服務(wù)端8001
public CommonResult create(Payment payment) {
return restTemplate.postForObject(PaymentSrv_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult getPayment(@PathVariable Long id) {
return restTemplate.getForObject(PaymentSrv_URL + "/payment/get/" + id, CommonResult.class, id);
}
@GetMapping(value = "/consumer/zk")
public String paymentInfo()
{
String result = restTemplate.getForObject(PaymentSrv_URL+"/payment/zk", String.class);
System.out.println("消費(fèi)者調(diào)用支付服務(wù)(zookeeper)--->result:" + result);
return result;
}
}
5 啟動(dòng)服務(wù)脸秽,登錄zookeeper客戶端
sh zkCli.sh
ls /services/cloud-consumer-order
get /services/cloud-consumer-order/81355215-85a5-4b84-b3a9-627302b4c3c3
{
"name": "cloud-consumer-order",
"id": "81355215-85a5-4b84-b3a9-627302b4c3c3",
"address": "B-YUAN-G.com",
"port": 81,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-consumer-order",
"metadata": {}
},
"registrationTimeUTC": 1655524194818,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [{
"value": "scheme",
"variable": true
}, {
"value": "://",
"variable": false
}, {
"value": "address",
"variable": true
}, {
"value": ":",
"variable": false
}, {
"value": "port",
"variable": true
}]
}
}
SpringCloud無介紹快使用,Seata處理分布式事務(wù)(二十五)
SpringCloud無介紹快使用蝴乔,sentinel服務(wù)熔斷功能(二十四)
SpringCloud無介紹快使用记餐,sentinel注解@SentinelResource的基本使用(二十三)
SpringCloud無介紹快使用,sentinel熱點(diǎn)key限流與系統(tǒng)規(guī)則的基本使用(二十二)
SpringCloud無介紹快使用薇正,sentinel熔斷降級(jí)和限流的基本使用(二十一)
SpringCloud無介紹快使用片酝,Nacos集群和Nginx代理(二十)
SpringCloud無介紹快使用,nacos配置中心的基本使用(十九)
SpringCloud無介紹快使用挖腰,nacos注冊(cè)中心的基本使用(十八)
SpringCloud無介紹快使用雕沿,gateway通過微服務(wù)名實(shí)現(xiàn)動(dòng)態(tài)路由(十七)
SpringCloud無介紹快使用,gateway的基本使用(十六)
SpringCloud無介紹快使用猴仑,Ribbon負(fù)載均衡工具與OpenFeign的使用(十五)
SpringCloud無介紹快使用审轮,使用Zookeeper替換Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)(十四)
SpringCloud無介紹快使用,服務(wù)發(fā)現(xiàn)Discovery和Eureka自我保護(hù)(十三)
SpringCloud無介紹快使用辽俗,集群cloud-provider-payment8002搭建(十二)
SpringCloud無介紹快使用疾渣,集群Eureka服務(wù)注冊(cè)中心cloud-eureka-server7002搭建(十一)
SpringCloud無介紹快使用,單機(jī)Eureka服務(wù)注冊(cè)中心cloud-eureka-server7001搭建(十)
SpringCloud無介紹快使用崖飘,新建cloud-api-commons公共模塊module(九)
SpringCloud無介紹快使用榴捡,新建子module消費(fèi)者訂單模塊(八)
SpringCloud無介紹快使用,熱部署devtools配置(七)
SpringCloud無介紹快使用朱浴,子module提供者支付微服務(wù)業(yè)務(wù)開發(fā)(六)
SpringCloud無介紹快使用吊圾,新建子module提供者支付微服務(wù)yml整合和新建啟動(dòng)類(五)
SpringCloud無介紹快使用,新建子module提供者支付微服務(wù)pom整合(四)
SpringCloud無介紹快使用翰蠢,springcloud父工程pom文件整理(三)
SpringCloud無介紹快使用项乒,IDEA新建springcloud父工程(二)
SpringCloud無介紹快使用,與Spingboot之間的兼容版本選擇(一)
作為程序員第 181 篇文章躏筏,每次寫一句歌詞記錄一下板丽,看看人生有幾首歌的時(shí)間呈枉,wahahaha ...