SpringCloud-Eureka

父工程


<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-dependenciesartifactId>
    <version>Greenwich.SR1version>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-dependenciesartifactId>
    <version>2.1.4.RELEASEversion>
    <type>pomtype>
    <scope>importscope>
dependency>
project>

eureka 注册中心

  • 依赖
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-eureka-serverartifactId>
    <version>1.4.6.RELEASEversion>
dependency>
  • 启动类开启Eureka服务端@EnableEurekaServer
  • 配置信息
server:
  port: 7001
# Eureka
eureka:
  instance:
    # Eureka服务端的实例名称
    hostname: localhost
  client:
    # false表示不向Eureka注册自己
    register-with-eureka: false
    # false表示自己是注册中心
    fetch-registry: false
    service-url:
      # 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群(配置要关联的eureka客户端)
      defaultZone: http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
  • 主机名称配置文件:C:\Windows\System32\drivers\etc\hosts “ip    名称”

服务提供者

  • 依赖

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-eurekaartifactId>
    <version>1.4.6.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
dependency>

  • 配置信息
server:
  port: 8081
mybatis:
  # 别名配置
  type-aliases-package: com.example.pojo
  # 配置类的位置
  config-location: classpath:mybatis/mybatis-config.xml
  # 扫描映射文件
  mapper-locations: classpath:mybatis/mapper/*.xml
spring:
  application:
    name: springcloud-provider-dept
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/spring_cloud?useSSL=true&useUnicode=true&charsetEncoding=UTF-8&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
# eureka
eureka:
  # 客户端注册
  client:
    service-url: 
      # 注册到哪里
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
  instance:
    # 设置客户端实例的访问链接描述
    instance-id: springcloud-provider-dept-8081
# 客户端info配置
info:
  # 访问 localhost:8081/actuator/info 得到json数据:{"app":{"name":"部门信息"}}
  app.name: 部门信息
  • 启动类添加注解
    开启Eureka@EnableEurekaClient
    服务发现@EnableDiscoveryClient
import org.springframework.cloud.client.discovery.DiscoveryClient;
// 获取配置信息,得到具体的微服务等
@Autowired
private DiscoveryClient client;
@GetMapping("/discovery")
public Object discovery(){
    // 获取微服务列表
    List<String> services = client.getServices();
    // 获取具体的微服务的信息
    List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept");
    return client;
}

消费者

  • 引入实体类模块的依赖
  • 配置端口号
  • 添加配置类
@Configuration
public class ConfigBean {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
  • 无需引入远程服务,直接通过http请求,通过 RestTemplate 实现
@RestController
@RequestMapping("/consumer/dept")
public class DeptController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/query/{deptNo}")
    public Dept queryByDeptNo(@PathVariable("deptNo") Long deptNo){
        return restTemplate.getForObject("http://localhost:8081/dept/query/" + deptNo, Dept.class);
    }
}

你可能感兴趣的:(spring,cloud)