springboot集成springcloud

2019年1月13日 17点热度 0条评论 来源: zhangbeizhen18

本例场景:
  springboot集成springcloud
  本例demo源码: https://github.com/zhangbeizhen/springboot-springcloud
1.本例分为3部分:
  1>.服务提供方provider
  2>.服务消费方consumer
  3>.注册中心Eureka
2.注册中心Eureka
  1>.在pom.xml引入依赖

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

  2>.在application.yml配置Eureka信息

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server  # eureka实例的主机名
  client:
    register-with-eureka: false #不把自己注册到eureka上
    fetch-registry: false       #不从eureka上来获取服务的注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka/

   3>.在启动类加注解@EnableEurekaServer,使能Eureka服务器

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

 4>.在浏览器访问Eureka
  http://127.0.0.1:8761
3.服务提供方provider
  1>.在pom.xml引入依赖

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

  2>.在application.yml配置注册信息

server:
  port: 8081
spring:
  application:
    name: provider
#配置eureka信息
eureka:
  instance:
    prefer-ip-address: true  # 注册服务的时候使用服务的ip地址
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

  3>.EmployeeService类

@Service
public class EmployeeService {
  public String getEmpInfo() {
   String info = "这是一个非常棒的同事.";
   return info;
  }
}

  4>.EmployeeController类

@RestController
public class EmployeeController {
  @Autowired
  private EmployeeService employeeService;
  @GetMapping("/emp")
  public String getEmpInfo(){
    return employeeService.getEmpInfo();
  }
}	

4.服务消费方consumer
  1>.在pom.xml引入依赖

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

  2>.在application.yml配置注册信息

spring:
  application:
    name: consumer
server:
  port: 8091
#配置eureka信息
eureka:
  instance:
    prefer-ip-address: true  # 注册服务的时候使用服务的ip地址
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

  3>.RestConfig配置类

@Configuration
public class RestConfig {
  @LoadBalanced /**使用负载均衡机制*/
  @Bean
  public RestTemplate restTemplate(){
    return new RestTemplate();
  }
}

  4>.ManageController类

@RestController
public class ManageController {
  @Autowired
  RestTemplate restTemplate;
  @GetMapping("/query")
  public String getEmpInfo(){
    String info = restTemplate.getForObject("http://PROVIDER/emp", String.class);
    return "消费者服务获取的信息: "+info;
  }
}

  5>.在启动类加@EnableDiscoveryClient注解,开启发现服务功能

@EnableDiscoveryClient //开启发现服务功能
@SpringBootApplication
public class ConsumerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
  }
}

 6>.测试,Postman截图
5.三个服务都启动后,在浏览器访问Eureka
  http://127.0.0.1:8761
  截图:
以上,TKS.  

    原文作者:zhangbeizhen18
    原文地址: https://blog.csdn.net/zhangbeizhen18/article/details/89388777
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。