码迷,mamicode.com
首页 > 编程语言 > 详细

spring cloud - 1

时间:2018-10-21 10:15:57      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:att   lis   dal   type   9.png   tab   value   service   html   

provider:

 

1. pom.xml

    1) properties

    <properties>
                ......
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

 

    2) dependencies

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <dependency>

 

    3) dependencyManagement

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

2. config file

    1) bootstrap.yml

#http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-zookeeper-config.html
spring:
  cloud:
    zookeeper:
      discovery:
        instancePort: ${server.port}
        instanceHost: localhost
        #enabled: true
        #register: true
      connectString: 192.168.1.186:2181,192.168.1.187:2181,192.168.1.188:2181

 

    2) application.properties

server.port=8080
spring.application.name=springcloud_zk

 

3. discovery client - ON

    spring boot main class.

@EnableDiscoveryClient

 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
//public class DemoApplication extends SpringBootServletInitializer {
public class DemoApplication {
    
//    @Override
//    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//        return application.sources(DemoApplication.class);
//    }
    
    public static void main(String[] args) {      
        SpringApplication.run(DemoApplication.class, args);
    }
}

 

 

4. test info

    1) autowired

    @Autowired
    private DiscoveryClient discoveryClient;

 

    2) println

        // https://stackoverflow.com/questions/45085790/spring-cloud-discoveryclient-getlocalserviceinstance-deprecated-how-to-use-r
        discoveryClient.getServices().forEach(id -> {
            discoveryClient.getInstances(id).forEach(instance -> {
                System.out.println("/listPage, host:" + instance.getHost() + ", "
                        + "service_id:" + instance.getServiceId());
            });
        });

 

all code:

    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    public String findAll(Model model) {
        List<UserDetails> users = userService.getUsers();
        
        model.addAttribute("users", users);
        
        for(UserDetails user : users) {
            System.out.println(user.getEmail() + " " + user.getName());
        }
        
        // https://stackoverflow.com/questions/45085790/spring-cloud-discoveryclient-getlocalserviceinstance-deprecated-how-to-use-r
        discoveryClient.getServices().forEach(id -> {
            discoveryClient.getInstances(id).forEach(instance -> {
                System.out.println("/listPage, host:" + instance.getHost() + ", "
                        + "service_id:" + instance.getServiceId());
            });
        });
        
        redisCliTest();
        
        redisTest();
        
        return "userList";
    }

 

    3) zk info

#zkCli.sh
>ls /services/springcloud_zk
>get /services/springcloud_zk/[tab key]

 

技术分享图片

 

    4) print info

    http://localhost:8080/user/listPage

    console:

/listPage, host:localhost, service_id:springcloud_zk

 

 

Reference:

    1. 《SpringCloud之服务注册发现(Spring Cloud Zookeeper) |第十五章 -yellowcong

spring cloud - 1

标签:att   lis   dal   type   9.png   tab   value   service   html   

原文地址:https://www.cnblogs.com/xiaobin-hlj80/p/9823729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!