eureka配置过程

eureka 配置

1.引入依赖

<?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>heima-springcloud</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.itheima</groupId>
  <artifactId>eureka-server</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  </dependencies>
</project>

2.写驱动器

//声明当前应用为eureka服务
@EnableEurekaServer
@SpringBootApplication 
public class EurekaServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaServerApplication.class);
 }
}

3.编写配置文件
resources\application.yml

server:
port: 10086
spring:
application:
 name: eureka-server # 应用名称,会在Eureka中作为服务的id标识(serviceId)
eureka:
client:
 service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要写其它Server的地址。
  defaultZone: http://127.0.0.1:10086/eureka
 register-with-eureka: false # 不注册自己
 fetch-registry: false #不拉取

1.服务注册方

1.添加依赖

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

2.修改启动类
通过添加 @EnableDiscoveryClient 来开启Eureka客户端功能

@SpringBootApplication
@MapperScan("com.itheima.user.mapper")
@EnableDiscoveryClient // 开启Eureka客户端发现功能
public class UserServiceDemoApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceDemoApplication.class, args);
}
}

3.将服务注册方注册到eureka 修改配置文件

server:
port: 9091
spring:
datasource:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql://localhost:3306/springcloud
 username: root
 password: root
application:
  #应用名
 name: user-service
mybatis:
type-aliases-package: cn.itcast.user.pojo
eureka:
client:
 service-url:
  defaultZone: http://localhost:10086/eureka

2.服务调用方

1.添加依赖

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

2.修改启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
 }
  @Bean
  public RestTemplate restTemplate(){
    return new RestTemplate();
 }
}

3.新增配置文件

server:
port: 8080
spring:
application:
 name: consumer-demo # 应用名称
eureka:
client:
 service-url: # EurekaServer地址
  defaultZone: http://127.0.0.1:10086/eureka

4修改处理器controller 访问此controller

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
  @Autowired
  private RestTemplate restTemplate;
  @Autowired
  private DiscoveryClient discoveryClient;
  @GetMapping("{id}")
  public User queryById(@PathVariable Long id){
    String url = "http://localhost:9091/user/" + id;
    //获取eureka中注册的user-service实例列表
    List<ServiceInstance> serviceInstanceList =discoveryClient.getInstances("user-service");
    //从实例中去吃id和端口
    ServiceInstance serviceInstance = serviceInstanceList.get(0);
    url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()
+ "/user/" + id;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值