Eureka配置注册方和调用方

服务提供者

https://www.cnblogs.com/zou-rong/p/12574586.html接着改造

将user-service注册到Euraka

也有三个步骤

一:添加依赖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>cloud-demo</artifactId>
<groupId>com.company</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>user-service</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>

二:启动类添加注解

package com.company;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import tk.mybatis.spring.annotation.MapperScan;

@EnableDiscoveryClient//兼容zookeeper等,官方推荐使用
//@EnableEurekaClient,只支持Eureka
@SpringBootApplication
@MapperScan("com.company.mapper")
public class UserApplication {

public static void main(String[] args) {
SpringApplication.run(UserApplication.class);
}
}

三:修改配置文件application.yaml,指定服务名称和注册地址

 server:
#端口
port: 8088

logging:
level:
#日志级别,目录: 级别
com.company: debug

  spring:
application:
name: user-service
#配置com.zaxxer:HikariCP:3.4.1连接池
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
username: root
password: yourPassword

#整合mybatis
mybatis:
#启用别名
type-aliases-package: com.company.pojo

#注册Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka

将consumer-service连接到Euraka类同

一:添加依赖pom.xml

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

二:启动类添加注解

package com.company;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}

@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

三:修改配置文件application.yaml,指定服务名称和连接地址

server:
port: 8080
#指定服务名称,默认是unknown
spring:
application:
name: consumer-service
#调用方配置,需要从Eureka拉取服务列表
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8888/eureka

logging:
level:
#日志级别,目录: 级别
com.company: debug

修改consumer-demo的controller

package com.company.controller;

import com.company.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
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;

import java.util.List;

@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerController {

@AutowiredE
private RestTemplate template;

@Autowired
private DiscoveryClient client;//ureka客户端

@GetMapping("/{id}")
public User getUserById(@PathVariable("id")Long id){
//根据服务id(名称)获取服务列表
List<ServiceInstance> instances = client.getInstances("user-service");
//当前服务只有一个实例,集群的时候会有多个
ServiceInstance instance = instances.get(0);
//获取服务实例的IP和端口
String url="http://"+instance.getHost()+":"+instance.getPort()+"/user/"+id;
log.debug(url);
return template.getForObject(url,User.class);
}
}

三个都以debug启动,开始测试

  测试http://localhost:8080/consumer/1consumer控制台打印url

  

 获取的是主机名,配置localhost和127.0.0.1效果一致,比较有意思,调用成功

如果想固定使用IP地址,可以在服务提供方

user-service项目的配置文件中添加如下配置

#注册Eureka
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8888/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
prefer:更喜欢。。

这样消费方获取的时候就是指定的IP地址了  

  

原文地址:https://www.cnblogs.com/zou-rong/p/12575138.html