• Eureka服务注册与发现-提供消费服务模型


    1.工具及软件版本
    • JDK1.8
    • Spring Boot 1.4.3.RELEASE
    <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>1.4.3.RELEASE</version>
     <relativePath/> <!-- lookup parent from repository -->
    </parent>
    • Spring Cloud Camden SR4
    <!--引入spring-cloud依赖-->
    <dependencyManagement>
     <dependencies>
     <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-dependencies</artifactId>
     <version>Camden.SR4</version>
     <type>pom</type>
     <scope>import</scope>
     </dependency>
     </dependencies>
    </dependencyManagement>
    • Maven3.3.9
    • 数据库:mysql5.7
    • 项目目录
    2.编写EureKa Server
    • 项目:microservice-discovery-eureka
    • 在pom.xml中添加以下依赖
    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    
    • 编写启动类,在启动类上添加@EnableEurekaServer
    @SpringBootApplication
    @EnableEurekaServer
    public class MicroserviceDiscoveryEurekaApplication {
     public static void main(String[] args) {
     SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
     }
    }
    
    • 在配置文件application.yml中添加以下内容
    server:
     port: 8761
    eureka:
     client:
     registerWithEureka: false
     fetchRegistry: false
     serviceUrl:
     defaultZone: http://localhost:8761/eureka
    3.将服务提供者注册到Eureka Server上
    • 项目:microservice-simple-provider-user
    • 在pom.xml添加添加以下依赖
    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    • 在配置文件application.yml中添加以下配置
    server:
     port: 8000
     
    spring:
     application:
     name: microservice-provider-user
     datasource:
     url: jdbc:mysql://127.0.0.1:3306/springcloud
     username: root
     password: root
     driver-class-name: com.mysql.jdbc.Driver
     jpa:
     properties:
     hibernate:
     hbm2ddl:
     auto: none
     show-sql: true
     devtools:
     restart:
     additional-exclude: non-restart/**
     resources:
     static-locations: classpath:/resources/static/,classpath:/static/
     
    eureka:
     client:
     serviceUrl:
     defaultZone: http://localhost:8761/eureka/
     instance:
     prefer-ip-address: true
     
    logging:
     level:
     org:
     springframework:
     security: INFO
    • 在启动类上添加@EnableDiscoveryClient注解,声明这是一个Eureka Client
    @SpringBootApplication
    @EnableDiscoveryClient
    public class MicroserviceSimpleProviderUserApplication {
     public static void main(String[] args) {
     SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
     }
    }
    4.将服务消费者注册到Eureka Server上
    • 项目:microservice-simple-consumer-movie
    • 在pom.xml中添加以下依赖
    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
     
    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    • 在配置文件application.yml中添加以下配置
    server:
     port: 8010
     
    user:
     userServiceUrl: http://MICROSERVICE-PROVIDER-USER/
     
    spring:
     application:
     name: microservice-consumer-movie
     
    eureka:
     client:
     serviceUrl:
     defaultZone: http://localhost:8761/eureka/
     instance:
     prefer-ip-address: true
    • 在启动类上添加@EnableDiscoveryClient注解,声明这是一个Eureka Client
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ConsumerMovieApplication {
     @Bean
     @LoadBalanced
     public RestTemplate restTemplate(){
     return new RestTemplate();
     }
     public static void main(String[] args) {
     SpringApplication.run(ConsumerMovieApplication.class, args);
     }
    }
    • 服务调用
    @RestController
    public class MovieController {
     
     @Autowired
     private RestTemplate restTemplate;
     
     @Value("${user.userServiceUrl}")
     private String userServiceUrl;
     
     @GetMapping("/user/{id}")
     public User findById(@PathVariable Long id){
     return this.restTemplate.getForObject(this.userServiceUrl + id,User.class);
     }
    }
     
    注意:消费者调用提供者的服务,可用通过IP的形式也可以通过服务名称,如果通过服务名称获取需要引入ribbon。
     
  • 相关阅读:
    需求分析的方法与实践
    系统架构分析与设计方法论
    装修-3
    装修-2
    装修-1
    daikuan
    JAVA容器全面总结
    超图8C iserver启动成功,访问不了网站localhost:8090/iserver/manager,显示404
    Arcgis中给字段添加属性域
    arcgis for server搭建集群环境
  • 原文地址:https://www.cnblogs.com/myxcf/p/9492249.html
Copyright © 2020-2023  润新知