我是靠谱客的博主 会撒娇香水,最近开发中收集的这篇文章主要介绍配置Eureka集群Eureka-serverEureka-client查看eureka-server 服务提供方注册进eureka-server 消费方远程调用服务提供方,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
目录
Eureka-server
添加依赖
配置文件
启动类
Eureka-client
添加依赖
配置文件
编辑
启动类
controller
消费方
config
引入ribbon
controller
查看eureka-server
服务提供方注册进eureka-server
消费方远程调用服务提供方
Eureka-server
添加依赖
<?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>mycloud2022</artifactId>
<groupId>com.qfedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mycloud-eureka-server7001</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.qfedu</groupId>
<artifactId>mycloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
配置文件
server:
port: 7001
eureka:
instance:
# eureka服务端的实例名称
hostname: eureka7001.com
client:
# false表示不像注册中心注册自己
register-with-eureka: false
# false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
# 设置与eureka server交互的地址查询服务和注册服务都依赖这个地址
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
启动类
package com.qfedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName EurekaServerMain7001
* @Description TODO
* @Author huocarry
* @Date 2022/12/29 20:35
* @Version 1.0
**/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMain7001.class,args);
}
}
Eureka-client
添加依赖
<?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>mycloud2022</artifactId>
<groupId>com.qfedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mycloud-provider-payment8001</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.qfedu</groupId>
<artifactId>mycloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
配置文件
server:
port: 8001
spring:
application:
name: mycloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/hccy
username: root
password: 990304
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.qfedu.entity
eureka:
client:
# 表示是否让自己注册进Eureka Server,默认为true
register-with-eureka: true
# 是否从Eureka Server抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: payment8001
# 显示ip以及端口号
prefer-ip-address: true
instance属性:
启动类
@EnableEurekaClient
package com.qfedu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @ClassName PaymentMain8001
* @Description TODO
* @Author huocarry
* @Date 2022/12/28 17:29
* @Version 1.0
**/
@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = "com.qfedu.dao")
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
controller
package com.qfedu.controller;
import com.qfedu.entity.Payment;
import com.qfedu.result.CommonResult;
import com.qfedu.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
/**
* @ClassName PaymentController
* @Description TODO
* @Author huocarry
* @Date 2022/12/28 19:56
* @Version 1.0
**/
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@PostMapping("/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("get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("*******查询结果:"+payment);
if (payment!=null){
return new CommonResult(200,"查询成功,serverPort="+serverPort,payment);
}else {
return new CommonResult(444,"没有查询结果,对应ID:"+id,null);
}
}
}
消费方
config
package com.qfedu.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
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;
/**
* @ClassName RestTemplateConfig
* @Description TODO
* @Author huocarry
* @Date 2022/12/29 17:32
* @Version 1.0
**/
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public IRule iRule() {
return new RandomRule();
}
}
引入ribbon
ribbon=负载均衡+RestTemplate
@LoadBalanced注解:能让这个RestTemplate在请求时拥有客户端负载均衡的能力
IRule
controller
package com.qfedu.controller;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qfedu.entity.Payment;
import com.qfedu.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
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;
/**
* @ClassName OrderController
* @Description TODO
* @Author huocarry
* @Date 2022/12/29 17:26
* @Version 1.0
**/
@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderController {
private static final String PAYMENT_URL="http://mycloud-payment-service";
// @Autowired
// private EurekaClient eurekaClient;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/payment/create")
public CommonResult create(Payment payment){
// InstanceInfo info = eurekaClient.getNextServerFromEureka("mycloud-payment-service", false);
// String url = info.getHomePageUrl();
return restTemplate.postForObject(PAYMENT_URL+"payment/create",payment,CommonResult.class);
}
@GetMapping("/payment/get/{id}")
public CommonResult getPayment(@PathVariable("id") Long id){
// InstanceInfo info = eurekaClient.getNextServerFromEureka("mycloud-payment-service", false);
// String url = info.getHomePageUrl();
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
查看eureka-server
服务提供方注册进eureka-server
消费方远程调用服务提供方
最后
以上就是会撒娇香水为你收集整理的配置Eureka集群Eureka-serverEureka-client查看eureka-server 服务提供方注册进eureka-server 消费方远程调用服务提供方的全部内容,希望文章能够帮你解决配置Eureka集群Eureka-serverEureka-client查看eureka-server 服务提供方注册进eureka-server 消费方远程调用服务提供方所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复