四,Eureka 第四章

news/2024/11/22 12:23:03/

 

 

 2.1.3 增加依赖

 <!--添加依赖--><dependencies><!--Eureka Server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!--增加公共方法--><dependency><groupId>cn.bdqn</groupId><artifactId>springcloud-api-commons</artifactId><version>${project.version}</version></dependency><!--web--><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.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

 2.1.4 yml

server:port: 7001
eureka:instance:hostname: localhost #eureka服务器端的client:#false 表示不向注册中心注册自己register-with-eureka: false#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-register: falseserver-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 2.1.5编写主启动类


@EnableEurekaServer
@SpringBootApplication
public class EurakeServer7001Application {public static void main(String[] args) {SpringApplication.run(EurakeSever7001Application.class,args);}
}

2.2.2修改pom添加依赖

 <!--添加Eureka client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

2.2.3修改yml:添加Eureka的配置 

 eureka:client:#表示是否将自己注册进EurekaServer默认为trueregister-with-eureka: true#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon  使用负载均衡fetch-registry: trueservice-url:defaultZone: http://localhost:7001/eurekainstance:prefer-ip-address: true #使用ip地址注册

2.2.4修改主启动类:标注Eureka客户端


@SpringBootApplication
@EnableEurekaClientpublic class PaymentApplication {public static void main(String[] args) {SpringApplication.run(PaymentApplication.class,args);}
}

 2.3.2修改pom添加依赖

        <!--eureka client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

 2.3.3 修改yml:添加Eureka的配置

eureka:client:#表示是否将自己注册进EurekaServer默认为trueregister-with-eureka: true#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon  使用负载均衡fetch-registry: trueserver-url:defaultZone: http://localhost:7001/eurekainstance:prefer-ip-address: true #使用ip地址注册

2.3.4修改启动类 标注Eureka客户端

@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class,args);}
}

 

 2.3.4修改主启动类 标注为Eureka客户端

2.3.6补充


//    private static final String PAYMENT_URL="http://localhost:8001";@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;//根据id查询@GetMapping("/consumer/payment/get/{id}")public ResponseResult queryById(@PathVariable("id") Integer id){List<ServiceInstance> serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");ServiceInstance instance = serviceInstances.get(0);ResponseResult rs = restTemplate.getForObject("http://"+instance.getHost()+instance.getPort()+"/payment/get/"+id,ResponseResult.class);
//            ResponseResult rs =restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResponseResult.class);return rs;}//创建订单@GetMapping("/consumer/payment/save")public ResponseResult save(Payment payment){
//        ResponseResult rs =restTemplate.postForObject(PAYMENT_URL+"/payment/save", payment,ResponseResult.class);List<ServiceInstance> serviceInstances =   discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");ServiceInstance instance =  serviceInstances.get(0);ResponseResult rs = restTemplate.postForObject("http://"+instance.getHost()+instance.getPort()+"/payment/save",payment,ResponseResult.class);return  rs;}
}

 

 3.2.1修改pom添加依赖

<!--依赖--><dependencies><!--eureka server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!--公共方法--><dependency><groupId>cn.bdqn</groupId><artifactId>springcloud-api-commons</artifactId><version>${project.version}</version></dependency><!--web--><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><!--测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

3.2.2编写yml

server:port: 7002eureka:instance:hostname: eureka7002.com #eureka服务器端的client:#false 表示不向注册中心注册自己register-with-eureka: false#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 3.2.5 编写启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7002Application {public static void main(String[] args) {SpringApplication.run(EurekaServer7002Application.class,args);}
}

 

 

 

 3.3.3修改pom添加依赖

<dependencies><!--eureka server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!--公共类--><dependency><groupId>cn.bdqn</groupId><artifactId>springcloud-api-commons</artifactId><version>${project.version}</version></dependency><!--web--><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><!--测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

 3.3.4编写yml

server:port: 7003eureka:instance:hostname: eureka7003.com #eureka服务器端的client:#false 表示不向注册中心注册自己register-with-eureka: false#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 3.3.5编写启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7003Application {public static void main(String[] args) {SpringApplication.run(EurekaServer7003Application.class,args);}
}

 

3.4.2 修改后的配置文件 

springcloud-eureka-sever-7001

server:port: 7001
eureka:instance:hostname: eureka7001.com #eureka服务器端的client:#false 表示不向注册中心注册自己register-with-eureka: false#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-register: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://eureka7001.com:7002/eureka/,http://eureka7003.com:7002/eureka/

springcloud-eureka-sever-7001 

server:port: 7002eureka:instance:hostname: eureka7002.com #eureka服务器端的client:#false 表示不向注册中心注册自己register-with-eureka: false#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

 springcloud-eureka-sever003

server:port: 7003eureka:instance:hostname: eureka7003.com #eureka服务器端的client:#false 表示不向注册中心注册自己register-with-eureka: false#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/

 

 

4.1支付微服务发布到 Eureka Sever中

 server:port: 8001spring:application:name: springcloud-payment-provider-servicedatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMTusername: rootpassword: xiaoduo456newmybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包eureka:client:#表示是否将自己注册进EurekaServer默认为trueregister-with-eureka: true#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon  使用负载均衡fetch-registry: trueservice-url:#defaultZone: http://localhost:7001/eurekadefaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka,instance:prefer-ip-address: true #使用ip地址注册

 

 

 

 4.2订单微服务发布到Eureka Server中 

 server:port: 8001spring:application:name: springcloud-payment-provider-servicedatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMTusername: rootpassword: xiaoduo456newmybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包eureka:client:#表示是否将自己注册进EurekaServer默认为trueregister-with-eureka: true#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon  使用负载均衡fetch-registry: trueservice-url:#defaultZone: http://localhost:7001/eurekadefaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka,instance:prefer-ip-address: true #使用ip地址注册

 

5.2.2修改pom 添加依赖

<!--依懒--><dependencies><!--添加Eureka client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--公共的部分--><dependency><groupId>cn.bdqn</groupId><artifactId>springcloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><!--web--><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><version>2.0.0</version></dependency><!--阿里云数据源--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId></dependency><!--驱动--><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><!--lombok--><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>

5.2.3编辑yml 

server:port: 8002spring:application:name: springcloud-payment-provider-servicedatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMTusername: rootpassword: xiaoduo456newmybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: cn.bdqn.domaineureka:client:#表示是否将自己注册进EurekaServer默认为trueregister-with-eureka: true#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon  使用负载均衡fetch-registry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka,instance:prefer-ip-address: true #使用ip地址注册

5.24编写启动类

@SpringBootApplication
@EnableEurekaClient
public class Payment8002Application {public static void main(String[] args) {SpringApplication.run(Payment8002Application.class,args);}
}

5.25编写PaymentMapper接口 


@Mapper
public interface PaymentMapper {//保存一个支付流水public void insert(Payment payment);//根据id获取具体的支付信息public Payment selectById(@Param("id") Integer id);
}

5.2.6编写PaymentMapper.xml映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "--//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bdqn.Mapper.PaymentMapper"><resultMap id="PaymentResultMap" type="cn.bdqn.domain.Payment"><id column="id" property="id"></id><id column="flow_number" property="flowNumber"></id></resultMap><insert id="insert" parameterType="cn.bdqn.Mapper.PaymentMapper">insert into t_payment(flow_number) values(#{flowNumber})</insert><select id="selectById" resultMap="PaymentResultMap">select  id,flow_number from t_payment where id=#{id};</select>
</mapper>

 5.

5.2.7编写payment业务接口以及实现类


public interface PaymentServer {//保存一个支付流水public void save(Payment payment);//根据id获取具体的支付信息public Payment queryById(Integer id);}

@Service
public class PaymentServerImpl implements PaymentServer {@Autowiredprivate PaymentMapper paymentMapper;@Overridepublic Payment queryById(Integer id) {return paymentMapper.selectById(id);}@Overridepublic void save(Payment payment) {paymentMapper.insert(payment);}
}

 

5.2.8  编写paymentController控制器


@RestController
public class PaymentConroller {@Autowiredprivate PaymentServerImpl paymentServer;@GetMapping("/payment/id/{id}")public ResponseResult queryById(@PathVariable(name="id") Integer id){Payment payment = paymentServer.queryById(id);if(payment!=null) {return  new ResponseResult(200,"成功",payment);}else{return  new ResponseResult(404,"没有对应的记录,查询id"+id,null);}}@PostMapping("/payment/save")public ResponseResult save(@RequestBody Payment payment){try {paymentServer.save(payment);return new ResponseResult(200,"插入成功",null);}catch (Exception e){e.printStackTrace();return new ResponseResult(200,"插入失败",null);}}
}


 


http://www.ppmy.cn/news/980787.html

相关文章

【Nodejs】Puppeteer\爬虫实践

puppeteer 文档:puppeteer.js中文文档|puppeteerjs中文网|puppeteer爬虫教程 Puppeteer本身依赖6.4以上的Node&#xff0c;但是为了异步超级好用的async/await&#xff0c;推荐使用7.6版本以上的Node。另外headless Chrome本身对服务器依赖的库的版本要求比较高&#xff0c;c…

【雕爷学编程】Arduino动手做(173)---SG90舵机双轴云台模块

37款传感器与执行器的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&am…

Istio 故障注入与重试的实验

故障注入 Istio流量治理有故障注入的功能&#xff0c;在接收到用户请求程序的流量时&#xff0c;注入故障现象&#xff0c;例如注入HTTP请求错误&#xff0c;当有流量进入Sidecar时&#xff0c;直接返回一个500的错误请求代码。 通过故障注入可以用来测试整个应用程序的故障恢…

Langchain 集成 FAISS

Langchain 集成 FAISS 1. FAISS2. Similarity Search with score3. Saving and loading4. Merging5. Similarity Search with filtering 1. FAISS Facebook AI Similarity Search (Faiss)是一个用于高效相似性搜索和密集向量聚类的库。它包含的算法可以搜索任意大小的向量集&a…

6-Linux的磁盘分区和挂载

Linux的磁盘分区和挂载 Linux分区查看所有设备的挂载情况 将磁盘进行挂载的案例增加一块磁盘的总体步骤1-在虚拟机中增加磁盘2- 分区3-格式化分区4-挂载分区5-进行永久挂载 磁盘情况查询查询系统整体磁盘使用情况查询指定目录的磁盘占用情况 磁盘情况-工作实用指令统计文件夹下…

如何在Debian中配置代理服务器?

开始搭建代理服务器 首先我参考如下文章进行搭建代理服务器&#xff0c;步骤每一个命令都执行过报了各种错&#xff0c;找了博客 目前尚未开始&#xff0c;我已经知道我的路很长&#xff0c;很难走呀&#xff0c;加油&#xff0c;go&#xff01;go&#xff01;go&#xff01; …

vue基础学习笔记

VUE学习笔记 Vue框架第一个Vue程序el挂载点data数据对象Vue指令内容绑定v-text&#xff0c;v-html事件绑定v-on显示切换v-show,v-if属性绑定v-bindv-for列表循环v-model表单元素绑定 Vue框架 javascript框架 简化dom操作 响应式数据驱动 第一个Vue程序 导入开发版本的Vue.js…

【C++STL标准库】序列容器之deuqe与、orwa_list与list

基本概念这里就不再浪费时间去解释&#xff0c;这里给出deuqe与、orwa_list、list的基本使用方法&#xff1a; deque队列&#xff1a; #include <iostream> #include <deque>template <typename T> void print(T Begin, T End);int main() {std::deque<…