此文主要记录springcloud的zookeeper的注册与发现,没有过多解释,只是为了提供后期拷贝使用
主要模块分为:
0.父工程模块:主要用来作为版本管理
1.公共模块Common-API:用来封装其他模块的共同的功能,比如bean类,共同的异常处理等
2.服务提供者:ConsulClientProvider8001,ConsulClientProvider8002
3.服务消费者:ConsulClientConsumer80
具体配置如下:
父工程(只有pom.xml)
参照springcloud之Zookeeper注册服务与发现
公共模块Common-API
参照springcloud之Zookeeper注册服务与发现
服务提供者ConsulClientProvider8001
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>springcloud</artifactId>
<groupId>com.qwy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ConsulClientProvider8001</artifactId>
<dependencies>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--自己的jar-->
<dependency>
<groupId>com.qwy</groupId>
<artifactId>Common-API</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<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>
</dependencies>
</project>
application.yml
server:
port: 8001
spring:
application:
name: consul-client-provider
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/mydb
username: root
password: admin
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
主启动类
package com.qwy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author:齐
* @Package:com.qwy
* @Project:springcloud1
* @name:ConsulClientProvider8001
* @Date:2022/12/12 16:27
* @Filename:ConsulClientProvider8001
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulClientProvider8001 {
public static void main(String[] args) {
SpringApplication.run(ConsulClientProvider8001.class,args);
}
}
mapper代理接口
参照springcloud之Zookeeper注册服务与发现
服务接口
参照springcloud之Zookeeper注册服务与发现
服务实现类
参照springcloud之Zookeeper注册服务与发现
业务控制器
参照springcloud之Zookeeper注册服务与发现
服务提供者ConsulClientProvider8002
参照服务提供者ConsulClientProvider8001
服务消费者ConsulClientConsumer80
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>springcloud</artifactId>
<groupId>com.qwy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>EurekaClientConsumer80</artifactId>
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.qwy</groupId>
<artifactId>Common-API</artifactId>
<version>1.0-SNAPSHOT</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<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.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.20</version>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 80
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/mydb2
username: root
password: admin
application:
name: consul-client-consumer
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
主启动类
package com.qwy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @Author:齐
* @Package:com.qwy
* @Project:springcloud1
* @name:EurekaClientConsumer80
* @Date:2022/12/11 3:50
* @Filename:EurekaClientConsumer80
*/
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientConsumer80 {
public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumer80.class,args);
}
}
mapper代理接口
参照springcloud之Zookeeper注册服务与发现
服务接口
参照springcloud之Zookeeper注册服务与发现
服务实现类
参照springcloud之Zookeeper注册服务与发现
spring的配置类
package com.qwy.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Author:齐文勇
* @Package:com.qwy.config
* @Project:springcloud1
* @name:ApplicationConfig
* @Date:2022/12/11 4:04
* @Filename:ApplicationConfig
*/
@Configuration
public class ApplicationConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
业务控制器
package com.qwy.controller;
import com.alibaba.fastjson2.JSONObject;
import com.qwy.bean.*;
import com.qwy.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
/**
* @Author:齐文勇
* @Package:com.qwy.controller
* @Project:springcloud1
* @name:OrderController
* @Date:2022/12/11 4:15
* @Filename:OrderController
*/
@RestController
@RequestMapping("/orders")
public class OrderController {
private final String CLIENT_PROVIDER_URL="http://consul-client-provider";
@Autowired
private OrdersService ordersService;
@Autowired
private RestTemplate restTemplate;
@PostMapping
public CommonResult save(@RequestBody Orders orders){
boolean save = ordersService.save(orders);
return new CommonResult(save? Code.SAVE_OK:Code.SAVE_ERR,save);
}
@GetMapping("/{id}")
public CommonResult findOrderAndUserById(@PathVariable("id") Long id){
Orders orders = ordersService.findById(id);
Long usersId = orders.getUsersId();
CommonResult commonResult = restTemplate.getForObject(CLIENT_PROVIDER_URL + "/users/" + usersId, CommonResult.class, usersId);
String josn = JSONObject.toJSONString(commonResult.getData());
Users users = JSONObject.parseObject(josn, Users.class);
ExtendsOrders extendsOrders= new ExtendsOrders();
extendsOrders.setUsers(users);
extendsOrders.setCreateTime(orders.getCreateTime());
extendsOrders.setId(orders.getId());
extendsOrders.setName(orders.getName());
Integer code = orders != null ? Code.SELECT_OK : Code.SELECT_ERR;
String message = orders != null ? "" : "查询失败";
return new CommonResult(code,extendsOrders,message);
}
}